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/S02000098 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000098 geosparql: asWKT POLYGON ((-2.167521952499591 57.67771964691023, -2.165884633422828 57.678997289969224, -2.166306209589163 57.67951771061978, -2.164781615745983 57.67979815343445, -2.165136441974751 57.68039051851845, -2.162535266997774 57.6836634341943, -2.160911811541501 57.68438409207487, -2.160440379091506 57.68396253005743, -2.159804044925247 57.6841699281682, -2.160249443435891 57.68249866211873, -2.158255055679632 57.682761657317045, -2.158251331101035 57.68190835219379, -2.156945343655018 57.682368069740455, -2.15692541882814 57.68164053715579, -2.155567662930729 57.681758981759934, -2.154298967761559 57.683107881831575, -2.153310906745252 57.68342345706159, -2.152169450731491 57.683155368321735, -2.151198991076026 57.683677506641416, -2.151755406050555 57.68439541977823, -2.150751630042403 57.68498047209377, -2.149326118024236 57.68497317799373, -2.149365097622816 57.68629352615635, -2.147587635520854 57.68635849044869, -2.146851949549907 57.68690726198764, -2.146162015993852 57.6863331981057, -2.144452734512035 57.68667648555398, -2.14497530556858 57.68734057689411, -2.140486010215406 57.688746880853145, -2.139118467943794 57.69076939294724, -2.133364041167283 57.69497929069938, -2.127513087922313 57.69597336935094, -2.126677589099967 57.696908366244536, -2.124884844543005 57.6975658528134, -2.124350754435636 57.69836579697409, -2.124905947300947 57.69882334648047, -2.123029981201091 57.69971442121762, -2.121115805041426 57.69924021998372, -2.118537058743939 57.70073371144658, -2.118635627966301 57.70009588016996, -2.117395880301558 57.70064495121869, -2.116855811198708 57.69965740645423, -2.116217857251743 57.699523265190905, -2.115984473686182 57.69999055352803, -2.113750053304292 57.699022511383966, -2.112974817178193 57.69790042264769, -2.109550462799481 57.697238760570954, -2.100644576406131 57.698018639553524, -2.082448438044495 57.700735479753625, -2.079327211280651 57.700468012728024, -2.06774758862045 57.698777125332384, -2.065916970650089 57.69770023341026, -2.064792961683801 57.6977008159338, -2.063804264463176 57.69832108613574, -2.060313889961636 57.69780184075654, -2.058752447725449 57.697039087568996, -2.058246967330814 57.695674019808294, -2.056350408201569 57.69505511577672, -2.054974972615037 57.69512757663763, -2.052289504371864 57.694113733762684, -2.051986661304009 57.693485096992276, -2.040897299102406 57.69208795861214, -2.040174797485833 57.69097440226955, -2.040811303943437 57.690210706016856, -2.039450631929074 57.68831588563552, -2.034821830953317 57.68966909154608, -2.035056096135535 57.68862652881945, -2.031830241204178 57.68155442712734, -2.029305491277232 57.68167657267335, -2.028566427544522 57.68073064708704, -2.029850694894651 57.67964797595622, -2.028619037773204 57.67867813190833, -2.029795250953851 57.677030685388516, -2.027224676621866 57.67695322564053, -2.02659364873747 57.67623442274846, -2.028435525415219 57.67563267394785, -2.028914774931175 57.6748980901713, -2.02553840575042 57.6739555405162, -2.022486981680156 57.676634174055444, -2.01053753523105 57.67838933122266, -2.005147781100343 57.678257714445394, -2.005961198889575 57.679806424612266, -1.999466592704854 57.68056146019956, -1.996900950520199 57.68181894527231, -1.990529322000141 57.67976170645048, -1.976831870461467 57.67669702029459, -1.965230739668306 57.67535604046719, -1.952203746307814 57.675288941980014, -1.950391629857263 57.67632120098868, -1.95017212842365 57.67745287961497, -1.948980577183482 57.67824284970856, -1.949516382173388 57.67878199204756, -1.948544395529366 57.678404353586714, -1.946934411282198 57.67862824738864, -1.941833707951542 57.68077276977203, -1.939972945755835 57.680457532464104, -1.936052358971685 57.678641202834655, -1.934191286108988 57.67858636273951, -1.93407496856745 57.678002451757905, -1.933034335731463 57.67858575824813, -1.932466427068932 57.67740878024915, -1.930269936403174 57.67742555633847, -1.929900175162418 57.67789242805609, -1.92996818724658 57.67739844089881, -1.927087149693266 57.67596864622904, -1.925226098449569 57.67597654103247, -1.924272389615328 57.67503284015232, -1.922544833223248 57.675355165632794, -1.920837086564474 57.674258273705725, -1.919981414854145 57.674554156259546, -1.919915859353071 57.67386247491363, -1.918876034465109 57.674041466185045, -1.919480967433241 57.67341308790128, -1.917906698953074 57.67268452219961, -1.91918467897219 57.67094277568674, -1.918369057592164 57.66834638528696, -1.912428101831483 57.66427345627049, -1.911745785218125 57.662260954542845, -1.909705215522734 57.66059779801619, -1.901117936336015 57.656863754116905, -1.898876625304018 57.65536194476134, -1.898985224210215 57.65245176299159, -1.895224618195893 57.649206105437464, -1.893826365779706 57.64608808722272, -1.879359785341829 57.633706511755356, -1.873427964568506 57.6298922172275, -1.847647659617222 57.620225797261746, -1.861490468080969 57.61182522364404, -1.863348505384439 57.61175538750793, -1.884336679948141 57.62215987311636, -1.892641172003039 57.62189770882415, -1.898188425344695 57.61978246187611, -1.917953240635996 57.62221303616671, -1.919828747021288 57.6219986540464, -1.925715171035556 57.625199953141696, -1.922628311216018 57.62811735648199, -1.925235484581823 57.62895864158422, -1.925420636251804 57.62860390485529, -1.930668346192987 57.63072793251085, -1.929718619062181 57.63290008153599, -1.930805660119529 57.633053961024785, -1.929084659250482 57.636550342560675, -1.93433232218655 57.63750149495599, -1.937459703217153 57.633900445237416, -1.937232914880561 57.63228152374272, -1.939049389303907 57.63086584076393, -1.940468103436706 57.63085474054163, -1.942521082878449 57.632528948588956, -1.948574407900921 57.63328636671412, -1.948993757063122 57.63279250757543, -1.953401661622102 57.63370560957304, -1.952927721259206 57.63490912575399, -1.957535088850756 57.63731327118783, -1.958153433421786 57.63714088639301, -1.957115593966891 57.63377064804211, -1.958267879352968 57.6304444025566, -1.959526750022746 57.63017883376958, -1.96028843205138 57.631161150671346, -1.959905091256735 57.63294103403952, -1.961811761012574 57.63330989413859, -1.961661410432069 57.63394248931192, -1.963930203498265 57.63402819570355, -1.963376095765423 57.63548317983482, -1.966005333752581 57.63543891251343, -1.970164098892488 57.63406913717098, -1.969745228634504 57.633230191276574, -1.97092755979336 57.63329184410531, -1.971194189043826 57.633742020755115, -1.972300556221082 57.633414904423766, -1.971111400736232 57.638072071670365, -1.973423251563111 57.63734500265057, -1.976288373350175 57.635549109520895, -1.9822367311525 57.629352269081664, -1.981534446113117 57.62735810336609, -1.980345844093782 57.62697167970766, -1.984448762278081 57.625211710138785, -1.983594991949793 57.62495111933905, -1.986660167426405 57.62141243372631, -1.984182863171184 57.6205678133811, -1.979043251117576 57.6209802645524, -1.97942976336009 57.618339525601954, -1.984670500531567 57.615340158218636, -1.982830074554053 57.613660229138596, -1.986880797507244 57.6129151691856, -1.986446579058398 57.61035515948716, -1.994630436625382 57.61043659631093, -1.99623726423968 57.60862221378518, -1.997475665922222 57.60870308226123, -1.997274929462328 57.60736470699298, -1.992723231947496 57.60683457948873, -1.996338025908182 57.604795741261825, -1.998212247485956 57.60281067149451, -1.997877711636234 57.600690829176884, -2.005105763679974 57.60101410022511, -2.005975553386341 57.59954993323796, -2.008150376392979 57.59867851475717, -2.009253940492067 57.59611846594496, -2.01681595716303 57.59683625967949, -2.022825200790823 57.602000167248804, -2.017974909969823 57.606025054113545, -2.019715201914836 57.605970890725416, -2.019197298563708 57.60754288798341, -2.020252391920425 57.60899786050853, -2.021859221403535 57.60939281459863, -2.023031307785824 57.61034472725594, -2.022647132718869 57.611512501170765, -2.02350083881424 57.611754865055126, -2.030110583164817 57.61031627512983, -2.03133250561947 57.610549520232766, -2.032438114080435 57.61168101527312, -2.030950981170875 57.61450184962412, -2.032075539304371 57.618031624136265, -2.028342618558578 57.61799658714657, -2.032048742175669 57.625558818260664, -2.031550291876963 57.629951298844155, -2.032306197283818 57.63260088921649, -2.035437047239561 57.6319084097597, -2.03938954148316 57.632356346263585, -2.043508702207161 57.632040604127205, -2.047241153230835 57.6306020869122, -2.048649118673773 57.631607569574705, -2.054960473104805 57.630437271132415, -2.056737363323485 57.631658082259605, -2.056513171370295 57.62749038747842, -2.063543407021012 57.62625644962525, -2.066802400563481 57.6230301163873, -2.074099062496423 57.62168766716402, -2.079013742437515 57.626059076182685, -2.080671557585737 57.626165806767695, -2.087331438000963 57.62454455297036, -2.092109927897106 57.62725381621756, -2.094251159466745 57.626497721420236, -2.09443935283134 57.62804254228995, -2.103308527870379 57.626221157051944, -2.10339352631261 57.62667020196306, -2.107860649604607 57.62553468724195, -2.108120817095171 57.62856151312779, -2.113627600486304 57.62789197804825, -2.120600844177451 57.6251818274017, -2.121128941809693 57.62288184436372, -2.126034963976879 57.62308363318653, -2.132038165610728 57.62573626113464, -2.134399399684892 57.62585952318926, -2.137137924355072 57.623916397683466, -2.141870046028229 57.62668670051744, -2.14350699339415 57.625696801521286, -2.145419459412394 57.62663775778186, -2.146152775708883 57.62579256971259, -2.150185965579318 57.6253387206863, -2.15107647267814 57.62609217154299, -2.150718825258609 57.62867052494751, -2.146106886960677 57.631038299311804, -2.146593131229242 57.63119043965899, -2.14585827541922 57.63167633285857, -2.149127781566361 57.63266955657827, -2.147858068386729 57.633407599695175, -2.149020470251791 57.63508592551562, -2.150041936077429 57.63505776517143, -2.150731478748482 57.63575756669053, -2.150081286222964 57.63647922632742, -2.152523173220309 57.63967170306751, -2.147472269847552 57.641501120123486, -2.148003858427187 57.64452753207403, -2.150571768267298 57.64571015802689, -2.150224987685191 57.646923183194396, -2.154088640528588 57.6493976500154, -2.154025327083936 57.65026900597501, -2.151196612438599 57.65093711798497, -2.153012621234108 57.65247987892573, -2.141218102645577 57.652799024766736, -2.141275507267009 57.654631341238165, -2.142369376168979 57.65583374611782, -2.143896576250523 57.656415858705955, -2.144325969367885 57.659047180972614, -2.145081524113971 57.65940561124569, -2.147742196589202 57.65844141410698, -2.150305221273565 57.658204856801305, -2.154062498441204 57.65902670210762, -2.153883514902116 57.66029342008148, -2.152396201643499 57.66130123923168, -2.146903551005863 57.662394603061564, -2.147217255325983 57.66949023305262, -2.150349251187768 57.66883083833823, -2.147519296278474 57.673684603006116, -2.151630264319312 57.67452406974684, -2.155555610753281 57.67113298339298, -2.166290115954472 57.67234988154637, -2.167954871442507 57.673443511341326, -2.163920446705996 57.674760228815835, -2.167521952499591 57.67771964691023))
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>