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/S02002334 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002334 geosparql: asWKT POLYGON ((-4.484078160185263 55.47885998807896, -4.483904442564001 55.47853903125274, -4.485475742035759 55.47785635717802, -4.483177730413828 55.472749030530586, -4.482484610021491 55.47224536074711, -4.492253612855798 55.4707811670582, -4.4935124243003735 55.4701434001685, -4.492799936113991 55.469910736261106, -4.493750603987432 55.46902936458288, -4.493836364092963 55.46855841668697, -4.492338414830006 55.466986293897, -4.491314043576605 55.466348302403055, -4.490970467547047 55.46554183984273, -4.491421860265295 55.46477222060412, -4.492572775860675 55.46373127687336, -4.4920732580056635 55.463261467950744, -4.490599931525163 55.462602055683895, -4.4900376518175005 55.46186386162654, -4.488441027461476 55.46133007631459, -4.489538296284848 55.45951364530794, -4.489796792491166 55.45872099126552, -4.491141215097959 55.45791611291633, -4.492068705354104 55.45807519280194, -4.493233295481422 55.45785551109853, -4.493472714539722 55.457338285560965, -4.4901119453168805 55.45687013492264, -4.487874367963674 55.45705588684155, -4.485245956499127 55.4574913295309, -4.484327273698681 55.457798522104945, -4.482293629905861 55.458084295381184, -4.480718810317463 55.45764165172801, -4.480610100684007 55.45734633762375, -4.481361869240361 55.456547293797925, -4.481258247186177 55.45598042475128, -4.4799327802229 55.45545181701642, -4.477241588230408 55.455062319802856, -4.476709205228719 55.454492445365084, -4.477734976342477 55.45304340125851, -4.47741368905671 55.452232856454415, -4.476019981470935 55.45200041218476, -4.4748908859462135 55.451371599531754, -4.475133429211529 55.45092625619977, -4.476552970422839 55.45018383581533, -4.476080640044194 55.44986442051924, -4.4750728975461485 55.449758080285555, -4.47587524648602 55.44923668834354, -4.474919474998234 55.44854594268894, -4.475075016500794 55.44812572967333, -4.477450292877439 55.44799943837685, -4.478875935194528 55.4473818055652, -4.47913174533186 55.44664495731253, -4.481368445927761 55.44652948735799, -4.482575061960019 55.4467315106652, -4.483608668691845 55.44591684061078, -4.483183825841576 55.445271104357914, -4.484750810941234 55.44393684333917, -4.484992838759562 55.443083410470585, -4.485469106245608 55.44261082368735, -4.486351192639836 55.44260727499961, -4.488679501635724 55.443573821404286, -4.490152389763683 55.44378652997211, -4.490504062642811 55.44326971962136, -4.491673663088102 55.44238389591496, -4.492812272233969 55.44271214362832, -4.493880489808711 55.44235059828793, -4.49428501347917 55.44187045036619, -4.49410771595288 55.441140602020674, -4.493370883135342 55.4409147345824, -4.4919989118128365 55.44089685807942, -4.492101878496803 55.439621085067785, -4.4928302121702615 55.43936175232696, -4.493360275553949 55.43854107898197, -4.492756874746047 55.438123728443365, -4.489003544608575 55.437847830042834, -4.487824298262693 55.436745564459834, -4.486392510860509 55.43584976611233, -4.485227948635342 55.43583389005818, -4.483735891482465 55.436745079101875, -4.482724576307884 55.43645101776741, -4.482450586150607 55.4356628945994, -4.481881583615692 55.43485918643244, -4.48075709979048 55.43462493272758, -4.479746391813844 55.43471734098017, -4.478382575307902 55.43527980594211, -4.477515615883527 55.43514367061722, -4.477177358612613 55.43461480875558, -4.477315064424092 55.4341886606287, -4.479375099548529 55.43267908549651, -4.481345904891031 55.43211777502509, -4.480327824532829 55.431360924028226, -4.479306240913282 55.43107693158728, -4.478132246355349 55.431412631333785, -4.477242847786485 55.4310684163532, -4.475477464031307 55.43064395782255, -4.474572778959095 55.43025688848139, -4.4730089753313065 55.43045121802989, -4.471850229608972 55.43037397685541, -4.4709951157115215 55.42971712343876, -4.471530621919693 55.4291553057977, -4.468364956363914 55.42845253470796, -4.467249358204571 55.42891368862186, -4.466749728870271 55.429696778646374, -4.466246022730087 55.42992639212482, -4.463961988712397 55.43044150327598, -4.462922290121124 55.4310062520948, -4.4612425179147985 55.43040548889635, -4.458052200006198 55.43019461669154, -4.4562796963895 55.430720106330114, -4.455905116699777 55.430924477388494, -4.454614796553977 55.4309171289639, -4.4545861840623155 55.429081362539826, -4.4536568159132255 55.42824161835801, -4.452747864964967 55.42890252902811, -4.4518506257460935 55.42883513353939, -4.451769102899972 55.428336111702954, -4.452247473967271 55.427486098705565, -4.4519493590253525 55.427113663521844, -4.45203185294773 55.4264558523667, -4.452579083484307 55.425901075537254, -4.454084763469271 55.425382790941036, -4.453355254405099 55.42488419014137, -4.4526756342458995 55.42353337770098, -4.453197120103816 55.42281911926699, -4.453173342903699 55.42243578920011, -4.452442938543996 55.4217942849485, -4.452962200191581 55.42152859586532, -4.454540280898285 55.421593999848454, -4.455135007064699 55.421245890117156, -4.454794492442697 55.41998625292059, -4.454984383250835 55.41945571595952, -4.45437558867056 55.41896548399345, -4.452580643622824 55.41863747231647, -4.45146330343858 55.41794709710686, -4.45126994661618 55.41714740583899, -4.451700720402211 55.416678558667144, -4.4524037613900065 55.416498171229094, -4.452979056702894 55.41591406428719, -4.450523875024119 55.41517950850531, -4.450386329696396 55.41346906480763, -4.450433253864787 55.41195626472894, -4.452745674106256 55.41143619805072, -4.453288236149873 55.41037635614396, -4.454139789838934 55.40971389100436, -4.456076879027529 55.40857120129199, -4.456549982386016 55.408684836948964, -4.45804831422007 55.408339221480205, -4.459036362590264 55.40852788244324, -4.462584689620431 55.40809691155978, -4.4643884846093265 55.40738102902842, -4.465472252744729 55.406544816256535, -4.465979488620211 55.406445598232125, -4.466451402499279 55.40580508366556, -4.465993493780719 55.4053774808613, -4.465466491623837 55.4040434306305, -4.466379812353717 55.40346502101895, -4.467199013529024 55.40355635071839, -4.468104997976695 55.40306346678004, -4.469276848033638 55.403464956030156, -4.47668792938281 55.401260235389486, -4.478362962775254 55.40106988221562, -4.480864821385295 55.40096699364783, -4.484363367226361 55.40029291111427, -4.4845136779746415 55.40026457727558, -4.48745012828945 55.401624146610416, -4.492339430072302 55.40098073780987, -4.500867886853396 55.39911398118493, -4.501913792406098 55.39911864282728, -4.501561709472978 55.400845369020566, -4.502355425361967 55.402681672463125, -4.504439911469347 55.4033212146613, -4.511826155017952 55.40620600589924, -4.512926424653031 55.40671551753332, -4.516697914507655 55.40891024967887, -4.520619957362843 55.40753417483379, -4.5212795957914835 55.40737405579391, -4.526287151217922 55.40865401860163, -4.5278011120505655 55.409155747277055, -4.5227020794773845 55.410779267081324, -4.525202379792204 55.413639064510804, -4.52712000678397 55.413951792308985, -4.52827854781851 55.415193438804195, -4.528346284304334 55.41558574071281, -4.530205295518059 55.41732703153816, -4.531351287839899 55.41851767485906, -4.5305794690951435 55.418927356480026, -4.531117910450471 55.419291036631705, -4.531708871636749 55.42106754176993, -4.533559158736637 55.422098861741205, -4.535126712997715 55.42384612136816, -4.537017077301675 55.42392196431613, -4.537872876990503 55.42368757605876, -4.539504240944597 55.42354493871315, -4.541023517832597 55.42409314124625, -4.542431573879873 55.42374656858533, -4.542366668341745 55.422786130138036, -4.543134978024918 55.4221013932287, -4.544831625546893 55.42132901690637, -4.545561877469607 55.42071696550484, -4.54707175160846 55.42153674693763, -4.54996896874969 55.42267005125974, -4.552151830362208 55.42331124002137, -4.551191162551357 55.42446656229042, -4.552044422515647 55.42544290442442, -4.5519083789301575 55.42605337982556, -4.553518329799543 55.42665526711531, -4.5536913336187785 55.42705794294508, -4.55304664301285 55.427319701278755, -4.55134935694786 55.42843716229953, -4.551255942802827 55.429194161466384, -4.55314490860433 55.43029629596892, -4.555590927239139 55.4307215970596, -4.556423893842994 55.432358105119654, -4.561330599574695 55.43245317206661, -4.563072241545821 55.43298297466379, -4.5656303594710534 55.432695609925155, -4.568188303355214 55.43337898640854, -4.571058027159443 55.43350744223306, -4.570253729707675 55.43134907801853, -4.571693887613332 55.430159248901376, -4.574590281504716 55.43021514845437, -4.575015330397478 55.42921743529752, -4.579359958423532 55.42993754314565, -4.580029617213663 55.42963373351944, -4.58226515621589 55.43095303022491, -4.585090106144543 55.4322598275115, -4.589885412987612 55.43468514189782, -4.5910545512131735 55.43676188738504, -4.592133294588401 55.436365837116135, -4.592873970334261 55.43532553782725, -4.595026234829474 55.435706730949946, -4.596567367174582 55.43609374639074, -4.598578789182976 55.4369103375861, -4.602018101661263 55.43936940929142, -4.6014962822694665 55.44152614793336, -4.6024932301681565 55.44252013043722, -4.60380984244881 55.44217266843544, -4.605078898371911 55.44166829865345, -4.605724871444731 55.442832069985684, -4.601045200656119 55.44404630576007, -4.5999640467751375 55.444437836989685, -4.600128836282657 55.44490260461126, -4.599827144109909 55.44620255084608, -4.599352511682237 55.446680057107336, -4.598836842828119 55.447976413061575, -4.597845705011216 55.4504603973819, -4.5972179856118744 55.45255793049037, -4.596359727690971 55.45246490801663, -4.595325300422153 55.455188069673575, -4.59487173791604 55.45598870470389, -4.59146349098537 55.460456466198906, -4.590396931353558 55.46204310039352, -4.589314441471314 55.46298467833191, -4.591094461368564 55.46376117791738, -4.592078069380042 55.46475804522782, -4.593251568481009 55.46528148699463, -4.592984993673983 55.4657994967816, -4.5957032876041355 55.46599353158731, -4.5959103050454875 55.465773408432234, -4.5982087906407845 55.46559876763986, -4.598921239390384 55.46512520605931, -4.600456783257272 55.463456615836236, -4.604788610420634 55.462690289829304, -4.607595767727964 55.462315872309055, -4.6110867929979795 55.46247511035745, -4.617699817618973 55.463430403369905, -4.618900921702375 55.46341721507201, -4.618114749722653 55.46610917914064, -4.6180606443750305 55.46744068158382, -4.616212289585771 55.46772292381628, -4.61609667040859 55.46741078820314, -4.61434747386343 55.46785159229575, -4.613601494878671 55.467778408699964, -4.6104900563393825 55.468113242419356, -4.609548892377724 55.4683017190352, -4.609877250019324 55.46882903474814, -4.607942857361929 55.46924781784465, -4.607520354357421 55.46860064303681, -4.607339227267298 55.46825394109478, -4.60573367517305 55.46784525600857, -4.605954962246285 55.46710130474879, -4.5989663629188104 55.46675197805809, -4.5975357463195055 55.466431038609, -4.593309935496391 55.46617013606842, -4.593346154212481 55.467203065597964, -4.591447367401811 55.467683762100116, -4.59165932817287 55.468306146808246, -4.590636076304063 55.46858184258012, -4.590169016062221 55.46845690158412, -4.588285945509418 55.468937206053276, -4.588165509392633 55.46927233734974, -4.5870916955872545 55.46954673551356, -4.587334616844245 55.470324669352486, -4.584382934773551 55.47070961203283, -4.584284533200705 55.47041505390426, -4.584488240928597 55.46844272303467, -4.583029618734659 55.46861903090244, -4.582861715520424 55.467532756343594, -4.581728373516391 55.468590383039924, -4.580249490468817 55.46847776534477, -4.577695918382328 55.4659608277973, -4.571237495126805 55.470285479795294, -4.569423788103684 55.47037751875421, -4.568753193592885 55.469555653742106, -4.5664676262228046 55.46750027288341, -4.565927967771679 55.46796102755318, -4.5635470730089 55.46881992446971, -4.5603927029145845 55.468733168427065, -4.5569346919248535 55.46982122128458, -4.557134950293515 55.47095859472928, -4.556308043379408 55.47211743494434, -4.554009539900712 55.47352272630002, -4.552711390791722 55.47449362672402, -4.552010582700427 55.475892497177924, -4.551809275855609 55.47791913873247, -4.550618684321456 55.478842827731334, -4.54803931157665 55.479094321708196, -4.5473498344325884 55.47895587196844, -4.5460557686190475 55.479504147299124, -4.544546494942642 55.479661385361936, -4.543014650354648 55.478731452058646, -4.539423315092157 55.478752111941965, -4.535568795645259 55.480342135459395, -4.5338449992822625 55.480117176849284, -4.532541327296094 55.479532944189046, -4.5322590552416075 55.47908036879891, -4.532931889950922 55.47772714245751, -4.532351380112507 55.47755938728715, -4.531851566526731 55.47692255317279, -4.531589098812767 55.47603811448519, -4.530496232819089 55.47553039661198, -4.528992923160914 55.476215884789006, -4.526077891103727 55.47694218523762, -4.523099507146344 55.477800343193024, -4.521198908026608 55.477233630589694, -4.52266051642538 55.475933886877925, -4.523720971262366 55.47558148946258, -4.524479012901368 55.475969302418605, -4.525721804130947 55.47494786294474, -4.525849312069834 55.47429952751959, -4.525330775747833 55.473989061120434, -4.527056473579044 55.472785920266006, -4.52615189911338 55.47236363993262, -4.527358867899772 55.47151443239657, -4.528155614440798 55.47135414395254, -4.527627846666144 55.47090955281121, -4.525811876304981 55.470845245137156, -4.522529252394292 55.47246283672503, -4.522670167627426 55.473043513844125, -4.521294773550117 55.474183319931974, -4.51845600857284 55.474583353497245, -4.517921299330396 55.47438762246442, -4.51747466868167 55.47358784379126, -4.513558509369549 55.47272456668081, -4.510807919395609 55.473266428060455, -4.505896853491799 55.47517380995872, -4.505202764974377 55.47645540547136, -4.50359418342455 55.47779166640242, -4.500630107863134 55.477969132635714, -4.498393034569169 55.4793810852646, -4.4938539917114655 55.48018377522533, -4.4890942177654924 55.478995343576806, -4.484078160185263 55.47885998807896))
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>