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/S01010680 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01010680 geosparql: asWKT POLYGON ((-6.204683897648452 57.41881265752412, -6.205761114422961 57.41788930690681, -6.203704884805447 57.41730956939242, -6.203161340179205 57.41688689688834, -6.203998070609465 57.41622013081103, -6.202636610751298 57.41543541992251, -6.202241149983001 57.41460330927124, -6.202475377672782 57.41388505259804, -6.200931381374615 57.41306432888735, -6.199686131240652 57.413249797734146, -6.199245759699032 57.41275191190466, -6.197569430644143 57.411862098934435, -6.197033739547773 57.41116118026538, -6.198686063441373 57.41016628671389, -6.199085204614333 57.40960709946065, -6.200165393172514 57.40923290219524, -6.200336584554206 57.40876405627624, -6.199852154711564 57.40639894265007, -6.198997771360259 57.40519545925534, -6.197741210327927 57.40474190482461, -6.196975523510294 57.40351457049219, -6.200463836736573 57.40347899422576, -6.2004210351973 57.40217216966775, -6.200825303691921 57.40074342407723, -6.202654431378253 57.40107149927076, -6.203386441143419 57.40380951759827, -6.201662453271349 57.40385257774712, -6.201680112514883 57.40511392969951, -6.202175265018862 57.40509738588654, -6.20392594798157 57.407183995539576, -6.204628024180828 57.40749500445958, -6.205442808117468 57.40838130654177, -6.204468636648917 57.40877713260735, -6.20688004955415 57.409286353569726, -6.2061286867360845 57.40956330045544, -6.208583941489469 57.4110638955486, -6.210093260647544 57.411292160467454, -6.210740380470863 57.41086358042484, -6.212915716892877 57.41134123144297, -6.215510776428166 57.41249861450253, -6.214513265626982 57.413263998788395, -6.214816646740998 57.41421584406349, -6.212454102196446 57.415009722853895, -6.214974177592597 57.41714524099499, -6.216365593954218 57.417628135019854, -6.214614847909813 57.41843008521121, -6.215634116445052 57.41956398559117, -6.215453486877255 57.42017250650535, -6.217744028384206 57.421460502436766, -6.216616871243102 57.42198572082183, -6.215681346572482 57.421698638575485, -6.213846813273882 57.42069647511321, -6.212251026941607 57.419209581688065, -6.211489771626534 57.41927627615114, -6.213200504230358 57.42294833161703, -6.212593795301255 57.4230820856343, -6.2104453113348095 57.42193329909182, -6.209829664492966 57.42054616578474, -6.208562065077293 57.420493221187925, -6.206670828023528 57.419260822368265, -6.20568626098802 57.41920119906617, -6.204683897648452 57.41881265752412))
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>