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/S02001355 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001355 geosparql: asWKT POLYGON ((-2.575754558043005 56.57359523296269, -2.576443565296794 56.57181320400005, -2.5771281563203754 56.571881899634405, -2.5780486326600274 56.57025768656375, -2.579128724425366 56.56967150949058, -2.5782592891866623 56.56921737189824, -2.578977353562497 56.56873722909261, -2.5796718791825537 56.56794403556838, -2.580263784418336 56.568044958467596, -2.5807776039488486 56.567250391969274, -2.5804319910608005 56.56667943614867, -2.5796078419720505 56.566531344051434, -2.580959537636009 56.56541361003766, -2.5810312467484744 56.56486700969729, -2.581028948214715 56.56450490099464, -2.581704563286294 56.56403196569757, -2.5826287654123634 56.5640454252517, -2.5831996859698894 56.563489429887376, -2.585240702231502 56.56289587375491, -2.589040223662174 56.56303427428901, -2.5893210964385776 56.56475425798249, -2.5904250839222356 56.56472237202701, -2.590463340850274 56.56510047225222, -2.5915175582267693 56.56539877885158, -2.593040785587527 56.565374441866325, -2.5932694682463504 56.56646939441573, -2.592346358039108 56.5664757195629, -2.5923244847721283 56.56743519323627, -2.5934640679457663 56.56738954498942, -2.593477887964841 56.568236449100915, -2.5935868127830237 56.570106405115666, -2.593212813144533 56.57324362643013, -2.5946650319700995 56.57347925670727, -2.5956840094100895 56.573172577508274, -2.596444486201788 56.5739139946764, -2.5980852031706516 56.573624561161495, -2.5987764552005936 56.57513953473464, -2.5996623604480846 56.57660864531804, -2.598673234172612 56.57684699731191, -2.5946112450874046 56.576273546591985, -2.5916455832839635 56.577105242300675, -2.591907313629219 56.57757140813996, -2.590206540381266 56.57800999957413, -2.589750793637779 56.57753388910018, -2.5886168769068383 56.577801677348546, -2.5879934313252484 56.57827535430398, -2.5869651427873395 56.577316731473715, -2.585544007771011 56.576538625302554, -2.58453233176546 56.57655752716913, -2.583305891544953 56.57743524962783, -2.5817731118682183 56.57694595740465, -2.583244739581584 56.5754050136642, -2.5801795265209075 56.57452696551442, -2.575754558043005 56.57359523296269))
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>