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/S02001166 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001166 geosparql: asWKT POLYGON ((-4.127816536086049 56.04650954793701, -4.128076854106462 56.04769179006589, -4.126184537706108 56.05065416035505, -4.126300945787666 56.05306065323402, -4.123857510406746 56.054477745910596, -4.121003906564863 56.055155924894116, -4.118270253235925 56.05714407423931, -4.119245419122942 56.06086587737632, -4.121350815490984 56.06176432474249, -4.123479517839261 56.06191642957372, -4.12311878986664 56.06324371712663, -4.126787776555607 56.06656858715751, -4.128295265122064 56.06681219982979, -4.128660815509514 56.06761471027608, -4.128066388837516 56.06907186090003, -4.126394880700725 56.06935231577538, -4.125608691066329 56.070830721751314, -4.123684960305403 56.071492936275206, -4.120496475395075 56.0754570648013, -4.117457783357904 56.07601251696903, -4.11750629381725 56.07865382270183, -4.114385245140435 56.07947122582538, -4.114289938224966 56.08037154367471, -4.112293437841082 56.0808910303575, -4.112900949040769 56.08141983543148, -4.111058561112248 56.08181982715911, -4.110248810720938 56.082588579807705, -4.109244032709307 56.08772825511757, -4.106928967915997 56.08978085152215, -4.107238406458738 56.09073716742584, -4.10574919273683 56.092020735246265, -4.090950269462719 56.089774009109696, -4.062382495373486 56.10685318255379, -4.059768862731484 56.1118125533357, -4.059321798847469 56.114884469675566, -4.05594998504914 56.11590224797791, -4.055545104182905 56.1182634991746, -4.052535838645265 56.120659086779845, -4.057519223868133 56.12149278606492, -4.057975936642925 56.124019425220574, -4.05670363478876 56.128515989713215, -4.055186639445474 56.12903551396046, -4.053442499138955 56.12861519575312, -4.051028869733037 56.12982357298609, -4.050468993995173 56.12897913182439, -4.04808262428844 56.12918948198846, -4.0464636824141 56.12839853726193, -4.043317425202175 56.12854049796216, -4.041382795778652 56.127862552416666, -4.036838311167416 56.1298607310345, -4.035599303825544 56.12925209883535, -4.035481473195739 56.12610871741591, -4.032021875591833 56.12610278793777, -4.027775483627939 56.12734984229037, -4.027079890444639 56.129113655724304, -4.023422689867839 56.13025202198176, -4.024449192427611 56.13110690335101, -4.024548837034347 56.13239035122164, -4.023207215389109 56.132583085906255, -4.02186065679229 56.13114930990066, -4.014477734533991 56.128133635716786, -4.014249246801506 56.127148836780464, -4.015424375716333 56.12531437636129, -4.018131518085971 56.124164812449976, -4.016853033105439 56.12279276998494, -4.014564346934921 56.12271330755448, -4.012240165586257 56.1216458548874, -4.006874723540903 56.120870462516024, -3.998103467469787 56.12297178409626, -3.994248902670874 56.120302182299035, -3.991033994219245 56.12003953147805, -3.984941853516422 56.120757688313056, -3.982607548993837 56.12260148308668, -3.98562344042429 56.126803654277175, -3.984645662384539 56.12843694611987, -3.981716747981057 56.12811553454894, -3.978388249471998 56.13003809857566, -3.978304553077212 56.13185471862792, -3.977041415588701 56.13295332273951, -3.975073844367028 56.133497034468725, -3.97275161620994 56.13528652097436, -3.966828810969131 56.133071452949544, -3.9627498401787 56.13260614509927, -3.963368801418857 56.13180550076749, -3.963022138412696 56.13125880838128, -3.965549138956049 56.13100438923309, -3.965352667893539 56.13037290740836, -3.96064666141753 56.12702551125987, -3.958250554375859 56.12798663598087, -3.958077740459259 56.12676898663574, -3.95496986139011 56.12580097490742, -3.955765650715807 56.12540915384176, -3.95557749287198 56.12469165118127, -3.953234516441359 56.124512749589265, -3.950451641099695 56.12492419528085, -3.950610302051268 56.125484989503775, -3.948583713646739 56.12614455136548, -3.949053849080712 56.12653252997503, -3.942818101276618 56.12826633006753, -3.941404184971304 56.12732592292262, -3.943074053952953 56.12474656766991, -3.944744140138279 56.12402967667152, -3.943751606725385 56.123686061388376, -3.944911735405219 56.12288643314337, -3.944166372115808 56.12264436617542, -3.943185648172644 56.12297802239437, -3.943138542136075 56.122143624762835, -3.946971845382337 56.12296899522063, -3.946808093614426 56.1232891791255, -3.948844898227762 56.122402061620676, -3.945841852031299 56.1199601689041, -3.948215175298121 56.11853882645512, -3.947450202450693 56.11836217719023, -3.947886895732891 56.11683575706147, -3.947056312072477 56.11672644103091, -3.947379899551998 56.116268878193935, -3.94515078464564 56.11359965229631, -3.94565690894862 56.11342992293729, -3.949641355485935 56.11143497578396, -3.956322026053845 56.11289317039131, -3.957935147478422 56.11200921246071, -3.960908841354885 56.11147776309278, -3.964684377230735 56.109327315663506, -3.963358814866522 56.10862559013715, -3.963328851691052 56.10913083358077, -3.960910026661549 56.10930418026854, -3.962099389238956 56.11047280672141, -3.961614394836872 56.110698007614914, -3.958158818506591 56.10816169559985, -3.957157821866031 56.10837746676532, -3.957152346472483 56.10700261993709, -3.958617276455016 56.10576620844621, -3.956628722029981 56.105240577392756, -3.954484025282672 56.10671977978416, -3.954963338107232 56.1060128433704, -3.953835067378117 56.105644287550255, -3.953360425537499 56.10452848030883, -3.9544314860538 56.10478820102944, -3.955355856871663 56.10446956976558, -3.953939551643282 56.10278247863717, -3.951926289221315 56.099381843601634, -3.949741455701623 56.09978535116613, -3.94798978737838 56.100713207805995, -3.948401980227529 56.09996083004352, -3.947467193316121 56.09959814568608, -3.946940080568227 56.09839328329658, -3.948673201996969 56.09769195041275, -3.945803185466111 56.096254451755684, -3.944718254873008 56.09482472075696, -3.948125374537026 56.09282157697031, -3.953055182217573 56.090882813452716, -3.955459119272315 56.09264205009443, -3.956750610216898 56.09243287294524, -3.957299815364757 56.09153449680766, -3.96011387295266 56.09155274547308, -3.955581173329852 56.0893420676283, -3.947738636142287 56.08810012527153, -3.949303594653673 56.08376189252832, -3.946393086669899 56.08082793061481, -3.943015581716165 56.08049111879184, -3.942458987948899 56.07760261998472, -3.945516810304737 56.077189600902784, -3.94604946400394 56.07436842256834, -3.948281688886947 56.07338961375897, -3.947845887447179 56.072713515994984, -3.948787748956988 56.0716382378081, -3.945086968052231 56.07038088971247, -3.945237447699478 56.06887530095729, -3.941565065874438 56.06858884238862, -3.942679121464737 56.066144944083526, -3.947345161682592 56.065972538660304, -3.948572257665658 56.0654589058354, -3.948063988602605 56.064936727997, -3.952433990252506 56.063995953086724, -3.953119945236658 56.06231360422139, -3.949258073758495 56.061224401816155, -3.948031341097254 56.05919483842442, -3.943998169151961 56.05596939125897, -3.945491039210235 56.05595483866826, -3.944956228724088 56.05107459061006, -3.942718714112896 56.050328018672644, -3.944166633101561 56.04942452698972, -3.946241609669333 56.049814178578536, -3.947768068427682 56.049196964406796, -3.95422115564801 56.05011038711884, -3.960906480929229 56.048000290360434, -3.972655155385529 56.04884641532545, -3.975673185691033 56.04757593947922, -3.979322683666811 56.043904809338656, -3.978053073462072 56.040743902538985, -3.978549231525986 56.03945985075368, -3.982378009587308 56.04024310572629, -3.987243562822933 56.03964358556612, -3.992800982168996 56.03814301269664, -3.994414089218295 56.03891675077616, -3.99540187527822 56.038460422215884, -3.995383070917307 56.03685211006541, -4.001334440498196 56.0355334932108, -4.001684480836055 56.03485381274989, -4.002754384090002 56.03474657219885, -4.007573960907297 56.032044096646615, -4.010496879842048 56.03115172751625, -4.012201267471543 56.03151934583398, -4.013116418189346 56.0312168351328, -4.014440945510342 56.029838211535875, -4.017611724710915 56.03001104160223, -4.020053539953958 56.027904111957795, -4.03134261863481 56.03017205075825, -4.035955613553542 56.02908046762153, -4.038551563916114 56.0296127335774, -4.042243994942716 56.02963251877819, -4.043953562155076 56.03070058509906, -4.04494622154849 56.031564830601525, -4.047734505129157 56.03117706760794, -4.049705687683184 56.03051523886744, -4.051821788908385 56.031073184390884, -4.053552134778062 56.031619536809764, -4.055028316037393 56.030705249848324, -4.057826466840058 56.03050580753053, -4.05898491759935 56.029354131391614, -4.060120430308477 56.029092109882455, -4.060513756149092 56.030335117939586, -4.062148427338265 56.030841151504426, -4.070187686031074 56.029957413978494, -4.071493920778773 56.03067239788976, -4.079514904159995 56.02970162289952, -4.080365456704929 56.03059495084609, -4.091811844138348 56.03034738701326, -4.092198140755158 56.03096093014728, -4.093098554484625 56.03069401567382, -4.09889032686278 56.03390274884914, -4.101768923595809 56.034887217736205, -4.104699826757008 56.037689315325366, -4.106848027348518 56.03795494925272, -4.108636116127804 56.03886889816509, -4.116621752335663 56.043846709479176, -4.127816536086049 56.04650954793701))
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>