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/S02002095 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002095 geosparql: asWKT POLYGON ((-3.6086803791025277 57.60220509593771, -3.6072140385625846 57.6017151533779, -3.6057377536049984 57.602012423177975, -3.6026769409723585 57.60382640368001, -3.6012778266876175 57.603308291732084, -3.601132901458258 57.601466355160966, -3.6024119486151647 57.60092070390604, -3.604155434759786 57.59990333952579, -3.603765788647817 57.59931255754408, -3.602418515174849 57.59877944481898, -3.6041415348695525 57.59844813998927, -3.6056705345068827 57.59859042699042, -3.6075440120073616 57.59857558734571, -3.6087228900097656 57.598269100667764, -3.609469640261261 57.59896081696897, -3.609688584355768 57.59964345192581, -3.6109060684483194 57.59968804593184, -3.611462990292524 57.599377262149034, -3.6113383267450416 57.598821762227686, -3.612558830652782 57.598582536948605, -3.6148953797270167 57.597633965738964, -3.6163684954499726 57.59743828899293, -3.6158829942848527 57.59670028355994, -3.617365854920943 57.59641604179628, -3.6229797807879263 57.596706216233706, -3.623877772781926 57.59683661383709, -3.622966653483195 57.59760868247086, -3.6226155376432647 57.59849068527844, -3.625476124154281 57.59864300755669, -3.625891856117824 57.59906631601172, -3.6267554570537843 57.59871109818675, -3.6298235796671325 57.59883918739816, -3.6298460707367943 57.59965878652319, -3.6304832301381778 57.59964718762929, -3.6303711319038214 57.59897070856697, -3.6324111591081163 57.599009203904906, -3.6336837154545796 57.599248276007344, -3.6331376393162724 57.600152657948094, -3.6332289833614855 57.60079760009961, -3.6325007426532983 57.60097770588696, -3.6343156807441384 57.60301155823386, -3.6351437093970116 57.603189507402554, -3.636828930314253 57.602332183343734, -3.6374869370079566 57.60319509723701, -3.6409661500516113 57.60529964130135, -3.6432453766277835 57.60476888857361, -3.644582331101237 57.60404990359962, -3.6468930166741464 57.60327844410906, -3.6468475489600163 57.60458835507819, -3.6486146042636407 57.60575065206387, -3.6441889212328595 57.60680540739027, -3.6397445647344524 57.6077993638829, -3.6399917200362877 57.60853479584326, -3.6373296854382504 57.609176637884616, -3.637252031420686 57.60948951139212, -3.6345508490628693 57.610624226657386, -3.633840972257829 57.610747044167375, -3.6311159090036527 57.61115638197045, -3.630629293585416 57.60963540021726, -3.628482119243253 57.60990565421698, -3.626594431508802 57.60961552170785, -3.62591164318796 57.609318874647414, -3.624264088529619 57.60785617659443, -3.6234461228702117 57.6074257411469, -3.6228648099692173 57.607710989947485, -3.62190330673325 57.60741464872794, -3.621459634701226 57.606321551208396, -3.6207130696833962 57.606648700195414, -3.6188776690569977 57.60659722164388, -3.618952665628076 57.607530583837, -3.6176299886478294 57.607897894124015, -3.6163024318076538 57.60809858402287, -3.615060229594475 57.608451829012, -3.6144213503571265 57.60834892541215, -3.6132452390044154 57.60753171675713, -3.6130058944260988 57.60704065070978, -3.613879331006333 57.60673303062399, -3.6116205095550167 57.604479958872815, -3.6138288407017938 57.60370611189087, -3.6127611678758274 57.60340530861434, -3.612646145515703 57.602319728535704, -3.6108863805035467 57.60226132174672, -3.609894842261302 57.60178883056295, -3.6086803791025277 57.60220509593771))
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>