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/S16000075 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S16000075 geosparql: asWKT POLYGON ((-2.267055803860411 57.146867767545295, -2.253547327569634 57.160167330512735, -2.262170749487316 57.170924699024695, -2.27331293199484 57.176695000355096, -2.2730179974555558 57.18087540643, -2.2856705072756354 57.187738092173326, -2.29498843045019 57.19770922093325, -2.2895880452215334 57.20186114225702, -2.2811196788787678 57.20086624434841, -2.2783726492505383 57.2042023923842, -2.2816793686009267 57.20698145069651, -2.2778988217075247 57.21075374317112, -2.2817329781343956 57.21293883344017, -2.2789433519265323 57.21972992406703, -2.2790905993056643 57.228266967164, -2.27393402415794 57.230188988594456, -2.272875179944157 57.234599213354926, -2.26181263480248 57.22851442617787, -2.252534899433887 57.23321354714671, -2.237507162313898 57.23345670538426, -2.2375367444179792 57.230582938259786, -2.2177064321134567 57.23024523276174, -2.2111082609121544 57.22920373990661, -2.2044389294030733 57.23080475316889, -2.201798496577298 57.222909465112515, -2.19242585287524 57.21916034453145, -2.171754842669999 57.21254479759541, -2.154641081780871 57.21363959228084, -2.143064125279451 57.2112584540093, -2.131444900447113 57.217936571024744, -2.128222613220397 57.227448290807494, -2.1160351373534376 57.225026673276844, -2.094656572549121 57.222506981736494, -2.0870013084425563 57.21915379642988, -2.0759484766957863 57.218291511473794, -2.060976026564036 57.211925371533155, -2.0723482392947297 57.189385635820265, -2.0749297542684366 57.17984511324159, -2.0827450407878634 57.17603320723554, -2.0962294133007613 57.17666315460808, -2.0983585586269733 57.17809967368038, -2.107080474348671 57.17764067563512, -2.105136926612691 57.17411921267069, -2.117324691892264 57.17694071761598, -2.1289975906975895 57.17339895349675, -2.1166772825310898 57.168844944931294, -2.1164940839341577 57.16583038982805, -2.127289228772184 57.16094126885828, -2.144827364523305 57.1606509203217, -2.150217102023551 57.156599516729806, -2.148953790788865 57.148713950977424, -2.1632677550834645 57.148318844172145, -2.1726468663014518 57.145517031015814, -2.180166946723616 57.14877802348657, -2.1976639616132663 57.1453317947975, -2.2216229366246925 57.14676046920657, -2.2338901363250137 57.14620904800481, -2.2461373737505768 57.14769027404215, -2.267055803860411 57.146867767545295))
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>