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/S02002447 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002447 geosparql: asWKT POLYGON ((-3.9373153205613627 56.11578973082037, -3.938319723490875 56.11401262251436, -3.937142904573308 56.11392661519023, -3.9356632300481365 56.11322611579657, -3.934635482408255 56.11211902588475, -3.932752249590143 56.11204200445095, -3.9330050489858834 56.11131014465308, -3.9329696461588957 56.10996273178065, -3.931934212637632 56.10823556783211, -3.931712006035565 56.10733141348903, -3.932003118344932 56.106401253735505, -3.9325910625077145 56.1056192163901, -3.9336352029230013 56.104955841776935, -3.935152164117855 56.10378180540084, -3.9358238006851956 56.102746818524494, -3.936320999612703 56.103249245318935, -3.9380684457642756 56.10284470688215, -3.939124740792421 56.10283879509215, -3.940997350609335 56.103266022641414, -3.9406241502888704 56.103807142179086, -3.9428510384114768 56.103553995870506, -3.9456507280039688 56.10380339209474, -3.949214916467403 56.1039559677801, -3.9494712621708907 56.10361941982574, -3.950730981316457 56.10436337217152, -3.950601652324575 56.10498548358809, -3.95173336863569 56.104787866854856, -3.9517790020616643 56.10441869916914, -3.9533604369667317 56.10452848372259, -3.9533716814270674 56.105067495577906, -3.954186429626361 56.10527926361602, -3.953835072061997 56.105644284780205, -3.9549633236003996 56.10601284270148, -3.9544840236303123 56.10671977891168, -3.9535951318809595 56.10630409612368, -3.9532166952379892 56.107988895773794, -3.952683916985258 56.10786500662388, -3.951888624614545 56.108552639034755, -3.9520798983549867 56.108928853629045, -3.9515608142136958 56.10999179502219, -3.95007815582015 56.110910249370285, -3.951172548422438 56.1112274625631, -3.953311116120774 56.111277688352004, -3.9557878513553346 56.11165968486966, -3.956322021369293 56.11289317316148, -3.9544315228759466 56.11236380370319, -3.953542913581778 56.11227075712985, -3.949641360307066 56.11143497570783, -3.949121913009779 56.11155475346086, -3.9478939124078867 56.11235053837277, -3.9451507927266403 56.11359965306764, -3.9455933715362423 56.114401461902965, -3.947379896246766 56.11626887644877, -3.947893199922685 56.116445316809425, -3.948521234836155 56.11729757836274, -3.9485983816043877 56.117838694367, -3.949709408128657 56.11790458627346, -3.9500619152565877 56.118476121824294, -3.949383201757314 56.11859113589739, -3.9483752925829236 56.1183497795632, -3.9479191405435734 56.119046741815446, -3.9464335775245316 56.12032191086853, -3.9448094668827154 56.11926650223211, -3.9435559378485205 56.11897171331754, -3.9410684350126126 56.118130169639414, -3.9410940075980903 56.117680444902774, -3.938371421170509 56.116959367226606, -3.9385621671441213 56.11636275462428, -3.9373153205613627 56.11578973082037))
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>