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/S02000499 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000499 geosparql: asWKT POLYGON ((-3.467075961939515 56.07182638570281, -3.466897953188588 56.072214885103214, -3.464187818204706 56.072768232126116, -3.461530448386802 56.07281053414918, -3.46165214002845 56.07368787446642, -3.462847642873266 56.07386241042798, -3.462820491207002 56.0748421677351, -3.459437184717242 56.07512135633473, -3.459277278125314 56.07461454353672, -3.456470297308629 56.074737562326, -3.456618298681041 56.07567930907874, -3.455027388403066 56.075671115944004, -3.45568737893232 56.07613059364554, -3.457792238358412 56.07613269972053, -3.458435727695572 56.07672958688746, -3.457846958310331 56.0771564177706, -3.457569538385163 56.07669615028157, -3.45426848853544 56.076812245320774, -3.454367822097363 56.077902378040086, -3.44983457539464 56.07799154358481, -3.449637989264633 56.07676788967948, -3.451403680573552 56.07501290227168, -3.450633690700293 56.07461760449442, -3.451990793178555 56.073963662935995, -3.450002191978728 56.0740589331707, -3.449698965141241 56.07368509448279, -3.452557111929783 56.072788864173624, -3.451915993449554 56.07154740390202, -3.453236458465523 56.07077706854061, -3.453480194616048 56.068716479324216, -3.445251012894549 56.06890850149176, -3.445338368448665 56.06833395202143, -3.446102283248334 56.06816517547319, -3.445664179924059 56.067694297198265, -3.446875856437734 56.06732894674699, -3.448091618564167 56.06809691716416, -3.448066945732021 56.06779118932728, -3.450941812120031 56.06727785748958, -3.451052629377401 56.06682614986474, -3.454758715650755 56.06641006306178, -3.454779356487614 56.066957951865156, -3.456601475626945 56.06501351773821, -3.456956550758048 56.0633559658835, -3.458497332119376 56.06119917062998, -3.460904952142552 56.05988573882577, -3.470070824292931 56.05807857038475, -3.4704902516658 56.05854981652529, -3.466987335064927 56.05976864335284, -3.466615980781222 56.060621579040635, -3.469243362933694 56.06545669629403, -3.46204464183627 56.066234103269004, -3.46314345044305 56.06725442581266, -3.462359414850586 56.06733561035537, -3.462390708952026 56.068161923827375, -3.463621219013459 56.068416906721595, -3.464552023870522 56.06966385243708, -3.464242751977941 56.07040434635552, -3.465732652367102 56.07071913132453, -3.465588435819403 56.07115215678364, -3.467075961939515 56.07182638570281))
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>