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/S02002271 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002271 geosparql: asWKT POLYGON ((-4.40798173545129 55.830470027550575, -4.408390168928616 55.83010251350612, -4.4075528911081285 55.82973243378869, -4.407687967396982 55.829334323079195, -4.405710916942437 55.828516930573855, -4.405093669047723 55.82800248043158, -4.406172552768783 55.82750957110322, -4.409787869952382 55.82585083173397, -4.410034303775465 55.82596284341632, -4.415112471818066 55.824685779737464, -4.4138014892306865 55.82311168432522, -4.4160601154461725 55.822716789528435, -4.416995021681277 55.82286019438526, -4.410752890568617 55.81951343947469, -4.4116417534095715 55.81917244166737, -4.413404222754109 55.818760362209, -4.413999370073586 55.81831725825554, -4.416118707202276 55.81876995593879, -4.417954291162535 55.81979443629752, -4.419710130660227 55.82133276975327, -4.4203626983544435 55.82309952677745, -4.420791280786491 55.82460105439677, -4.42169836448308 55.82487787594636, -4.421635776757027 55.82602911882676, -4.42384325691006 55.82719211707433, -4.424603116520156 55.82733057554162, -4.425364169696424 55.82778252176805, -4.425059522683935 55.828248082508985, -4.423512429396951 55.8277411572817, -4.421420576266253 55.82727604144378, -4.420456339619531 55.82769049559477, -4.421264963780706 55.82913960834917, -4.422191908780238 55.829408953648965, -4.423085095828637 55.831188932171166, -4.423724756788983 55.83120328794923, -4.423309267127959 55.83319206716503, -4.424027510961686 55.833236591168536, -4.425682195485661 55.83391498158479, -4.425958712724359 55.83322648206303, -4.427126452329106 55.83376963801837, -4.428807421116388 55.83434762168843, -4.427700320550283 55.83627499176544, -4.425249146707485 55.835936811729574, -4.425094923464711 55.83628139333775, -4.4245692897248965 55.83707370532542, -4.424120397539981 55.837046604279074, -4.423668608687642 55.83619030940861, -4.422316842821814 55.83660611301476, -4.422026050630799 55.83624301454023, -4.420982400492075 55.83666803112222, -4.421725825067725 55.83737763494375, -4.422034108758417 55.83811633464232, -4.41931904194693 55.83680275521474, -4.417804966438869 55.836424954041384, -4.418469151539976 55.836061365150755, -4.417391246581731 55.83567810726734, -4.41679118909402 55.8359846781424, -4.415499056249691 55.835332670822446, -4.414404806799458 55.83542220934855, -4.414062945182403 55.835060419200396, -4.410733063992662 55.834415709473404, -4.410682267636172 55.83411110927375, -4.411522883236352 55.83350143565328, -4.4122705328202985 55.83371146633054, -4.413950912122259 55.83376835724461, -4.41274034295837 55.83304613427754, -4.412348840017482 55.83239770212396, -4.411571724064708 55.83248484951296, -4.409984230442718 55.831347554169014, -4.410423442067302 55.83096145430007, -4.40798173545129 55.830470027550575))
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>