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/S01000012 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01000012 geosparql: asWKT POLYGON ((-2.083254366337826 57.10850032250991, -2.083629372528495 57.107697001987, -2.086834587898612 57.1083128897151, -2.086721876587409 57.10875847426254, -2.090550200154978 57.108019148688406, -2.089606283261404 57.106816097211045, -2.090412272258871 57.10562076835569, -2.09186593618399 57.10601496401593, -2.092706356703145 57.10542146090448, -2.092438623404344 57.10397538404695, -2.091298375649014 57.103464186472905, -2.086939713556688 57.10309898722188, -2.087003692587825 57.102227584210716, -2.091610538210606 57.10287107506035, -2.091570283695022 57.09994262059505, -2.090893436529222 57.09988921599845, -2.090875586705912 57.09934126096455, -2.091585011789224 57.099223963228134, -2.095083962591386 57.099185419014496, -2.095495108732906 57.10154255464219, -2.097284176300124 57.101341689240435, -2.098011301782893 57.100803836216315, -2.098155498299124 57.09824060519027, -2.099680114390619 57.09660341479779, -2.096735466639883 57.096274530007165, -2.095001250574742 57.097203703827105, -2.093502310895648 57.09546445673973, -2.09617930339071 57.094242104874205, -2.087533274845519 57.09267026679811, -2.084226933158001 57.092732010219734, -2.083166758172861 57.09366536083265, -2.080932241056739 57.09283834078855, -2.08065981478617 57.0921438347007, -2.083085133050726 57.09194461776897, -2.083034983489779 57.09165719213463, -2.080461149322969 57.09184752073396, -2.082489271423289 57.09113653411982, -2.082782962366568 57.089645146212014, -2.082402867371484 57.08938488763819, -2.0811497699583 57.08981689624594, -2.081065575258088 57.08904440464865, -2.085910214465948 57.08624742443405, -2.089295094940405 57.087305056917195, -2.09574909612104 57.088324354341324, -2.118636771514506 57.09042958691202, -2.11888010473228 57.1030906729551, -2.119430874600341 57.10496761196339, -2.122082841260652 57.10823489213365, -2.121143389390354 57.11376039279635, -2.12451119487831 57.113604375255385, -2.122996748967222 57.11498926482326, -2.123099313382608 57.11604018235971, -2.123084681658376 57.1166061296508, -2.12212573448464 57.11623876356718, -2.119650953256379 57.116887934547734, -2.117699025947543 57.11403984886037, -2.117597347126893 57.111889729031596, -2.118764327463855 57.11101133952005, -2.117850708837924 57.11096034317039, -2.117863704586073 57.11048470525942, -2.116526260248736 57.11044104258002, -2.116797785711009 57.10865589456887, -2.115902510228022 57.10832531280784, -2.116436931783801 57.10813214407785, -2.115016434298338 57.1078760659658, -2.114477989606731 57.1105166116033, -2.113151634182012 57.111853655140585, -2.113685970083108 57.11204924148272, -2.11222801022678 57.11402023221067, -2.110100992508324 57.11501924198286, -2.10657160065698 57.11646857087338, -2.102365288178395 57.11806208460142, -2.096303483134231 57.11751893178844, -2.092178306692647 57.11876170171857, -2.089951327557384 57.11973349309097, -2.089223605041223 57.11927587868373, -2.090312422255448 57.11882594442585, -2.089302337208098 57.117685821794204, -2.091560302571263 57.115932492844905, -2.089197022873406 57.115098776320934, -2.090180584628371 57.112160609669644, -2.087633070356786 57.11010529906618, -2.091642028756466 57.10892564328, -2.090782204877555 57.108378303252756, -2.086689542666649 57.10899927795178, -2.083254366337826 57.10850032250991))
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>