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/S22000064 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S22000064 geosparql: asWKT POLYGON ((-4.461846655905886 55.17022042159163, -4.478620657505107 55.152926080966644, -4.564966617398485 55.158015397297625, -4.584000812295121 55.14197678682742, -4.623170586923084 55.14078451059973, -4.658316495353163 55.11993598367971, -4.662032116835455 55.08974963228486, -4.6252234037401845 55.06844921353861, -4.634040996755463 55.051604764781665, -4.6458146483668665 55.048770571966905, -4.646739848084846 55.05638235574096, -4.715171523648032 55.03751124981977, -4.741456646267477 55.0473090082249, -4.761073662294209 55.03797466670959, -4.775992204358654 55.0418377207697, -4.789167208797721 55.031054422172566, -4.802353095779582 55.04439310533808, -4.830604637791081 55.03464149857628, -4.841055697034801 55.04474503647898, -4.884551036942454 55.03890302336521, -4.894729821017761 55.06086162327735, -4.923046183672818 55.0662077236309, -4.933399391174445 55.06437179517978, -4.9307812735295995 55.05138155664596, -4.9605591141366 55.021501251289166, -4.978059948814798 55.022121167634474, -4.973231009182249 55.011536678507795, -5.039750877470527 54.99779236891911, -5.06026889287596 55.02554572996412, -5.057791159563606 55.0337245605, -5.052478244794047 55.05125145377854, -5.039554609571662 55.06332943530748, -5.0251081213805495 55.0768204848111, -5.0111817037253745 55.089815771549915, -5.010780947200847 55.090927112380726, -5.000909335327961 55.118283111145495, -4.9930772009402045 55.13996142754385, -4.991817030749974 55.143447264175876, -4.941532923479097 55.1640248754955, -4.91083201761478 55.1983589974036, -4.865156177747405 55.22451517481387, -4.859888734080874 55.227528285972944, -4.864945906656112 55.24528731984457, -4.864944940464037 55.245382828688435, -4.864924739396266 55.24540586892156, -4.859467246349709 55.25162920146029, -4.8526952041023055 55.26077690719476, -4.836113992949193 55.283154954838444, -4.8361229967740655 55.28318802259197, -4.837539238249844 55.289562354812645, -4.837561266705691 55.29012645496679, -4.837837577177009 55.290904854055036, -4.845381519543788 55.32482004490119, -4.845608363666021 55.32583889522538, -4.832669459259955 55.33207928307987, -4.829085351056281 55.33380730118464, -4.774597738733747 55.36004644811113, -4.773920261783666 55.365577013255404, -4.7232791882119365 55.36192453147575, -4.740606507965137 55.35713217589259, -4.727088491076045 55.34778951096025, -4.696787172357779 55.346664669715636, -4.706446872266678 55.336113496096004, -4.690453648643502 55.33584663608411, -4.692028123562814 55.3437133606441, -4.65833739168614 55.333125072771374, -4.675623494960295 55.30720971206588, -4.713004640279797 55.28646465407767, -4.670669484597103 55.280629778602275, -4.670424769058291 55.27205952817458, -4.642703877470771 55.26407861062366, -4.627406597302964 55.26746530482114, -4.567359292241294 55.23638401004494, -4.536073958446808 55.23838670166445, -4.4959773537609955 55.198468102134726, -4.469586015413712 55.195827956884735, -4.461846655905886 55.17022042159163))
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>