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/S02002084 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002084 geosparql: asWKT POLYGON ((-3.281001646041335 57.64529707683079, -3.2818290468387974 57.64531174304257, -3.2827634199629103 57.64479695057077, -3.281743045014811 57.64444439230802, -3.2801431507874015 57.64310995866943, -3.2800050202831983 57.64274725678623, -3.282894315038655 57.64193121203391, -3.2838251772012357 57.64167045716658, -3.2862934839431883 57.642221072392495, -3.289003973363941 57.64259772550898, -3.2915564605999306 57.642769310634925, -3.3074038796444514 57.642779101660004, -3.309102652121566 57.6440943320116, -3.3093991875125788 57.64466093735111, -3.310493437588885 57.64573658020766, -3.3098250654501045 57.646451236268405, -3.31061158101645 57.647154716448064, -3.3114604377859993 57.646903788812565, -3.3124660196634417 57.647934995891944, -3.312884740211871 57.64793064927058, -3.3133601339446965 57.64903065820586, -3.314205946577715 57.6496958516682, -3.3152279817539023 57.649559001686164, -3.3157217513425987 57.649931388349295, -3.316703478578693 57.649741504176156, -3.3171111726646725 57.65035710625149, -3.3181771637036546 57.650642448008064, -3.3187657551035588 57.65162447450116, -3.315623639880671 57.652312982819595, -3.31367291964022 57.65307774299503, -3.3134923675507597 57.65454502657236, -3.3132608847307283 57.65509541114193, -3.31251385053652 57.65529182422967, -3.311192596654717 57.65490127272944, -3.3102028474456264 57.654408466471864, -3.307800929727338 57.65405503270726, -3.305032011761349 57.654233971222645, -3.3036818215838695 57.65421529657568, -3.3011299738159945 57.654713458783355, -3.3012110486431396 57.65488752890615, -3.299903154223226 57.655477974151324, -3.299022153836848 57.65507984187077, -3.297256477171953 57.65507562433936, -3.293854148074346 57.65476544912971, -3.2926272366970375 57.654505560096766, -3.2931496656025963 57.65364972922866, -3.2918924906803495 57.65316849881471, -3.291090940248583 57.65171116477544, -3.2895296832419003 57.65127918041132, -3.287841493776471 57.651871312023594, -3.287283729848622 57.65248742083122, -3.286003086605549 57.65272430377648, -3.2850163971499597 57.652537851917636, -3.284829521062434 57.65199178147674, -3.2855184469690673 57.65157155073474, -3.2852581358994883 57.65037044096911, -3.284962272346362 57.65005903381323, -3.283066336109684 57.649988452043985, -3.282753080928059 57.64918313757644, -3.2833931720074903 57.647856095999835, -3.281658190912408 57.64796863661954, -3.281001646041335 57.64529707683079))
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>