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/S01006153 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01006153 geosparql: asWKT POLYGON ((-4.16841910237633 56.20357739842319, -4.175242122386141 56.199647101351175, -4.140737020537512 56.19114587660075, -4.142542501230408 56.18912851142469, -4.151948983014985 56.18759010530363, -4.146057099839835 56.18366646618686, -4.156532073639824 56.18166878134987, -4.159511651042737 56.18164371053015, -4.157824244057473 56.1800016345344, -4.158551563270766 56.17946769568659, -4.158285186753845 56.17701894103221, -4.154020053833667 56.174758493701916, -4.156839503277512 56.17476662737666, -4.156884545176558 56.172551506535044, -4.158868066808679 56.17155886954651, -4.159340076503574 56.170556941917276, -4.158067319050486 56.17086311233026, -4.157463890498834 56.17043541734515, -4.155862003412216 56.17103092449588, -4.153606930654856 56.17009846628864, -4.154159629633521 56.167798538433715, -4.157722513643868 56.16268575605201, -4.153700595653844 56.1619021826468, -4.153452229964631 56.16120553776, -4.151458748122147 56.160404492904625, -4.150075576296945 56.16072513872251, -4.149387914303647 56.15965868345384, -4.148402411122165 56.160179088643964, -4.14799833046269 56.15928743098875, -4.146938557496557 56.15905422133383, -4.146766322382679 56.15798778058095, -4.155028386171742 56.15628021688725, -4.148929917136695 56.14915203769687, -4.14527259254391 56.14997050577489, -4.141982131331384 56.14899412192235, -4.142198782925434 56.14739070695683, -4.143618209780694 56.14715039723616, -4.14442734180405 56.14549175899437, -4.145978415231813 56.1450154773962, -4.14739003119012 56.14515417175475, -4.147197288384346 56.14435622742396, -4.145786830404163 56.143311303483486, -4.14918767596847 56.142811816755746, -4.149640619849579 56.14169854980997, -4.151567281551568 56.14218624006193, -4.153047714428198 56.143041155139116, -4.151440239165743 56.146825676495006, -4.154564883520666 56.148002412758196, -4.157964031220971 56.14775434971129, -4.162880027396413 56.14527785288193, -4.164956670554667 56.14527741848148, -4.167046135668889 56.14607655834486, -4.168238275527371 56.14609158439709, -4.168851122612417 56.14525402948127, -4.170712886391471 56.14458325303527, -4.173034171823689 56.14549504794548, -4.174459413528068 56.145362121824114, -4.180044008665388 56.14760926704018, -4.179455954966089 56.14603794542576, -4.180183295421657 56.14466808260223, -4.182204834185598 56.144263918332186, -4.184306886339141 56.144712056086995, -4.185964663115009 56.143855928275414, -4.183314041646814 56.14188378802946, -4.185927550571545 56.140675617291855, -4.186129819486895 56.14002264999421, -4.184819641560914 56.13843009874077, -4.182155871463609 56.13686853088042, -4.185991208790326 56.13440118537724, -4.184374933024218 56.13314465036391, -4.180945737236636 56.13283679502731, -4.181053940084079 56.131621645915295, -4.182973447816259 56.131416976623626, -4.184775375687617 56.130558309566226, -4.184235485197588 56.130118512394425, -4.183142328204497 56.13013784342299, -4.182932925785401 56.12956638011884, -4.183937566608183 56.129359144242905, -4.183890709031956 56.128915477043755, -4.182680260809686 56.12881161212772, -4.181615774098356 56.126941728192335, -4.179623278495828 56.127214914524984, -4.178866968510763 56.127816893925115, -4.177207196785889 56.12729511893751, -4.176950674778981 56.126637834811234, -4.181379505960268 56.12490271564841, -4.180517626067226 56.12387210591725, -4.18229568584761 56.12392644424612, -4.181575887877271 56.12321878609892, -4.183148312590013 56.12314588212936, -4.184216753848665 56.122640880326365, -4.18457640830047 56.123780162808664, -4.186562454308612 56.12360252568098, -4.192405487114338 56.122685287350905, -4.194234560084681 56.12146651345766, -4.193334811602487 56.11922676499159, -4.194311215489479 56.11829274207187, -4.193586331738848 56.1160298523569, -4.196747598293928 56.114773877875784, -4.199503524048811 56.11258846158758, -4.199791330526453 56.11120787512581, -4.207794551405331 56.10647037919529, -4.20802636833989 56.105032241361926, -4.209927447622627 56.103147349213465, -4.221062806341366 56.09984923716962, -4.22322668986211 56.09861499782782, -4.222352171884341 56.097390528922965, -4.223372374148207 56.09695874932066, -4.222445590270572 56.09538472615683, -4.229748375892791 56.09294332332667, -4.230642350715823 56.09144429541606, -4.234506367855279 56.09348636893519, -4.234193549384516 56.09391442672325, -4.238929624073849 56.09795369246543, -4.25199407773491 56.09479518747695, -4.258719474326114 56.0937825582091, -4.257037288781213 56.09882817307408, -4.258933654172259 56.098811455104936, -4.259303495141581 56.100458325023226, -4.257897521915839 56.10143669191177, -4.258442804364105 56.102235560863186, -4.257281087616777 56.10354197523414, -4.25790741720383 56.105175170247925, -4.25675882329233 56.108081051528515, -4.25714772706842 56.108954682142354, -4.253646297266896 56.11178669491187, -4.253447737458563 56.11664337405806, -4.255115756294557 56.118482241686024, -4.255899518242152 56.11840501328698, -4.259498344927855 56.12024447774214, -4.252648290065306 56.121832834636, -4.252103870223003 56.12166109690959, -4.251702294723612 56.122343193320795, -4.257940670924377 56.124685659351464, -4.264514402917096 56.1245652706369, -4.264536905912056 56.12576913607266, -4.268820398588203 56.1277395739976, -4.271188890575818 56.12806450546113, -4.271196445664962 56.128738403649194, -4.272536969740845 56.12882158564072, -4.271822438754466 56.129257130568284, -4.272703507719246 56.13055304318631, -4.272246840282979 56.13154104790779, -4.270725688378582 56.13139827340597, -4.270793834847811 56.13200814693111, -4.26986608148025 56.13155787203595, -4.27001121799152 56.132382022325054, -4.269455968704888 56.13251804822736, -4.270613767482263 56.13277536633065, -4.269387263571739 56.134083075284266, -4.270642821832689 56.13408695664801, -4.270622649947023 56.134563646683354, -4.269311966456453 56.13444394502881, -4.270156225473285 56.134841836563815, -4.268855490918543 56.135710533381925, -4.269282083799935 56.136394705464866, -4.272249878131636 56.13650190298716, -4.275924681087856 56.13822267782429, -4.277828859404151 56.13721695680758, -4.285988249352897 56.138827676518325, -4.281885994268061 56.14293878340027, -4.283658776338272 56.14514382226489, -4.28999129331723 56.14578149515487, -4.29450378237989 56.14451143598827, -4.299913281759076 56.14424904964948, -4.30403235322438 56.1428691285997, -4.307445986144509 56.14234704884657, -4.306140805030544 56.14474405507708, -4.308438924278931 56.14548301152338, -4.3099713726808 56.14526562754107, -4.310376340232732 56.1461118399133, -4.309785190696969 56.14645542426278, -4.310408220631087 56.14691110728387, -4.309341080898237 56.14924976984775, -4.306727832717655 56.14997264361894, -4.304325541106045 56.149649008766076, -4.302164044277022 56.149851085034044, -4.302660338050945 56.153301908932065, -4.304421174838955 56.15420374104236, -4.303124020445976 56.15674435094726, -4.304807384462752 56.15823179162015, -4.300595556003699 56.15970331847047, -4.30408424319359 56.16282875407282, -4.301368172949253 56.16454201111653, -4.302406722914847 56.16682338104714, -4.30173173708534 56.16986347232899, -4.299064352970531 56.16975253457012, -4.295190767997013 56.17067083089128, -4.29474452072985 56.171587290857545, -4.295436497536202 56.174174154160966, -4.292027449284291 56.17357406125229, -4.293379574890799 56.17092610478784, -4.292810079231096 56.16981391726287, -4.291632481756677 56.169242495571055, -4.288026014661157 56.16882438942714, -4.287483683832462 56.169663855775674, -4.284247029570825 56.16845186867581, -4.281375714898404 56.168867742213514, -4.281295156941845 56.16847371392585, -4.279882541041859 56.168563588888595, -4.279489120990114 56.16916847437736, -4.278340231011643 56.1680952141037, -4.275850256551724 56.16786284577342, -4.273631440246581 56.16824180226353, -4.271543081382999 56.169862271128245, -4.271890662347264 56.17051931788158, -4.274085694641939 56.17128272610027, -4.273428367584123 56.17256349851725, -4.275596032462199 56.17392076717904, -4.275426646034949 56.17481128339761, -4.276918366436168 56.17580588172058, -4.278464752508605 56.17821604925117, -4.282952635999296 56.178672264045204, -4.284416732043076 56.179442614979685, -4.284783996767652 56.18081055919899, -4.284276316466137 56.180737011724766, -4.284246437389925 56.18114654847854, -4.282531381721165 56.1808755838025, -4.279943152807437 56.18184351332935, -4.280588115679205 56.18297351069347, -4.277255638764625 56.18384380632857, -4.272746801776932 56.18389985051832, -4.27360304884797 56.18584328745692, -4.265316621138141 56.18551914503557, -4.26391779491746 56.18666816267204, -4.26040434267063 56.18693917806677, -4.268510480174946 56.1926681969869, -4.264302027100721 56.19483036878212, -4.264854654615295 56.1976421812941, -4.263654915144841 56.19834717552365, -4.264979351684322 56.20003045436566, -4.263575299912945 56.20164688294287, -4.259600309890228 56.203139562774474, -4.259715708804904 56.20427880758219, -4.262675899288936 56.20553676782745, -4.266653348404514 56.205176314960944, -4.270254735733189 56.20663802741976, -4.272524790329627 56.20821398913722, -4.26626561150868 56.21008136759304, -4.254048962461558 56.21513973148141, -4.252588340860212 56.214159806940565, -4.241619084900442 56.216048732395855, -4.232170689039915 56.214431271041136, -4.210400435376142 56.20633875420032, -4.195471610547761 56.2032706493536, -4.194965042002424 56.20399859647855, -4.19291267777177 56.202785844123454, -4.184753403530341 56.20049487645942, -4.184620113359657 56.20183627594999, -4.18804100927713 56.20670057876455, -4.18498392259949 56.20964841918727, -4.183853222719147 56.20961447667158, -4.171093237295769 56.20553452608234, -4.170613292287637 56.203889376604316, -4.16841910237633 56.20357739842319), (-4.215004152022554 56.10446904116207, -4.210798652994202 56.104722574661146, -4.209121971151984 56.10539726934869, -4.210541678696256 56.10741083540371, -4.215924256360177 56.10555527580315, -4.215004152022554 56.10446904116207))
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>