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/S02000113 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000113 geosparql: asWKT POLYGON ((-2.599662350611541 56.576608640873474, -2.598085191750169 56.57362455941963, -2.596444471597131 56.57391399744168, -2.595945055774517 56.57320604241349, -2.593212805021459 56.57324362736729, -2.593472129848491 56.567919674303916, -2.59440555785249 56.5679212127815, -2.594842620988525 56.565096298923216, -2.598059338148099 56.56683275397899, -2.598537999632831 56.566426098021545, -2.600904850911139 56.566693252789484, -2.604772531837647 56.56942361139091, -2.606192275032143 56.56966825083442, -2.607647873267791 56.569104132206455, -2.606156876756911 56.56847354477943, -2.605481670540992 56.56657196194333, -2.604558236252325 56.56670908820759, -2.604852592585718 56.5662717673255, -2.602686118397303 56.56571179226971, -2.604339482973312 56.564891590410205, -2.603898999392563 56.56447580866381, -2.605777383085506 56.5639586263834, -2.603301281484373 56.56239266887398, -2.604258119557001 56.56185002424874, -2.603290705756883 56.56117171206624, -2.605429523436975 56.56114675865703, -2.606150426613099 56.560703435897196, -2.603735329504335 56.55957770622168, -2.606737343991775 56.5589925420501, -2.608482517116066 56.5592625123818, -2.610194790115099 56.55849945823447, -2.611251583687948 56.55946454652866, -2.61184745426042 56.55807806540056, -2.61046944650679 56.55635990156054, -2.609089742997189 56.55554013448204, -2.608394588752108 56.555813063224214, -2.60632372330494 56.55449354409533, -2.609131649524179 56.55308728429669, -2.607652061407891 56.552115258034554, -2.61048254353722 56.55111313683425, -2.610871260659649 56.55147319759469, -2.6115769809492 56.55123983488496, -2.608941933969042 56.54937778647257, -2.614321413617792 56.546099077712576, -2.617872326688515 56.5484263193191, -2.618061766571623 56.54907222654629, -2.626087266048353 56.552437016944765, -2.623782091848812 56.55371879321325, -2.626096068502547 56.55616685177787, -2.615148054474335 56.558573320845376, -2.614600874133769 56.559035064168405, -2.619172655465591 56.56218225195287, -2.607966233479769 56.56663196007696, -2.608069098502574 56.566985138208544, -2.608882150404611 56.56691645680712, -2.608932701294851 56.567544453759815, -2.611952091738056 56.56858889424217, -2.6154523718845 56.570682862479494, -2.61976250607437 56.56952945578154, -2.616786152687739 56.571637542959245, -2.618414965366734 56.572716528346135, -2.615728010765565 56.573788333914145, -2.6138934383607 56.57288314425433, -2.608786757930537 56.57516278984375, -2.605910425267613 56.573414805239395, -2.603765411467731 56.573957966370635, -2.605193115880788 56.575840144567145, -2.599662350611541 56.576608640873474))
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>