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/S02001279 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001279 geosparql: asWKT POLYGON ((-2.1290978767318314 57.18371553788242, -2.1292583680711887 57.18223879725965, -2.130332228472683 57.181851417200036, -2.1303457929900493 57.18110590648609, -2.1293046615004205 57.181286554830834, -2.1284121001799443 57.180778084533735, -2.1281255450454544 57.179940320665274, -2.1273719690773345 57.180102144729716, -2.1246042611879656 57.178875204163965, -2.124285534768119 57.178336650225425, -2.1267686414177063 57.17839205747184, -2.1270856101816 57.17914954888495, -2.1276881992954126 57.17955630497126, -2.129613851415754 57.179384908783184, -2.1299251828969967 57.17899525531774, -2.1295706228177926 57.17845808139605, -2.128683513018187 57.17869861650168, -2.1274911949279973 57.17803540089491, -2.1287162618596827 57.177886288505775, -2.128578343213352 57.17696649267496, -2.124628336115693 57.177617402316066, -2.1232183496750974 57.17804070938318, -2.1226819083503896 57.17746867912079, -2.1215408538470415 57.17770333074273, -2.1181113714941473 57.17759603988273, -2.1165786605792962 57.1773253904766, -2.1153070156359637 57.17727685579709, -2.116355463229745 57.17681804204335, -2.1209156398904687 57.17548294917493, -2.124155043043857 57.17481504010406, -2.1273265813062325 57.173688984338526, -2.1285499153369476 57.17354400731574, -2.1303531434732697 57.17371282063764, -2.1322245816612937 57.17444746382379, -2.1333043406271672 57.175766817478106, -2.134848206012488 57.17658558966306, -2.1364542249890746 57.17724617474722, -2.1373291405917954 57.17733449169898, -2.140143118859817 57.177915269470844, -2.142840172257947 57.17818171336156, -2.1469261757700266 57.1783296998649, -2.1483987163065748 57.178444743461476, -2.1486625644119712 57.17824680526243, -2.1491383882247237 57.17688889401324, -2.149681494213692 57.17640188105892, -2.1504901400403185 57.17618752190726, -2.152143781904925 57.17612264435049, -2.152100221875789 57.17758692283651, -2.1517844772332055 57.17900926600311, -2.149735434913534 57.18044568316852, -2.1491969984635335 57.18105084348153, -2.1489752747177397 57.181965385146206, -2.1494222652712565 57.182054682730524, -2.1493092307530404 57.18273752322184, -2.147968432249229 57.183913726453476, -2.1473239627150114 57.184109047787636, -2.146746932658694 57.18373387737223, -2.1450316300679972 57.1844762592861, -2.143560953897554 57.18490015201057, -2.1408234287626 57.18532152286511, -2.138954718441535 57.18532360882458, -2.139077578181471 57.184851309918564, -2.135631768497332 57.184289752850276, -2.1355824339765364 57.18495597396478, -2.129016732650428 57.18409852036102, -2.1290978767318314 57.18371553788242))
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>