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/S01004372 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01004372 geosparql: asWKT POLYGON ((-5.285169797155381 55.70355776730795, -5.278917545050663 55.701249879356745, -5.276721975854174 55.701145427287535, -5.276315630334369 55.700095403577684, -5.274317784176371 55.699240707141776, -5.271053765776529 55.69961548256067, -5.267975954445274 55.700695456624594, -5.265931853243948 55.701954614319355, -5.265241766681163 55.70398684394992, -5.260470282454055 55.703664229088616, -5.253125504064281 55.701305939404826, -5.255207617990157 55.69954252865553, -5.268947786086128 55.69594058059943, -5.265411558148195 55.687709606162734, -5.259623936122169 55.67955638575467, -5.258664250300561 55.67337847974341, -5.262402869966896 55.661537518237594, -5.254768683663523 55.66076944698147, -5.252827526712372 55.65884306433609, -5.248169524801336 55.65694371058799, -5.242451116849677 55.656636570366935, -5.238067381838599 55.65188862039162, -5.232875450062331 55.649472375086695, -5.233255726975371 55.64579426431583, -5.224528722831572 55.639425204237185, -5.232972461072814 55.63580440838331, -5.23491384432942 55.63409894788916, -5.233261195616506 55.63043850714605, -5.235547446187112 55.628678981528445, -5.23680612838046 55.625445151938564, -5.243011955759291 55.62036330385066, -5.242440534848851 55.61787008938535, -5.249526437492357 55.61046527204094, -5.224574799989948 55.584879211937974, -5.211435755067738 55.573464981722985, -5.20956073836367 55.56682528071966, -5.21157857034246 55.56530693930234, -5.208676419494175 55.557543389054466, -5.219159280252844 55.55190091963547, -5.220271747911315 55.54863509957135, -5.223460136736554 55.54845229338453, -5.229513755721094 55.54917381229846, -5.234866549714288 55.54792665361813, -5.232555136122865 55.54083127359103, -5.214917299363898 55.5378344623223, -5.219093071739882 55.53286986370949, -5.227593920817286 55.529247580185114, -5.248141456146597 55.52489197469655, -5.269298068285833 55.52124482024504, -5.272308498644916 55.51933026340627, -5.275520335081223 55.51946010768229, -5.278581856901229 55.52140102087145, -5.283631417338855 55.52228157443891, -5.288608034329011 55.52171638095858, -5.298402380696584 55.51831500780248, -5.301616033732432 55.515441207666676, -5.272314266093667 55.50909874268168, -5.277107486326083 55.50215551715138, -5.288013593074901 55.49655836160902, -5.298115192650075 55.48175735304318, -5.294389240692182 55.476004786603326, -5.303806765021543 55.46899864147122, -5.303076907030952 55.46670772333825, -5.30521540017595 55.465598012509204, -5.306108189145001 55.4623641816594, -5.311036047776319 55.463761428467265, -5.314821785900598 55.46620263493194, -5.316851766630986 55.468291634365194, -5.316599789949938 55.469097943864945, -5.325115906912385 55.48136755831238, -5.326744199747348 55.491571105117345, -5.324493239166964 55.49522993797183, -5.326531004473584 55.496628125858734, -5.327556472457674 55.49816284518747, -5.332647705461977 55.50009561842701, -5.333834961979531 55.50202561564445, -5.335242880422611 55.50202358037327, -5.34801903238 55.50625343823758, -5.354874871363964 55.507083148680685, -5.354579055381783 55.508871922197876, -5.351807732573326 55.510910236193, -5.34955414714274 55.51377013188414, -5.350712749826763 55.52136981153086, -5.353257487448012 55.52493410251391, -5.35371875200416 55.528846173724475, -5.351659117112858 55.531737124898044, -5.348453124029482 55.53847508272924, -5.34583276698636 55.541562944760145, -5.343310598269182 55.54279473205955, -5.342207663077188 55.54489622007165, -5.342440980537638 55.54688854297783, -5.34445058357105 55.550176912328546, -5.343686481801846 55.55325032401998, -5.344378352354354 55.556865400805094, -5.347548523036572 55.5624841045568, -5.348392700092472 55.5656225729831, -5.347439850397206 55.56873750549833, -5.349294412366721 55.57093986231244, -5.356035749816022 55.57555213648401, -5.356772523703061 55.57742161639479, -5.364730370440452 55.58123694862097, -5.372494277643678 55.586510730208836, -5.374282288324642 55.58940501865095, -5.381649423814387 55.59748737371872, -5.38377592602261 55.60131704086059, -5.385332663568399 55.60225520884627, -5.386764747928174 55.60472309918721, -5.392426113694588 55.608600208286845, -5.39673374271074 55.613105388558175, -5.398103027667773 55.61754492151923, -5.3973458682035 55.61798501653749, -5.395284311481125 55.62309345189441, -5.395282245399345 55.62603705850708, -5.392583884828047 55.63047261833757, -5.390691984118191 55.63161517670887, -5.388435817202293 55.63225902333935, -5.386136134700608 55.63537514728755, -5.38329406665478 55.64188581065653, -5.383188564600796 55.644529529593775, -5.380976875580856 55.65104822374699, -5.381189071301617 55.65384724652125, -5.379415511070007 55.6577485342683, -5.372158621036917 55.667719440018296, -5.371708875771151 55.67088738470197, -5.370642289206661 55.67248648811302, -5.367210088477257 55.67590454892944, -5.363556290327025 55.67839952622621, -5.348965438746437 55.68474154484515, -5.333133109679496 55.68771169652742, -5.330280705206619 55.68850198189917, -5.329381126918778 55.68942340354391, -5.328585830706251 55.68940494726186, -5.329081164193234 55.68989606767316, -5.326295288621002 55.69363973363919, -5.326696033401429 55.69551897076555, -5.324844106176483 55.69874871727212, -5.320915580383446 55.70273945871764, -5.316246825720588 55.70586908406375, -5.301983153951193 55.707895642840676, -5.298628679981779 55.70712893944737, -5.294655998997742 55.70491313853512, -5.288859618934877 55.70545308808481, -5.288626579512213 55.70521106217949, -5.28986679347796 55.705289940240306, -5.292729336887612 55.70459646312006, -5.288348724910277 55.703592727775295, -5.287078530008634 55.70248151220506, -5.285169797155381 55.70355776730795))
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>