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/S02002287 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002287 geosparql: asWKT POLYGON ((-4.485635432797292 55.846927661233984, -4.492473498900809 55.84593539174868, -4.492019186946515 55.84457841624699, -4.490669886121086 55.84346427115639, -4.490534837608586 55.84287025422889, -4.49235270117126 55.8427196428027, -4.49242494673977 55.84229988363777, -4.493313998075303 55.84225397917997, -4.493252230310825 55.841703311251415, -4.495003651992932 55.841645056065765, -4.496368884669508 55.84296484638705, -4.497830670895668 55.84303140605447, -4.4992494228872815 55.84408844501498, -4.500002646761883 55.8437692295165, -4.502095860701101 55.843528918166726, -4.504161545212534 55.843109360943814, -4.507514353986523 55.84456902065888, -4.506841276712351 55.84503214792914, -4.5071375937242975 55.84566426085848, -4.505816127013036 55.84623048051942, -4.505227310823988 55.84601777549714, -4.504032502651071 55.84681508507059, -4.504713975327718 55.84711750615199, -4.505536071950756 55.847816129113035, -4.506304250714495 55.84756979656149, -4.507457415588877 55.848313475572134, -4.5062195914191205 55.84877487275682, -4.505690467309562 55.84833652978703, -4.504708547025912 55.8483832184863, -4.501716592208015 55.849558640495914, -4.498376508601008 55.850291657555275, -4.496719748858176 55.85104050114404, -4.493561948405183 55.85195329178346, -4.494948378573545 55.85288141119664, -4.495930398665713 55.85308648310151, -4.496549502535329 55.852944282459966, -4.4970975877251 55.85351737843634, -4.494611338986982 55.85411610456201, -4.491086300837937 55.85421452742706, -4.489682424753099 55.85400026894276, -4.489040411096162 55.85271424213812, -4.486743347793498 55.85321299634692, -4.484691845943472 55.85191710596589, -4.481924700210404 55.85111911950696, -4.482685905591857 55.85052852217727, -4.483786843002685 55.850254627532685, -4.484363887228432 55.849532918254795, -4.48553090260357 55.84929362471989, -4.48582460906323 55.84838888572149, -4.486419107056127 55.848070699121074, -4.486083426578481 55.84718824616628, -4.485635432797292 55.846927661233984))
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>