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/S02001700 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001700 geosparql: asWKT POLYGON ((-3.9013502296893017 56.00161875618325, -3.8990954315188713 55.999256421225084, -3.8974815029629997 55.9997155939133, -3.8968645026032003 55.99927265450249, -3.895906913327817 55.999538388798975, -3.894216692500743 55.999674655886324, -3.89378096194664 56.000202379941776, -3.8894475377719844 55.999258587781576, -3.889665117145123 55.9987588827748, -3.888992057405953 55.998282273923735, -3.8917399433811246 55.99727335522326, -3.8941243063520963 55.99616691212329, -3.896376993292038 55.995028199846104, -3.8961272031674135 55.99481960117782, -3.8974047880385507 55.99423520981969, -3.9003231236367926 55.99322194319534, -3.902120669134233 55.99283056697131, -3.9060469458520553 55.99218805207936, -3.909019766321256 55.99063285955563, -3.910453345939704 55.990101522320394, -3.915011115495239 55.98923948689934, -3.916247677269477 55.98893664216288, -3.9151581714642454 55.98702650522859, -3.915594033763032 55.98580041497202, -3.914846090339765 55.98399105238551, -3.911301199129454 55.9845611543027, -3.9113422282765304 55.984093216386235, -3.9108012323056234 55.98384998159539, -3.911550208450512 55.98311045443501, -3.912846613633213 55.98112227919047, -3.9140519438386523 55.98053740978723, -3.914492898656435 55.98005427138895, -3.91552155101421 55.97946314511917, -3.9138766660363373 55.97859306356548, -3.9118365316883046 55.97940553936575, -3.908793303695071 55.98087335649953, -3.9068273586363986 55.97926625934233, -3.9081399670184482 55.97880673216645, -3.9097443311984663 55.977950225928026, -3.9076484775881557 55.97614341575934, -3.906668594057821 55.975772158667226, -3.9069609946766395 55.97481405084339, -3.9056985833976485 55.9734503946663, -3.9035751389439977 55.965979688532855, -3.9024807871513807 55.96222582301459, -3.904256232465195 55.96206898720107, -3.9061146675329264 55.96030943149067, -3.9085918576672936 55.95973278288664, -3.9096917268471905 55.959393119863975, -3.9111826803695773 55.95931787719322, -3.91186144273111 55.95956795704142, -3.912238660273151 55.96042751241516, -3.9140429977116193 55.9610204675789, -3.9166418584321425 55.961059146313694, -3.9175709781455796 55.961640500709265, -3.9193365427171774 55.96212704215864, -3.9197780312934727 55.96266295502558, -3.9212048656281056 55.96345759689399, -3.9211064266325066 55.96392823320291, -3.922105007642874 55.964421301022504, -3.9235484329746546 55.96457940506576, -3.9241969477317102 55.96482989043643, -3.925960530273217 55.964594743757374, -3.9288722547989763 55.96344563492798, -3.929871613854084 55.96353422990654, -3.932424142833603 55.96305746990997, -3.9333989942849406 55.96326504430657, -3.935870365841904 55.96251179856559, -3.9379866095869467 55.96289281242974, -3.9381999798965817 55.96392381926671, -3.9398432358137967 55.965064424610496, -3.9409880473230934 55.965094217064916, -3.9409485368414154 55.96601937279764, -3.9402317540714145 55.966168158254774, -3.9400954298804858 55.96664569870704, -3.9405586953100515 55.9682497092319, -3.9420533552469244 55.969489682582115, -3.9433373101630456 55.969402038755206, -3.944988469795068 55.96973455297237, -3.945289716806921 55.97040469507422, -3.9458134846504183 55.97059143528011, -3.9465433161290413 55.97019078541793, -3.9473726732430285 55.970654872051725, -3.94817555587563 55.9707194654828, -3.949686157345312 55.97137765963558, -3.9494198949433454 55.97216910396869, -3.948753889969393 55.972691878491396, -3.9494072139121172 55.97325489308075, -3.9491399557624676 55.97393132297153, -3.948139124566833 55.97455285080755, -3.947419618970668 55.974681949197624, -3.946782304941353 55.97549003433752, -3.9460684135181725 55.97598658987779, -3.946149539046045 55.977314433899885, -3.945196434430411 55.97805919830054, -3.9448833266017638 55.97871836801886, -3.94433172918925 55.979001164971365, -3.943873036954022 55.980017601022304, -3.9432682557750547 55.98045490334877, -3.944605937792252 55.981558030674236, -3.945522475312091 55.98244492457689, -3.9470692881629548 55.982762886517946, -3.948630804119397 55.983308857529295, -3.9492965805490194 55.983860892494555, -3.9526281030019423 55.984251193823134, -3.953033653880743 55.984187252950235, -3.9547880970606144 55.98477322543161, -3.9558756709475134 55.9855171397377, -3.957792098885912 55.98619755530964, -3.958665289363839 55.9866312166611, -3.9634267247007964 55.986696543281326, -3.964818264926909 55.987107523921026, -3.9715840945852348 55.98579435883933, -3.9761756161661292 55.98457321784605, -3.985239176101651 55.982787471016124, -3.9855843585833246 55.9833921080982, -3.985521484243053 55.98432324642207, -3.985128798998697 55.98532530057809, -3.9878398740459353 55.98492392961691, -3.9893320592832913 55.987290319953054, -3.9859430822468145 55.9883559818871, -3.981217149876105 55.99033722462843, -3.9788479320548182 55.990942347387445, -3.976999124958307 55.991064580094054, -3.974642593671528 55.99088308242046, -3.9751516508151292 55.99168462606283, -3.9752883763320463 55.992887549538516, -3.9771322838849574 55.99273037384435, -3.978059330816089 55.993018350862265, -3.9801988388629157 55.99393838292426, -3.981446596746064 55.994914954884074, -3.982249322928067 55.995278590421236, -3.9815883493680158 55.996619249319245, -3.9809493965079876 55.997231628155596, -3.9809380374155316 55.998169122554636, -3.98118275341765 55.99897129560974, -3.979641301356951 55.999591878545424, -3.9776862143928793 56.00030984425595, -3.9764166654002557 56.00025831294358, -3.975186427210251 55.99940632448616, -3.9728936746805794 55.99910154848038, -3.970724162994708 55.998066839629715, -3.9696015961225206 55.99838134213012, -3.966857906165477 55.99866779233811, -3.966242332224144 56.00012446526138, -3.964190873876028 55.999833651758536, -3.9627888748526754 56.00033227652336, -3.961937885967903 55.998413694154266, -3.9582484157034004 55.9983914828969, -3.9570024060944613 55.99912121615588, -3.954139302633562 55.99895096831367, -3.9462811480001556 56.000540103998866, -3.943293105669526 56.001396040974086, -3.941575749902374 56.001369182739, -3.9415411366858923 56.000039700999864, -3.942570147751407 55.99913383257227, -3.941418942343828 55.99888236200674, -3.939696473982202 55.999071237614174, -3.938874309632198 55.99898531971443, -3.9374261835583297 55.99952931200184, -3.9370377062460835 55.99881648917703, -3.9354851948581127 55.998876822523094, -3.9335272710681988 55.99916816102008, -3.932480887952798 55.998124152036866, -3.929889102080002 55.998245645851455, -3.9291498488106216 55.99853580189527, -3.92769662251119 55.99865738032297, -3.9266758838355416 55.998125157545964, -3.9256633850864793 55.99872510679694, -3.922105304591419 56.00005673298907, -3.9183617826593617 56.00153492511154, -3.9167672451144377 56.00269203539876, -3.916528195496268 56.002722711808225, -3.914613302746707 56.00274578180342, -3.9106063834787292 56.00232037603595, -3.9110540066908692 56.00134742873708, -3.913938515456291 56.001090261592516, -3.916249153716337 56.00055820728726, -3.9143520944354897 55.99912706218033, -3.9150257180498587 55.99893576236724, -3.914829473405058 55.998099843952026, -3.913779339827593 55.99780368995159, -3.912217977837196 55.997961839054916, -3.9113532288272466 55.9982828156626, -3.910656978644726 55.99879687448691, -3.9094934676824367 55.99899617130973, -3.9084125548271236 55.99946069222747, -3.902680310000821 56.00143641331152, -3.9013502296893017 56.00161875618325))
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>