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/S01007228 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01007228 geosparql: asWKT POLYGON ((-2.704096653173038 56.74285766486807, -2.6996361754293643 56.741247720376855, -2.6978239550945036 56.738733414686855, -2.6954946025534023 56.736527420940135, -2.703607649093215 56.73681428345172, -2.702345016604035 56.73572538224005, -2.7062276553850415 56.734445688481784, -2.7014413902952072 56.730214362847704, -2.7000738656713748 56.73050055334621, -2.6999281027344337 56.72969282306555, -2.6976278075193116 56.72462084556088, -2.693370628573296 56.72327006518675, -2.688783645566293 56.72441850039513, -2.6825761338607066 56.723771539009185, -2.6771290485696606 56.725129219705906, -2.676700201545034 56.72422399901459, -2.677751408521182 56.72161274687855, -2.6819222019337263 56.721805997835055, -2.683196200170291 56.721336995726126, -2.6834856554832562 56.72092732447694, -2.6865246328421786 56.7219800576951, -2.6874534103235126 56.72243518986074, -2.690712384355486 56.722187035000594, -2.6929353346361093 56.72134616222182, -2.6928617645135833 56.71962061391024, -2.694356267951269 56.71899095014875, -2.6962711457275037 56.71847398538548, -2.6994460255912665 56.718226070666674, -2.7017181596854516 56.718926696445266, -2.704641166880681 56.7186340432373, -2.707303229573817 56.717813507433775, -2.7103325262721154 56.71650755879722, -2.7127520445969564 56.71614855171201, -2.7172598357122024 56.71561640193677, -2.721326789247212 56.71609920102142, -2.723829844439178 56.715739494728915, -2.728123507309181 56.71465390514232, -2.736701394962328 56.71220476736694, -2.7378886834074616 56.71277271244893, -2.7375567664158993 56.71417617131944, -2.7458672463412785 56.714586438732226, -2.748552095495665 56.71513715075702, -2.751669662688329 56.715239843300125, -2.7511176517023803 56.710977721978836, -2.7504253481329384 56.709077782469535, -2.750886519346808 56.70805494794456, -2.7515187039818203 56.70405766044148, -2.756194395212247 56.70266380143874, -2.7546026261890217 56.70065206299446, -2.752768999286188 56.69388026263256, -2.7605502682328678 56.69191041699384, -2.7621194529026902 56.691999648285545, -2.764710852480674 56.690968573295876, -2.763771379833949 56.6865003120265, -2.759564646608576 56.687559199957846, -2.7547236874626817 56.687121464983385, -2.755225242207395 56.68445018954861, -2.7533529095415923 56.683877562501735, -2.7452372365252544 56.68290222180349, -2.739266950358003 56.683935030148824, -2.7372146417751866 56.68249179310156, -2.7387729622400867 56.68121581675524, -2.7374029037457155 56.678762318248474, -2.7416130969939467 56.67872833878919, -2.7410148415056557 56.67818386847109, -2.746643259081081 56.67724287152751, -2.7533754420920244 56.67356380068795, -2.751087326875544 56.67338895172292, -2.751020008890029 56.67119294010688, -2.7476675451825625 56.67128033584038, -2.7439632790703024 56.671282221856316, -2.736927388988677 56.66945773119547, -2.7337515008983573 56.66976397696036, -2.719065948805855 56.67147576204828, -2.7169043858134447 56.670354728149675, -2.714634995788726 56.665411673390736, -2.7246049382706925 56.6642369006213, -2.724712053395723 56.66553156198702, -2.7274954023047986 56.66525738848437, -2.7319968385445996 56.66174261379343, -2.7327961671139622 56.6608139691979, -2.73238135973625 56.6571764776093, -2.729655122845352 56.65199973057897, -2.7312741767062314 56.651272864042916, -2.7348378642719413 56.651143554752906, -2.737489195433 56.651746836127614, -2.7381157949687402 56.651480060684065, -2.737889788300647 56.65024422013448, -2.739870362874422 56.6499809276374, -2.746349202004844 56.65029855199136, -2.747141029428127 56.65023665383096, -2.750892491317352 56.65039830419392, -2.7519681456372123 56.64715929107491, -2.761941179470932 56.64922785348703, -2.7619515124181593 56.654591261890864, -2.771116087054939 56.654835573688175, -2.7694833871347893 56.65794622240952, -2.7655327308031943 56.65763880094561, -2.765121842862003 56.65830357626263, -2.7672412201108556 56.658853138467244, -2.7680293466227965 56.660707973226266, -2.771859032538739 56.66929098620733, -2.775994801623295 56.67283193465264, -2.779185247449757 56.67323425829708, -2.7809257458177825 56.67296281713716, -2.7836576184290074 56.67406865588111, -2.7831971551368158 56.674251826627476, -2.781842823161099 56.677486042489576, -2.7812537069855474 56.67862947074479, -2.7865269552041108 56.678376129071296, -2.7871766970324003 56.68003685231598, -2.7883779248129503 56.67987405380711, -2.789542475539956 56.68285771629894, -2.7887774513339725 56.68304598219585, -2.7898972978404144 56.68435638104269, -2.779555937226298 56.68553726260119, -2.7812270136145565 56.687095658661605, -2.7844477320441112 56.68617375968799, -2.787352775835276 56.68854689820406, -2.7889485119992203 56.6893695810928, -2.793031455442015 56.68886411815985, -2.7921662888436773 56.68995270086739, -2.791316053976257 56.69137709439627, -2.79110744084175 56.69442981927858, -2.793042700414143 56.69421694390742, -2.7951811677040515 56.693746077041325, -2.796636541944824 56.69322293614373, -2.799570174975317 56.69258245242242, -2.800218793825213 56.69548005962598, -2.802524046702625 56.6956179555764, -2.8039536754182954 56.695276327287544, -2.8055689541230824 56.698275534688406, -2.8036516962622584 56.6987281362767, -2.8047071393967364 56.69988145394101, -2.8063323224376355 56.70113649418865, -2.8050865238436287 56.70168360180778, -2.8023694183013057 56.70139566861304, -2.8002933990050076 56.702548024674776, -2.7985769081953955 56.70288510612944, -2.798842576398387 56.70525475548601, -2.798407578183134 56.70556274855863, -2.7974272683022656 56.707420287156836, -2.798146945344468 56.707909240705376, -2.7956861110832723 56.71049440312384, -2.7929383411719746 56.711531694603146, -2.788771770040142 56.71133236824279, -2.784675050373381 56.712001680193836, -2.784724877642586 56.712852921159275, -2.7813102226008413 56.714681831166345, -2.7822246650005833 56.7159340999454, -2.7823005301025336 56.71722731448969, -2.786553767909857 56.71983283790787, -2.787218325188357 56.721921904176995, -2.788849886890988 56.72336698711641, -2.7902918714246336 56.72433711043229, -2.792785768425534 56.724815377158805, -2.795091622936665 56.72567212960205, -2.7964114962576705 56.727038247099834, -2.7980310548672485 56.727117725248185, -2.7986018534421526 56.72783278546577, -2.799743068534474 56.72847231185073, -2.800961350245429 56.728123096565966, -2.8019557084273994 56.72723627261189, -2.8035105011843657 56.72734303934856, -2.8047942629752374 56.727002356985075, -2.8056433672469083 56.72620628077395, -2.8069749340692627 56.725811353768165, -2.8100780620299575 56.725728331142776, -2.810528217068586 56.72538401277594, -2.811583990232531 56.72584430767139, -2.8137436983978814 56.72596498228976, -2.815547258356337 56.725486024304416, -2.8175880981656043 56.72540077861517, -2.8187053415737204 56.72718125553033, -2.8201581226923516 56.72785448479606, -2.8232883720397846 56.72751041977163, -2.826128189455388 56.72733889746486, -2.8292928780825846 56.72782098002944, -2.8324987248809035 56.7279433478717, -2.8346566407027742 56.72723714524768, -2.8348433714654457 56.72607695884473, -2.8373066227504875 56.72588075151553, -2.839086275102453 56.72654258056251, -2.840706000373838 56.72735819145036, -2.8426359183185497 56.727426012168266, -2.8431909217249496 56.72666760167608, -2.8444911933262578 56.727808749034836, -2.843941433647919 56.728800713749656, -2.844429182874771 56.73014500972951, -2.841203394602118 56.73297086613363, -2.8388286491807144 56.73331824754084, -2.8387604109627946 56.73465732126348, -2.8397684194200172 56.73734572933197, -2.842668273563301 56.73909600172728, -2.8399928875070226 56.740812034248464, -2.8374624492588083 56.74023611979049, -2.828102360938824 56.74295037831913, -2.832312548999645 56.74613714959663, -2.828708715669242 56.74657442376788, -2.8259312767614984 56.74667371392412, -2.819182364719632 56.74910798136089, -2.817385332827688 56.74993731726056, -2.8106581037544065 56.750394508920536, -2.81152981107855 56.751386054429595, -2.806897063000016 56.75191922842424, -2.808814727915455 56.75440433487382, -2.8073822902299663 56.75395543977342, -2.7987766771779397 56.753712144249896, -2.796428118453346 56.754204369785896, -2.794319968672362 56.753559469544115, -2.786011925499361 56.75571810346268, -2.7828463781239745 56.756804960062546, -2.780234013020428 56.75683320485365, -2.7777649272881013 56.75700283307726, -2.771153790837659 56.75894164331415, -2.7676742790680415 56.75590564682102, -2.7593074915964735 56.756766381514666, -2.751306149782516 56.75810619298183, -2.744532355907566 56.756323104367695, -2.7454792749977175 56.75211298954737, -2.7416428593903586 56.75161480831586, -2.7402752216407062 56.751919405682074, -2.739452104439873 56.74831277087178, -2.7374130047875003 56.7485584316574, -2.7327534810257403 56.749430394395915, -2.7311278567629267 56.749080584941034, -2.730601221434591 56.749748479540656, -2.724945224059425 56.75068887283164, -2.723632865134345 56.750471901546646, -2.717028002067767 56.750284655168095, -2.7159051290835783 56.74892054896744, -2.7156517485376908 56.747478952682435, -2.707596076215396 56.749854242617005, -2.7056436979565675 56.74776308790802, -2.7077724373410432 56.74358250635181, -2.704096653173038 56.74285766486807))
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 lang='en'>
<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>