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/S13002869 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S13002869 geosparql: asWKT POLYGON ((-2.8951043574081794 56.60871211600351, -2.903564530506855 56.60858448447386, -2.910326065529846 56.60983159812044, -2.9186598728262454 56.609519651890324, -2.9213790734410505 56.612671057761595, -2.921170662532793 56.61742345395284, -2.9281992361042564 56.62528450624702, -2.9332241344723284 56.624559641325554, -2.9358133499829213 56.626975751816566, -2.935804817273186 56.629920903090564, -2.930279860865074 56.631810396925815, -2.9334176413629116 56.63576595081081, -2.9556834245742305 56.63295920690248, -2.957488303342165 56.636769908340064, -2.9545089696286766 56.63745133293805, -2.9564699759466793 56.64149356436401, -2.9589394116082457 56.644419649217085, -2.957019987342905 56.6458943016938, -2.9602050079219544 56.647054810958714, -2.958121522109847 56.650101277521415, -2.9503250771072786 56.65402872365736, -2.9445661384647637 56.65574089586646, -2.941016797562261 56.655680656784746, -2.934229027605904 56.65790057689802, -2.937152096299336 56.661115580962964, -2.9405950098267035 56.66343273923672, -2.942917305200493 56.66940492128064, -2.9484780619228914 56.66803484096578, -2.951321733044607 56.670998545429796, -2.944295577793443 56.671730323133325, -2.939346753353928 56.67155489372762, -2.9340762102835116 56.673681509460096, -2.925439520129888 56.6743245946662, -2.9189819702518043 56.67238156607808, -2.9108056342578768 56.67492576856997, -2.908931431352839 56.672592759558576, -2.9043436655787476 56.67223178489884, -2.903271863713798 56.674491872839226, -2.8993444456410273 56.67835919944836, -2.8959441073014482 56.67949688493521, -2.8847260986221572 56.68068119393566, -2.8719422350582504 56.68137151066449, -2.8677440314229656 56.68243137604398, -2.862591351025597 56.68537978926147, -2.85362015225761 56.68807569960954, -2.848659388120526 56.69303110034198, -2.8408763475814425 56.69619237988199, -2.837050420510115 56.697219854050815, -2.834777194208412 56.695734755859355, -2.842739776026422 56.691456874951406, -2.838015835132767 56.68591951249663, -2.8469452564775515 56.68155840361939, -2.8459886291336547 56.67994868531186, -2.835706629050704 56.68123816191575, -2.8351874733951963 56.679538304484446, -2.8173137474528005 56.683009688763654, -2.810659080246019 56.68514649758722, -2.8073651870759693 56.684247027623634, -2.802263879487601 56.6856977191321, -2.7976428419492736 56.68247063883474, -2.7941927344597706 56.68133560931602, -2.7968108719831974 56.677004729935135, -2.7998177205549295 56.67010546628912, -2.795284172017218 56.670699621950355, -2.7837636128001746 56.6739862437866, -2.780272148982687 56.67276478042309, -2.7754650339995575 56.67270679509819, -2.7720189012725314 56.66929541126523, -2.766859103084896 56.65778909249934, -2.761901513741166 56.6568034630384, -2.7617408798443024 56.64924708068681, -2.7726283741844755 56.650203230205456, -2.7694209832721044 56.64694566726971, -2.7597418615326172 56.64537274588462, -2.7591660534784883 56.64024999014814, -2.767030272198518 56.63851189965228, -2.7783685834133696 56.636959099192424, -2.77988197373952 56.63792708122542, -2.7921125641168105 56.636978353276675, -2.7934760644448957 56.63793544957948, -2.8057825585235276 56.63681693909495, -2.812489112998234 56.633449175330384, -2.8202848919638255 56.63158869575133, -2.82256156538291 56.63485732024041, -2.830027650915251 56.63016023795506, -2.8371426748197623 56.63413658128564, -2.8389346826958746 56.63583963072727, -2.8446733534520554 56.63103375351266, -2.8389635813593412 56.6249524309479, -2.8349208230964362 56.61731070822737, -2.841676237009139 56.61773689907036, -2.854802177960209 56.617601595801936, -2.861141790375768 56.6166414808716, -2.8695011593733617 56.617500627396154, -2.8830209252630072 56.61665965457291, -2.8807729665679016 56.61109198151956, -2.8920762726508373 56.610457986124324, -2.8951043574081794 56.60871211600351))
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>