add cart choropleth column cross cube error file folder geo help home lock obs poi rdf remove search slice spreadsheet success table unlock warning

[this is a icon-] developer tool

SPARQL 1.1 Query: Results

Edit query
Query results
s p_blank o_blank
http://statistics.gov.scot/id/statistical-geography/S02001348 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001348 geosparql: asWKT POLYGON ((-2.707651542631436 56.49933446816858, -2.709925006824697 56.498096569144124, -2.7122088965430664 56.496280357813745, -2.714167479723835 56.49540123264197, -2.7152891929810634 56.49554032060345, -2.716031538123093 56.49537163080024, -2.7166285662033895 56.494904600488674, -2.7161693806423957 56.49427591812067, -2.7171967327310327 56.49399989199149, -2.718442005429519 56.49482487993669, -2.7183465765268275 56.49517844038082, -2.7166206683206555 56.496982697705, -2.7171004567470076 56.497913927926085, -2.72139386495521 56.49712987483988, -2.7218081694211578 56.497634743769424, -2.7350382937815847 56.495203974629845, -2.739925399285711 56.49423610461466, -2.7412345205691224 56.49644290462177, -2.7427532769833194 56.496029538402986, -2.743387703210743 56.4960796564229, -2.7440254343338086 56.49712698486443, -2.746184779878706 56.499557743047234, -2.7470844521016757 56.499478842689385, -2.74743598893574 56.49982219851184, -2.7515171843852264 56.49915427706322, -2.752847879996099 56.49888413726512, -2.753392684416089 56.49919056523585, -2.7542486383185607 56.49888755034695, -2.755953174247472 56.4989963078454, -2.757563447270273 56.49870058889933, -2.7573346854041874 56.49803486736167, -2.756403921514719 56.496742036858464, -2.7584449973309235 56.496443681108474, -2.760090174518076 56.49789890055131, -2.7607806135943385 56.49772672975473, -2.761165084978546 56.49815351581063, -2.760528281799944 56.498743002434956, -2.7565742213587625 56.50018596686062, -2.7563074042104136 56.501251354414194, -2.7570577757346486 56.501675438826275, -2.756684362233024 56.502095149850966, -2.7551264467214027 56.50099044203613, -2.7543626044894207 56.50106743452683, -2.753434238742609 56.50071972892919, -2.7529303341726257 56.49984220295823, -2.7514776718818887 56.500189348894985, -2.752201035977513 56.5006578014364, -2.751987691905746 56.5009800916898, -2.750143721351371 56.500587833391855, -2.749261720774535 56.500827395323135, -2.7496765702666806 56.50117191710425, -2.746980565356757 56.50211861610999, -2.745945884171442 56.50145485358719, -2.745145218041576 56.50121708587879, -2.7417073302379564 56.50152516230955, -2.74064007977405 56.50125661431591, -2.7377121469604333 56.50177375423432, -2.737141252380768 56.50211723346855, -2.7348576562167417 56.50284564271956, -2.734082801832863 56.502073583541005, -2.7328819588994544 56.501299048022574, -2.7301528462782114 56.50180806179334, -2.7305443407457166 56.50235899917948, -2.728395170494092 56.50282582098171, -2.7302464130174546 56.504477021922895, -2.728169889008185 56.504632962255855, -2.7271405333881735 56.50457821519377, -2.7263885918754127 56.50408637206982, -2.727003372997494 56.50310349427274, -2.7265201027143933 56.50246844249848, -2.7258891039158377 56.50217957010565, -2.7262267866958605 56.50157173558588, -2.724233991169147 56.50143400169094, -2.724408549622682 56.50078275923264, -2.717454412721482 56.50070633596269, -2.7142486884050303 56.500744976003986, -2.7120986620238687 56.500970761536436, -2.7112205113849637 56.500720255953865, -2.7104833298443984 56.49972221462237, -2.708265992109517 56.50015714814525, -2.707651542631436 56.49933446816858))
SPARQL API: The Basics

The most flexible way to access the data is by using SPARQL, a query language, analagous to SQL for relational databases, for retrieving and manipulating data from graph databases like ours. We support SPARQL 1.1 query syntax. Many online tutorials are available.

To submit a SPARQL query from your code, you issue an HTTP GET or POST to our endpoint:http://statistics.gov.scot/sparql, with the query itself as a url-encoded parameter called query.

For example, to run the following simple SPARQL query and get the results as JSON:

