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/S02002089 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002089 geosparql: asWKT POLYGON ((-3.3065431457316627 57.66233711863581, -3.3073568548140657 57.661960136047455, -3.3089045319886132 57.66254258597522, -3.308858765393201 57.66334635261933, -3.3113717492177686 57.66265198592993, -3.30990996274349 57.6616250733949, -3.3107151551485807 57.661610425602305, -3.3112931116708335 57.65959975373468, -3.3118176059709445 57.65932378333214, -3.3131640746189888 57.659129722764376, -3.313128782798025 57.66011952579163, -3.3143258065267465 57.65905496122418, -3.316654918462335 57.658114440548076, -3.317549334112069 57.65735951297176, -3.317724806890646 57.656413980982734, -3.3174328206988704 57.65532506766272, -3.3179435112675733 57.65528924844535, -3.318179082675689 57.65634483676355, -3.319920583184267 57.65627902605979, -3.3194774140389325 57.65553376977958, -3.320879382256529 57.65537540219781, -3.3204574613250717 57.65430749866715, -3.31967423892675 57.65403150529528, -3.3195400230631265 57.653565774199066, -3.322835463285838 57.653405580221836, -3.324079179003825 57.65442564489972, -3.327651786198826 57.65680470922809, -3.3297456580322673 57.65734574919001, -3.331045632638649 57.657116286301225, -3.3309136463470286 57.65663566667416, -3.331523295985396 57.65635076683982, -3.3331515399644145 57.65596529766259, -3.3351052254103215 57.65531585576689, -3.3357720073247705 57.655218981677095, -3.3364612592980034 57.65573273259391, -3.3314327064564746 57.65753751795915, -3.330300188170663 57.657482410875396, -3.330541687169913 57.65795595899094, -3.3316866782468044 57.657740481060785, -3.3335124868285333 57.65793298899063, -3.335566005081227 57.65833771368318, -3.334731922911949 57.65860628936294, -3.33316513701174 57.65803155961463, -3.3293534203725477 57.65912094088272, -3.3300208093807626 57.65981489193201, -3.3279572317889086 57.66048048957198, -3.328282811235769 57.660775693745705, -3.3203162323023836 57.66388987124678, -3.3205524515065883 57.664057324156225, -3.3156426695659826 57.66462208009647, -3.3138341452803703 57.663215339928676, -3.312143162960704 57.66378755659964, -3.3118411127242533 57.664973786727444, -3.3119670580378324 57.666140305802514, -3.3091264190495995 57.6663943186094, -3.3065431457316627 57.66233711863581))
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>