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/S02001255 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001255 geosparql: asWKT POLYGON ((-2.079731272909782 57.097749877375456, -2.080503513407972 57.09615039318743, -2.0817878148304962 57.09480209621853, -2.084143288692671 57.09284221902252, -2.084604730424387 57.092738062977666, -2.087533274837028 57.09267026320487, -2.09079562016499 57.09329282701219, -2.0961793083415285 57.0942421048704, -2.097132023821768 57.09358588122987, -2.0995635776217534 57.09338960158001, -2.1021783858114778 57.09353054806589, -2.101884386460518 57.09394752255487, -2.1028589922631054 57.094034794950744, -2.102972448352363 57.09354797071585, -2.1048714437139973 57.09368015090957, -2.104731536013603 57.09455615551374, -2.104116588187129 57.094890579447195, -2.1033030107800386 57.09594026339626, -2.103232213658589 57.09636186226574, -2.1024613287420637 57.09655954924275, -2.101128157265153 57.09613740522091, -2.1015448559811634 57.09564354564745, -2.1006942698535798 57.09528422716082, -2.098961479912261 57.09747637893865, -2.098155493343027 57.09824060339753, -2.0978733330122945 57.10040230710999, -2.0980113083756375 57.10080383261793, -2.0972841779459888 57.10134168744257, -2.0954951103811816 57.10154255374263, -2.095151927336126 57.10118640267108, -2.095292104835716 57.100279319154694, -2.0950839526884457 57.099185419022014, -2.094390328318018 57.09901526597423, -2.090875593307906 57.099341260959754, -2.090893443131314 57.099889215993656, -2.091553674022149 57.09989771735007, -2.091610544813227 57.10287107505551, -2.0891831742482405 57.10249554079845, -2.0891662568951217 57.10232488336757, -2.087003699190333 57.102227584206126, -2.08693972015935 57.10309898721728, -2.0854043533906306 57.10299224863778, -2.084659111984736 57.101923769350314, -2.083206414776802 57.101656051927414, -2.082366519384448 57.102715811869906, -2.082644674819397 57.10363461492746, -2.0820382810531024 57.10356941997351, -2.0803854298965314 57.1025733724541, -2.080552233814315 57.103372758850284, -2.079033188934149 57.10317610098603, -2.0792128015784286 57.102259713468904, -2.0800213954729556 57.10216038451225, -2.079655610091848 57.10092993645611, -2.0795536223234374 57.099555590157294, -2.078896032091703 57.09979839750018, -2.077805907448782 57.09985078758276, -2.077742074407254 57.09900639073312, -2.078077366494704 57.09869148636481, -2.079419866553115 57.09875618095933, -2.079731272909782 57.097749877375456))
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>