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/S37000015 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S37000015 geosparql: asWKT POLYGON ((-4.180429231197934 55.90491354469445, -4.173354545559432 55.89701846458556, -4.168195726079464 55.896581903188654, -4.161526406103675 55.89797269901596, -4.163118865306573 55.89623625436297, -4.160275301473201 55.890085027783186, -4.163262321569655 55.88961914623801, -4.161633498637007 55.887677770261945, -4.1696872356079 55.88622570093372, -4.164394330111054 55.883584095568075, -4.15843100046189 55.88587362972743, -4.156561834183137 55.8838006979852, -4.153116555168272 55.88453147357815, -4.150884575889701 55.88730980211917, -4.147786644613893 55.883626138349555, -4.12871531444169 55.88335269013918, -4.129541554143263 55.885446779270275, -4.125213443650549 55.885455095935534, -4.125319552987248 55.88806851980461, -4.115685735032757 55.88635261628901, -4.10730404122662 55.887454453346756, -4.106148659248538 55.884487820500524, -4.094956411135301 55.885240557423344, -4.092973601051221 55.88235889079144, -4.089342464106032 55.881524586251494, -4.081933594177689 55.884155746697616, -4.078818288969981 55.88160396771128, -4.079772668638066 55.86826992717521, -4.076740468936368 55.86353553834341, -4.071716850603421 55.86127012384565, -4.080429776975935 55.858799927183696, -4.085962836398095 55.85870083598832, -4.088404355301814 55.85384592289314, -4.074716230762382 55.84412436999007, -4.078534927304547 55.845043081196785, -4.081779864163597 55.8438019031303, -4.084862820407806 55.845266704039204, -4.091847100867783 55.84313769566122, -4.10218134540714 55.8425139315198, -4.105814303743571 55.83925055716147, -4.102818179760251 55.83907444859617, -4.102711016254121 55.83797715267797, -4.108186960264961 55.83746819546627, -4.107044683504119 55.83465681638393, -4.111973924556971 55.82940553133462, -4.111164511245591 55.82587219079806, -4.130610625331607 55.83328374825379, -4.146171765771292 55.828202269659144, -4.160775476232931 55.82717359876952, -4.17252076676434 55.82297087271696, -4.175854984681804 55.82432831449119, -4.180503468598646 55.83561313541543, -4.187770274673516 55.83658771180289, -4.193423036833386 55.83169675711929, -4.198842913675144 55.833651773587555, -4.199153300960378 55.83561355674943, -4.195068380259435 55.8386775321158, -4.203020988872772 55.84413445126591, -4.2052803633285 55.8443725667856, -4.206490079289567 55.84288772558847, -4.202594598773971 55.84009867459349, -4.203519284914602 55.8382343068946, -4.213755079853787 55.83651812027622, -4.220447138477603 55.83310807395761, -4.223172294731775 55.83468915170097, -4.222425683974813 55.83853667782231, -4.227590452988438 55.84081950412324, -4.233648854517948 55.83360953916046, -4.230743483905691 55.832596370556985, -4.228882600557264 55.824466826433955, -4.234966556905726 55.823844997177, -4.233765679525991 55.81866038693038, -4.227876217085433 55.818230704212596, -4.225501589495032 55.81217663017133, -4.214543417501345 55.81507614681415, -4.211343713789881 55.800316178825746, -4.204298878273124 55.80044991483732, -4.223943783747591 55.79194798854951, -4.229936962711959 55.78591842958816, -4.224520643683964 55.78427212819981, -4.226711918976281 55.78127915091328, -4.233284830947649 55.78315958301463, -4.236134444003609 55.78265119361297, -4.236604003634344 55.78444374482239, -4.243448321201964 55.78714285777958, -4.250754439720784 55.78488999464693, -4.249477829883084 55.78557036007313, -4.251536877242391 55.78664710509263, -4.255587773624237 55.78716329787124, -4.263273923827056 55.78600188650157, -4.268259350073841 55.79164403606981, -4.267444048573325 55.793096194089905, -4.263149917915343 55.793542938388114, -4.260319051447069 55.797989049742434, -4.261469216754394 55.79955419381628, -4.265796393263424 55.8001863117594, -4.265607527130914 55.80413083553992, -4.26925701660633 55.80281513968602, -4.269304012270884 55.803979059827306, -4.267097585740757 55.8068140067659, -4.265593128919957 55.80635462068422, -4.261085985590219 55.80898737708638, -4.260696268282029 55.81101853742424, -4.26668928071147 55.810935137363664, -4.271972925224365 55.80873091145276, -4.292638691282297 55.813940187776886, -4.298522008917788 55.809472226309424, -4.305547647524169 55.81044613043765, -4.306709611679857 55.80912561712645, -4.312007409614435 55.811587564741465, -4.315056711196461 55.80864513595076, -4.326251833499802 55.80844995323926, -4.322703200052115 55.80058722859249, -4.333462364277728 55.792878680895804, -4.343709023958909 55.795028590659236, -4.372073990526824 55.794769276061345, -4.378090639111862 55.79990368031846, -4.373788000239484 55.7996012829731, -4.369819706697544 55.80217377731491, -4.372058543699126 55.80272558056958, -4.367796719453811 55.80336230600373, -4.374284595478113 55.80728525238375, -4.37606787680111 55.8108566989411, -4.372189520488424 55.815840738000695, -4.368335403536064 55.816065147895216, -4.367467685358542 55.817705054147545, -4.381428599425493 55.82315253546535, -4.37757304573238 55.82679171624712, -4.376131989832179 55.83499755373119, -4.370254354048889 55.83945028278504, -4.368044183657656 55.84544454839226, -4.368780340527408 55.847120066211005, -4.374893780471885 55.848658604497814, -4.375629388546227 55.85128769531332, -4.379888702394789 55.85104896203932, -4.380887042654812 55.85634679876061, -4.364686108439005 55.85532915377533, -4.360498232082853 55.86083727312399, -4.356723365472869 55.86352597257898, -4.353106612950038 55.86357903378393, -4.356271513305995 55.8731900972718, -4.353448302095329 55.87374385067263, -4.38243410716678 55.886515658205866, -4.393200541438153 55.88915135269038, -4.390479505671552 55.89075283033975, -4.385333944564608 55.890146244829985, -4.387231816324359 55.89125717453753, -4.385933452867532 55.89264309695343, -4.3836057574536 55.892086926904376, -4.37894266269839 55.900093531156344, -4.375489750897132 55.9000299044872, -4.389967844879066 55.91051689095286, -4.386891956866177 55.91118773917235, -4.388733704359974 55.91316346083096, -4.387807162993425 55.91662462651737, -4.380006803371399 55.91879251944063, -4.379632276113629 55.92098556273655, -4.357563045326837 55.92074328715345, -4.346230466708109 55.91649744255768, -4.348688053444629 55.91516445190468, -4.348500687009253 55.905240308245304, -4.334886683697116 55.90385279137304, -4.327855695867274 55.899695301543645, -4.319460666611586 55.90119833838713, -4.319597167635715 55.902975301816234, -4.313999451137446 55.907031591568135, -4.300434593312158 55.90557014144174, -4.300884041251513 55.90886285116307, -4.290435962560919 55.91179798839913, -4.291991359938358 55.914319683728614, -4.30127924676312 55.91636901249688, -4.298065076816183 55.92917330035219, -4.284280124827172 55.927601766517206, -4.276358809036592 55.92964122233229, -4.26839799600028 55.9284993471317, -4.268809009564203 55.920222459220355, -4.263719984856528 55.91888803929304, -4.260266515438255 55.91292813648869, -4.256212507930528 55.91410076502892, -4.251354741658592 55.91124094077093, -4.253148658501178 55.90989236474144, -4.249407665345941 55.90903598117604, -4.246774032982035 55.90367097998371, -4.237715093397536 55.904768893937536, -4.237083610959718 55.90208236638739, -4.238714773324697 55.90173904947531, -4.23637241192287 55.89684217543698, -4.225973852037764 55.89608881294902, -4.216598947772542 55.89773912586498, -4.198883220235508 55.898011970645726, -4.19086224456208 55.89958757943935, -4.190721633440798 55.9009804116505, -4.182462103469235 55.90296772942585, -4.180429231197934 55.90491354469445))
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>