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/S01005509 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01005509 geosparql: asWKT POLYGON ((-1.147698382545918 60.18865237824196, -1.153203902540406 60.18891091768195, -1.155444498588882 60.186718549629724, -1.154452565779992 60.1843530156479, -1.157865094139654 60.182976675623344, -1.157700075398032 60.18085670688013, -1.159448610929721 60.1763162030911, -1.163780263378867 60.17659117196808, -1.16634269649419 60.17473922539854, -1.165054376469139 60.172125218649775, -1.166032766560848 60.17103967971967, -1.168174363822581 60.17136210310119, -1.169124841814646 60.17007860872174, -1.170552957970011 60.1702796928229, -1.170672926964273 60.16985452651409, -1.171052317451402 60.17035646981353, -1.174726641888333 60.17019639518577, -1.172264774105924 60.166526568893026, -1.183281260440995 60.164363021957314, -1.188419152637938 60.16596191031167, -1.195765625760333 60.163420114377246, -1.193212718944559 60.162913324163746, -1.193161025895775 60.16181658428177, -1.191453892453432 60.16094470789659, -1.195009021648206 60.16014402309329, -1.193422677986395 60.157549953534556, -1.196510893753375 60.15655059924899, -1.174956897494395 60.15144510783669, -1.175874637984924 60.149297116969365, -1.178426599294277 60.14774663649397, -1.18451081067714 60.149428854671186, -1.185533496145544 60.146185018622006, -1.184958599812476 60.14402778693717, -1.195471127030126 60.14444444968906, -1.199172847265931 60.143996974133984, -1.20745977174215 60.14553480116886, -1.218281098487872 60.1428971709698, -1.220583753534724 60.14347867370076, -1.223764135511939 60.14514197521217, -1.225822908586519 60.14542569686271, -1.230104699664337 60.143286652704845, -1.228318738573812 60.14112125350064, -1.228408778976587 60.136506458097934, -1.232922168212981 60.13905582525929, -1.237461180648635 60.14052767598595, -1.243577820169521 60.141532486569766, -1.251482160179294 60.141594715576716, -1.250940350592373 60.13858415074471, -1.253260336818178 60.13768441695114, -1.252423779118009 60.13318577956088, -1.256304541324509 60.13294986901248, -1.25729177104344 60.13111820027848, -1.266909435070718 60.131341489347925, -1.267579241211218 60.131817989342174, -1.26534742524821 60.13164803364863, -1.264405392052225 60.13245704501623, -1.262400721394856 60.13605917293505, -1.264771446851413 60.13643195984574, -1.265103680042192 60.13854574591108, -1.267044113900901 60.13858374614696, -1.266931282894355 60.14232819157383, -1.265030466217264 60.14281256901903, -1.262984455228267 60.14490128309167, -1.264078955136726 60.145077967923854, -1.26573463617007 60.144314944727405, -1.269528674522622 60.144560390058736, -1.269699076046584 60.14418420505592, -1.271596461524586 60.14469748069787, -1.267367480701784 60.14862500269961, -1.270901246643052 60.148991744243894, -1.269715094685625 60.15096055806362, -1.271999969261014 60.15108101758878, -1.272325095693267 60.149324069834186, -1.273601888082754 60.149244386200074, -1.275124992440867 60.14587195544325, -1.273398802754515 60.145313487071554, -1.274770583067666 60.14354392776132, -1.277985543961114 60.14217009604206, -1.27821092623686 60.14152877764939, -1.27913105033408 60.141537183861395, -1.279413682289344 60.140870733318636, -1.280982296727709 60.14115079526021, -1.280919693656212 60.13993412577916, -1.282484675133239 60.13836371755427, -1.285755482831071 60.13727795970915, -1.286294091531752 60.13669510000699, -1.285092354749901 60.13608668643009, -1.285724881425314 60.135053629229695, -1.288936082464489 60.132958735386865, -1.291467750398746 60.13254365433595, -1.295617319160591 60.13209489388349, -1.297502909983712 60.13145840565456, -1.301210237369079 60.13403706823148, -1.30273315711584 60.1343773215471, -1.304530253664172 60.132815415512404, -1.305815911564062 60.13245401261682, -1.303735236510773 60.135513962691135, -1.304342690259975 60.135732652776106, -1.302971340657277 60.13843714854345, -1.299656612951872 60.139380437829814, -1.299128172263973 60.14137101567101, -1.298006993034937 60.14159853710428, -1.297318470635583 60.14264544957274, -1.293254962272098 60.14405152332888, -1.292093542160631 60.14866060564927, -1.296507050918459 60.1486032932007, -1.302027860931003 60.14643259282153, -1.304481815766882 60.148762110740655, -1.301899018240049 60.153363801389226, -1.30079231839874 60.15798221678014, -1.29657832430618 60.16129117767706, -1.281159765519748 60.161962717790246, -1.266677743639452 60.16181156842841, -1.265862092831977 60.16283965606758, -1.264949548512192 60.16255624606764, -1.261433084316529 60.16831927353081, -1.276626560785349 60.17066577571806, -1.26495418185131 60.18736561161211, -1.268583644588126 60.191991957410345, -1.263831706747343 60.19482102583056, -1.262541098320765 60.19929443152084, -1.254710665068132 60.1978050392863, -1.246414679661176 60.196985969390596, -1.244769337844688 60.194022481993706, -1.239128548494017 60.192293193088744, -1.236406396275527 60.194585188397696, -1.231396804856369 60.19441259181114, -1.22726505233339 60.19218869859383, -1.226955946362124 60.19536551337042, -1.225594672781446 60.19800799876103, -1.223735138776824 60.19738195927726, -1.223442035623746 60.198388364729084, -1.224349399418893 60.19878281804131, -1.222755816536961 60.199094256174874, -1.216790358069223 60.2004060438286, -1.211370613337399 60.20225956233721, -1.206194464102708 60.202964983456944, -1.205983341379679 60.204984031779574, -1.204775913181628 60.206422444481035, -1.199107977470318 60.20728627022211, -1.196275526513118 60.20949597541171, -1.19539841710209 60.21142117888306, -1.186580318695198 60.2163059581028, -1.178486476245965 60.22171543553613, -1.172126082642049 60.224854539906474, -1.169348659203803 60.22332868764985, -1.166242911687499 60.224081424933466, -1.166055241614159 60.22223053519244, -1.170898532814284 60.21913612581283, -1.168118554091981 60.21844529368025, -1.168591850048956 60.21685894486375, -1.174847071481247 60.21570375239947, -1.180555342831414 60.211860120193386, -1.178664852664756 60.21025014122111, -1.169800279220004 60.21117370746898, -1.169321688640614 60.20869245651306, -1.180625859717306 60.20541350844907, -1.185977730637536 60.2047579990686, -1.18842672698321 60.203656638632516, -1.186941426863612 60.20317164686286, -1.186404508029239 60.20227043768042, -1.187677405967216 60.20112890246933, -1.186811203203441 60.20041424262391, -1.187769217062752 60.199603005801826, -1.187007713550001 60.19903265211322, -1.188869372815634 60.19814612936011, -1.187124630183965 60.19649225855993, -1.18902840854248 60.19535457474898, -1.190677536255808 60.195032417235005, -1.190689863648393 60.19306605003231, -1.195953996133408 60.19092510215057, -1.195739804087554 60.190097718687774, -1.198538616627326 60.18698092425341, -1.201771383754276 60.18527641606651, -1.201880033098464 60.1845138374199, -1.205070906138271 60.18303347910431, -1.211219631947017 60.17697332704477, -1.213012643566541 60.17737008497744, -1.215801609974315 60.176820918766275, -1.217636077764947 60.17547589750358, -1.219209323673087 60.172979978113034, -1.219196289147026 60.172010149649125, -1.218265446025069 60.17174426954794, -1.21877371047094 60.17083138920823, -1.218109346064219 60.170728702092646, -1.215320117838222 60.17206808213248, -1.21257059055062 60.17249176259463, -1.211650239514608 60.173294419160115, -1.212430892485792 60.17381087369217, -1.213955782374976 60.17335300421642, -1.21432606802816 60.17369640693743, -1.209691442293809 60.174557805751036, -1.206396386367857 60.17741146753042, -1.205732367178523 60.177290762541425, -1.202513651713826 60.17845669830631, -1.196511894066747 60.181374625595176, -1.197065024293478 60.18234772861531, -1.199026072565225 60.18251223227913, -1.198343377865867 60.183154609166216, -1.193621296041041 60.18309904357497, -1.192129515683219 60.183646676898405, -1.190502579694751 60.185288929044006, -1.184879107542387 60.187400516372705, -1.181204746690131 60.189416161593336, -1.17725668138067 60.19298341270597, -1.173389466148733 60.1933005819286, -1.172032096752792 60.19420799377346, -1.168422452064937 60.19502946201954, -1.167851804400606 60.19691151549416, -1.154721374509425 60.20344817935718, -1.152695668664333 60.20241368368465, -1.15419043733862 60.200731078179125, -1.156989886398798 60.19918651346756, -1.158454475602929 60.19693304948183, -1.160378835182476 60.190085118782534, -1.15863477623189 60.189331710580085, -1.155102635479741 60.18938770958166, -1.151720476049192 60.191028870896595, -1.149632164110446 60.190340596409904, -1.147168909225349 60.19044862305466, -1.146398640150769 60.18969320213024, -1.147698382545918 60.18865237824196))
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>