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/S01000439 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01000439 geosparql: asWKT POLYGON ((-1.997125907124968 57.351784749847496, -2.005618036654591 57.353509320758384, -2.005551661861039 57.35414709635793, -2.011585738060972 57.35822482186476, -2.013165020140148 57.359015141396256, -2.017453838804284 57.36005660466671, -2.015408811930612 57.358790315452154, -2.011020329680729 57.35727270785672, -2.008626450447929 57.355143998638155, -2.008061175469711 57.35421881720427, -2.010553289347724 57.35161364019928, -2.009256821915192 57.35059869892598, -2.008160037786319 57.35049996627884, -2.008857855436952 57.35001485187241, -2.008625070422551 57.34943099278021, -2.00771117404801 57.34962867285227, -2.002792428182857 57.34873959818515, -2.002077850320759 57.34776947977126, -1.999917728970022 57.3475090033464, -1.999535558021781 57.34700597093007, -2.001645712416452 57.34559566928073, -2.004702850201742 57.345002725226735, -2.006845896311472 57.34348454282651, -2.008573248561639 57.34122976783635, -2.008290374691708 57.33929850199182, -2.005382794076926 57.336711638444015, -2.006611892949971 57.3359660093034, -2.007741487468051 57.33619051099104, -2.008024241547036 57.3378163669661, -2.009452881824952 57.337906093076334, -2.0125087937744 57.33598351379515, -2.012574977684807 57.33523794162453, -2.014103086207587 57.335058121748254, -2.013455651092391 57.33607324244624, -2.014136980252682 57.336782803448216, -2.014917701857643 57.33675576261795, -2.016495239335748 57.335569841209725, -2.020719173441102 57.33538350158732, -2.020863341879148 57.33464396182486, -2.023719997504013 57.33414041653342, -2.027108305897419 57.33392413956504, -2.028506536788337 57.33443282393682, -2.03418358026871 57.333275644739835, -2.038335761444468 57.33303190494206, -2.04099415925977 57.33379459645445, -2.044728883225976 57.33172728504818, -2.045123812595074 57.32873588863436, -2.04920847179647 57.328114540454564, -2.055317343144636 57.32635137149273, -2.062228074867251 57.32774943890423, -2.061276462067692 57.32475864920027, -2.072030152858313 57.32122270755432, -2.07486877534592 57.320870716084556, -2.077591148038147 57.320768120955876, -2.08126462900147 57.322339907586695, -2.082549782395693 57.32509905691309, -2.085072811226224 57.32478967769225, -2.086198384933111 57.32324387488545, -2.088156571627645 57.32273049504999, -2.089386540891304 57.32322367750278, -2.09436851504939 57.32338172013071, -2.096538134533044 57.327539084795276, -2.091874671372473 57.32880017104908, -2.096054700207187 57.333189607603735, -2.094942918013696 57.33360365896539, -2.094482373220727 57.3353646244168, -2.100696687195226 57.33606044782966, -2.104091287149494 57.338159639730236, -2.106685018901049 57.338938960927074, -2.107368933391219 57.33989953190282, -2.111025767153113 57.34057005532665, -2.115651146469912 57.34277563487662, -2.116979837607439 57.34263966327403, -2.117264051830334 57.34319632869903, -2.120868681263029 57.342995290426096, -2.12319541885078 57.34236391313303, -2.122565594667813 57.3449766681348, -2.127752133407311 57.344443086569804, -2.130454389281924 57.34748604950078, -2.131334078243488 57.34721565193063, -2.135729501332104 57.34959140363957, -2.130698882356299 57.350274289389965, -2.134208690691391 57.354188850779714, -2.126803696112387 57.35625741339967, -2.128994478628201 57.35760210389618, -2.131470477801588 57.357464796849364, -2.132036676360044 57.357769616402734, -2.137880504971314 57.355984761010646, -2.138829408685269 57.35639692223167, -2.141352868671369 57.35566650697683, -2.144385621829584 57.35768417275656, -2.1480382317991 57.3567008051387, -2.148390777932756 57.357571714951575, -2.154639542409775 57.35742045842986, -2.154708219449498 57.36188478330158, -2.155713197365865 57.3636800848807, -2.150614927304015 57.36493489461241, -2.143786162164147 57.3658860751462, -2.13755135768667 57.36572238184319, -2.135121486554969 57.36498843685119, -2.131897362251196 57.36525235963116, -2.130054246374216 57.36586510451143, -2.12792584923808 57.3657325486847, -2.129451583228828 57.364680009757, -2.128898923739774 57.36352180965392, -2.127620081720223 57.363855474222134, -2.127356007313192 57.364403687211954, -2.125759478531644 57.36421665837763, -2.125342011021985 57.36366913133465, -2.123995453182491 57.36365250105585, -2.124440934513705 57.36266396377902, -2.123206367428308 57.36133573957331, -2.12237626834596 57.36164196204823, -2.118843737799683 57.35900443728006, -2.116867629837469 57.35958118469022, -2.114639186403963 57.35921494511261, -2.112297320471805 57.35979195398237, -2.104192640757241 57.360339488354754, -2.105237429560675 57.361307162458196, -2.099954107222779 57.36226364616817, -2.098306086403531 57.361384641023804, -2.092903617608582 57.36131689273118, -2.095973868844299 57.359446180386705, -2.088576433800733 57.35910129339199, -2.08829224438005 57.35842778952596, -2.08682541885865 57.356668201397696, -2.083752758484354 57.35758652488745, -2.083169523808574 57.35692219304038, -2.080213804314091 57.3581637300638, -2.079266760011773 57.35832602148634, -2.077533477164872 57.35610837499275, -2.077951937749001 57.35750043589065, -2.074478455032306 57.35761932098423, -2.07571165361631 57.359199539533016, -2.072595275489273 57.35994644479433, -2.068847198207719 57.3593112869361, -2.068092873613007 57.36027683833105, -2.068849629263307 57.36060479518433, -2.068653514486426 57.362392460615446, -2.069700621570658 57.36232002154429, -2.070468744653765 57.36412511528125, -2.058133064129053 57.363556547467155, -2.054377824316506 57.36467208758117, -2.051931452929515 57.36280472399237, -2.047642992593957 57.36320156133796, -2.049077246375603 57.364107995105016, -2.0500024670473 57.36409563458114, -2.050440902989576 57.36559624382963, -2.05226897110365 57.36634376740988, -2.046184860178822 57.366759361529404, -2.046907680186816 57.36917592398583, -2.046099874346324 57.36919992300493, -2.046429198011069 57.37011578419982, -2.043804212272978 57.370441755210756, -2.044298017778478 57.37171213684049, -2.04276911336273 57.37395829762685, -2.037365151752559 57.37467605300151, -2.03796409382294 57.37498128472984, -2.034907741919612 57.37844050683815, -2.026762571507226 57.38334705820395, -2.021274394211767 57.384219434030435, -2.020576129970039 57.384767494202585, -2.024902033308446 57.38668899577124, -2.026731727149096 57.386643701869616, -2.029444007521127 57.38782880312704, -2.034002462782347 57.38850134632464, -2.03538409592239 57.389480072178486, -2.037064007875804 57.389246037332164, -2.037578955835641 57.38852727074297, -2.040904743265904 57.387493221514696, -2.044247519721424 57.38690821153844, -2.047774689303826 57.38749078908248, -2.051500876069776 57.38757015611215, -2.054696299137258 57.38866468842194, -2.056423738296044 57.386993146429326, -2.072969329976165 57.39268860593181, -2.073995385223762 57.39001117328232, -2.07968844312476 57.39173235978592, -2.084223625698831 57.3961398831409, -2.084525060561504 57.39698405103319, -2.082412110446309 57.397048334792345, -2.082486762037024 57.400641346097714, -2.084851959482536 57.40162786084097, -2.082825664713543 57.403335916951114, -2.081046463261746 57.403983828011576, -2.078078369327252 57.40903395082593, -2.074397132204465 57.40897886928274, -2.07200148469191 57.41339514426254, -2.067528723393464 57.413857791796595, -2.067589962086228 57.413218023229646, -2.066944729204717 57.413106134633296, -2.067503521767691 57.41147843879952, -2.061298587674777 57.410889972774605, -2.061655818563521 57.412061254991556, -2.060058171451965 57.412250664187304, -2.054480493705894 57.4112830865369, -2.052372073439621 57.415119567800346, -2.049861489859298 57.41737523068921, -2.049664417506841 57.41936046709508, -2.048866218022298 57.42006142588846, -2.045104285172272 57.42066466968809, -2.046506559019183 57.423592491335214, -2.044859791933848 57.42493149983798, -2.043220589793282 57.424434888467864, -2.042313165308265 57.42499506640568, -2.042810888509357 57.42536814451769, -2.039699752741071 57.42750366691757, -2.038618355248678 57.42724739808306, -2.038035193282107 57.42786576407915, -2.037062141065749 57.42783543335123, -2.033875319699977 57.43067483950261, -2.031892843073879 57.42991183957312, -2.028062915284937 57.430397817944375, -2.028014316830335 57.432167400307556, -2.018787169059328 57.43112715062503, -2.019551990391711 57.42878257440205, -2.021565616956661 57.426455746018455, -2.014005251952613 57.42545077628546, -2.012173241260738 57.424426954620586, -2.007111013698585 57.42282844568651, -2.001649514141737 57.41884934994588, -2.000550712218095 57.41866072501569, -1.998886016757325 57.41249864724539, -1.996689084358936 57.409058265102026, -1.99442556593831 57.408573128688204, -1.992761124719256 57.40888743782956, -1.985205492535686 57.40705433545535, -1.981644684782526 57.405392087519836, -1.984508674851654 57.4017724614126, -1.982862277480358 57.39951760964741, -1.979967735805026 57.39808894681108, -1.980234886560012 57.39634635357339, -1.977524340902743 57.393884658838395, -1.97667729983213 57.39156698047477, -1.976777530423664 57.39090228298599, -1.979856110806646 57.389142205267966, -1.985263212502015 57.38730149792751, -1.985530089078982 57.38541516779169, -1.987094006070672 57.38429250114933, -1.982172730016406 57.380034123922094, -1.980777455788536 57.37670135039402, -1.985818530246231 57.370441076543656, -1.988795213813683 57.368653817150495, -1.986934405223357 57.36499768473152, -1.984840158900251 57.36403630581286, -1.98364401148089 57.36231148051718, -1.98725205256009 57.35973385393169, -1.989562612454256 57.359204084214674, -1.99006150152554 57.35827890409562, -1.994732118923775 57.35751564745897, -1.995613380296247 57.3542549553287, -1.996693687615142 57.35359924908072, -1.997125907124968 57.351784749847496))
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>