SELECT * WHERE {?s ?p ?o} LIMIT 10

Option 1: POST (recommended)

Issue a POST to the endpoint, with the query in the body, and an Accept header of sparql-results+json:

POST http://statistics.gov.scot/sparql HTTP/1.1
Host: statistics.gov.scot
Accept: application/sparql-results+json
Content-Type: application/x-www-form-urlencoded

query=SELECT+%2A+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10

Option 2: GET

Issue a GET to the following URL (note the .json extension - see the formats section for more detail on this):

GET http://statistics.gov.scot/sparql.json?query=SELECT+%2A+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10

Scroll down to the end of this page for examples of both of these methods in a few different languages.

Results formats

As with other aspects of our API, to get the data in different formats, you can use either (a) a format extension or (b) an HTTP Accept header. Available result formats depend on the type of SPARQL query. There are four main forms:

SELECT queries return tabular results, and the formats available reflect this:

Format Extensions Accept Headers
XML .xml application/xml,
application/sparql-results+xml
JSON .json application/json,
application/sparql-results+json
Text .txt, .text text/plain
CSV .csv text/csv

CONSTRUCT and DESCRIBE queries return graph data, so the results are available in the same formats as our resource APIs:

Format Extensions Accept Headers
RDF/XML .rdf application/rdf+xml
N-triples .nt, .txt, .text application/n-triples,
text/plain
Turtle .ttl text/turtle
JSON-LD .json application/ld+json,
application/json

ASK queries return a boolean result:

Format Extensions Accept Headers
XML .xml application/xml,
application/sparql-results+xml
JSON .json application/json,
application/sparql-results+json
Text .txt, .text text/plain
Results pagination

We accept page and per_page parameters for paginating the results of SELECT queries (we automatically modify your query to apply LIMIT and OFFSET clauses). For other query types (i.e. DESCRIBE, CONSTRUCT, ASK), pagination like this doesn’t make so much sense, so these parameters are ignored.

For requests made through the website (i.e. HTML format), the page size is defaulted to 20. For requests to our sparql endpoint for data formats (i.e. non-HTML), there will be no defaults for these parameters (i.e. results are unlimited. For performance reasons we generally advise LIMITing your query if possible).

Parameter Substitution

You can parameterise your SPARQL by including %{tokens} in your queries, and providing values for the tokens in the request parameters.

Note that the following tokens are reserved and cannot be used as parameters for substitution:

  • controller
  • action
  • page
  • per_page
  • id
  • commit
  • utf8
  • query
Cross Origin Resource Sharing

Our servers are configured to allow access from all domains. This means that if you’re writing JavaScript to request data from our server in to a web page hosted on another domain, your browser should check this header and allow it.

If you need to support very old browsers, you can additionally pass a callback parameter and the results will be wrapped in that function. For example:

http://statistics.gov.scot/sparql.json?callback=myCallbackFunction&query=SELECT+%2A+WHERE+%7B%3Fs+%3Fp+%3Fo%7D+LIMIT+10

This help topic on the jQuery website has more details.

Examples

Using cURL

Here’s a couple of examples running a query using the widely available cURL command line program.

Request the results as XML, using a POST:

curl -X POST -H "Accept: application/sparql-results+xml" -d "query=SELECT%20*%20WHERE%20%7B%3Fs%20%3Fp%20%3Fo%7D%20LIMIT%2010" http://statistics.gov.scot/sparql

Request the results as JSON, using a GET:

curl -X GET -H "Accept: application/sparql-results+json" http://statistics.gov.scot/sparql?query=SELECT%20*%20WHERE%20%7B%3Fs%20%3Fp%20%3Fo%7D%20LIMIT%2010

Using JavaScript

This example HTML page uses jQuery to issue a POST to our SPARQL endpoint, requesting the results as JSON.

<!DOCTYPE html>
<html>
<head>
	<script src='http://code.jquery.com/jquery-1.9.1.min.js'></script>
</head>
<body>
<script type='text/javascript'>

	var query = 'SELECT * WHERE {?s ?p ?o} LIMIT 10';
	var url = 'http://statistics.gov.scot/sparql.json';
	$.ajax({
		method: 'POST',
		dataType: 'json',
		url: url,
		data: {query: query},
		success: function(data) {
			alert('success: ' + data.results.bindings.length + ' results');
			console.log(data);
		}
	});
</script>
</body>
</html>