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/S22000049 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S22000049 geosparql: asWKT POLYGON ((-2.651724704405843 56.5209033666257, -2.6614911211797954 56.53660508552943, -2.6817587061320705 56.52816017933254, -2.6999811131462272 56.535585878202276, -2.718220003675111 56.53055535691414, -2.737649138607382 56.54300470574695, -2.764512565005398 56.542273027237606, -2.763600599922472 56.553455430467, -2.7914668745208013 56.55744927748305, -2.806710000927336 56.568877989776304, -2.7976638455269107 56.57747130685529, -2.7799978858033563 56.57722401812082, -2.7906135211738756 56.583598600958275, -2.782962990209052 56.59387992875196, -2.7231355810282647 56.60337377385899, -2.6898821200611054 56.636740104801405, -2.660517848909056 56.64138807176516, -2.658299599833371 56.65537883953146, -2.6911950731211163 56.639128215248455, -2.735710911870504 56.64522773691069, -2.716909897174151 56.67034440684221, -2.753381038151934 56.67355348173872, -2.7057422452878357 56.6811904938569, -2.6357460220914573 56.68080380991538, -2.622121476808809 56.66327308729549, -2.5921004512474433 56.66518034503793, -2.5947922066770595 56.67137537383834, -2.5771522635109276 56.6713416264022, -2.560753469452873 56.683445945949195, -2.5669921520428907 56.70912384976737, -2.5458944621943074 56.711766147484866, -2.5566995909700636 56.71599453317766, -2.5356706001641514 56.712205727359866, -2.5388857119259893 56.71942290687015, -2.5288308358303864 56.7095110332831, -2.5159271359155286 56.72348368787018, -2.499410029521554 56.72258017282972, -2.487451888654031 56.727936322524094, -2.493632226100328 56.74323357860128, -2.4702321172906405 56.75268956349118, -2.4597824369141765 56.747130824282976, -2.444944679484666 56.751697950621306, -2.42456006922487 56.75474066966008, -2.452239259871133 56.70463604670491, -2.465518451987705 56.70221018091868, -2.4484257899757895 56.70226719247481, -2.4372574384509984 56.702303180060525, -2.448262100138874 56.683044398377476, -2.468265580864144 56.67082020341816, -2.483575341899319 56.67165014399996, -2.483636635854232 56.671610945169625, -2.4871057390756413 56.66909169813903, -2.511397626635317 56.65147548969871, -2.5113736047587247 56.65141971916907, -2.511419306295902 56.651386419818664, -2.502336739016656 56.63042207099186, -2.483058347721993 56.6287478371306, -2.4828661399709215 56.628731118889306, -2.4802107274924134 56.621388012219604, -2.5177752816561187 56.5929005552427, -2.520928114706384 56.58120835230072, -2.5375216790806254 56.575981844069645, -2.5375837425093977 56.567089721987756, -2.5775106460223722 56.55734277759599, -2.6051632838585674 56.55075728711735, -2.636006973853041 56.52718087003762, -2.651724704405843 56.5209033666257))
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>