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/S14000002 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S14000002 geosparql: asWKT POLYGON ((-2.0897919850295903 57.140496103414435, -2.071157264624 57.13978046986614, -2.0552232051151385 57.142467264548074, -2.058723154123215 57.13215764676731, -2.049693593651447 57.12851535359235, -2.0645530981579587 57.10672824366338, -2.0728367591457304 57.10411320779026, -2.0732020162562446 57.09766494047459, -2.0785020656910342 57.095581240502916, -2.0851948939490135 57.0858535559669, -2.1186778132432527 57.09043638681389, -2.118738733736717 57.103788792508055, -2.121166740385108 57.113832234711545, -2.1260664613682945 57.118425824668286, -2.1444890511077714 57.11718871079803, -2.160185665526797 57.112847857570074, -2.1750479256486344 57.11264291095189, -2.1788784303271846 57.107161411825324, -2.189815361913396 57.10749295612699, -2.2156069114392034 57.10040576603896, -2.2219410364639334 57.10166940317897, -2.231920208165898 57.09500382051957, -2.259371446535872 57.09523025090488, -2.261881264301386 57.084659969192316, -2.276287836393296 57.07823570826337, -2.2916632947606206 57.07947195719908, -2.306021425908383 57.0766430328881, -2.317044465230832 57.08005346827028, -2.2913528938241763 57.0893191071816, -2.280647985713287 57.09595872994804, -2.2809759236038762 57.10069030428477, -2.304033829267239 57.10505706060613, -2.3100004376265497 57.1027759967129, -2.33094430717695 57.10150970029967, -2.3444613330438293 57.106687977702705, -2.3608702470648564 57.10919031083801, -2.3516371778142693 57.1207589776611, -2.3511786927010547 57.1356461674488, -2.3328680940807924 57.13159085358308, -2.324163883145492 57.125722552920685, -2.309275201976448 57.12488163133753, -2.2837860852101852 57.13079981052528, -2.2780086688858026 57.13533124510958, -2.272843573422497 57.14444782654962, -2.265611740512913 57.14830540961783, -2.2339026056138636 57.14636088124648, -2.2086474805433864 57.14575060789975, -2.1844605427819905 57.14775313519839, -2.1786080599600908 57.14972081206197, -2.1652716529877365 57.14777901837752, -2.1591764128196873 57.14388109192752, -2.1302103086058937 57.14771886509607, -2.10647094489064 57.152089080819145, -2.0987711148139643 57.14124930110013, -2.0897919850295903 57.140496103414435))
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>