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/S02000045 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000045 geosparql: asWKT POLYGON ((-2.172506074618163 57.18020149818385, -2.178535925979229 57.18438445639727, -2.177723443711671 57.18518624519791, -2.175908724352185 57.18553061388493, -2.176409439669559 57.186099466592665, -2.180334504618083 57.185918309134436, -2.181544208065333 57.18794068878717, -2.180810746281639 57.18838534921285, -2.180087241696335 57.18809585347548, -2.180332814944533 57.187596423830016, -2.179482766616488 57.18759764360514, -2.179019865460182 57.18916246466519, -2.178100243175161 57.18904576603973, -2.177505764265867 57.18780451894461, -2.17410685997374 57.18644245283552, -2.174135502104383 57.185499192207324, -2.173173306324204 57.18492561488328, -2.169872364780872 57.1866009672486, -2.168003807981654 57.190439209695334, -2.169332183655301 57.19147046587189, -2.17122404337859 57.19268059701362, -2.172949264158747 57.19079965464919, -2.177998315751535 57.191180086295034, -2.180801826565486 57.194160518515105, -2.175400715348573 57.1952123172319, -2.173859573336284 57.19477607917347, -2.171851851055282 57.19603038305438, -2.171209098484587 57.20373862768668, -2.170100440736868 57.20739619350981, -2.174763653929766 57.21022906365751, -2.171796335971847 57.2125590748285, -2.171131862679178 57.21207489956679, -2.169461061768375 57.21235564366001, -2.167187857922215 57.21122684438146, -2.165143571763349 57.213133932569136, -2.161123609684565 57.21383983109818, -2.160656831236403 57.21310383074989, -2.154730846850455 57.21333585592552, -2.151731256103167 57.21261189976628, -2.152109331416253 57.21195568884586, -2.148182427748604 57.21113396793995, -2.146184029362548 57.212331036037405, -2.145518318446427 57.21144250686923, -2.144708068802275 57.2116859818982, -2.143398483289105 57.21123834103547, -2.140606016807337 57.212633856592625, -2.140955571822845 57.213127524990504, -2.136921867639085 57.214721967167456, -2.138104364690217 57.21658911776379, -2.131569152988793 57.21797054163878, -2.128778249313825 57.21059845908335, -2.121002573426408 57.20707591676288, -2.11909296050779 57.20520929611245, -2.125196525712062 57.2039726853745, -2.121701037214633 57.203032921101126, -2.11761850743742 57.199400668533315, -2.121123546923978 57.19928120707283, -2.122594857719563 57.19806447483025, -2.123613588572563 57.19811323867424, -2.124284823921138 57.19907325116584, -2.125277015188136 57.19893294228099, -2.124764790423836 57.19822164303359, -2.126353926250934 57.19802840871396, -2.126498462157065 57.19749467416901, -2.127607665434637 57.19761931176924, -2.12730749747769 57.19695487546495, -2.12887868633806 57.19669276677014, -2.12930674833393 57.19606352310297, -2.128971650252332 57.195708124797555, -2.12561547917434 57.19576184917216, -2.123829761452735 57.19126317621129, -2.122197529950244 57.190474034336894, -2.12109051987139 57.188472153280784, -2.121799999331892 57.18787859123459, -2.121284931370287 57.18721434833344, -2.12292017350286 57.186422249691944, -2.124878484468083 57.18785982263175, -2.125847010121281 57.1867942686438, -2.126451911762234 57.187245841201964, -2.130087931248577 57.186679434479856, -2.128586898270038 57.18417980530865, -2.125112404663472 57.18403061793975, -2.123674864199923 57.18245270781493, -2.129258363111757 57.18223879816306, -2.129016717752561 57.18409851768148, -2.135322597683435 57.18493626593526, -2.136561433828995 57.18442289388489, -2.141014817584318 57.18529829060422, -2.146746949208379 57.18373387914942, -2.147323985867656 57.184109045963815, -2.149309229116979 57.182737527715254, -2.149035209712691 57.18042023903568, -2.147348333375479 57.18050307959081, -2.147160375857881 57.178994165268456, -2.14382158237039 57.17897850450744, -2.143889077221974 57.17952651422455, -2.143028895431911 57.17954866463987, -2.14284016068683 57.17818171517135, -2.148398704742861 57.17844474706837, -2.148177169518496 57.176819088497666, -2.150026234544693 57.17599045168158, -2.151846052012616 57.17611402247349, -2.156248337676141 57.17679132038056, -2.159849452184931 57.179571498922186, -2.16132012028852 57.17923724273297, -2.161565236246054 57.17855422787369, -2.163982298089125 57.17904514374503, -2.163788585381305 57.18012335880154, -2.168138438088767 57.180027766930706, -2.171783591604933 57.180516640359144, -2.172506074618163 57.18020149818385))
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>