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/S37000034 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S37000034 geosparql: asWKT POLYGON ((-4.180429146101957 55.904913528813644, -4.177688016080674 55.9026487751639, -4.177512476778696 55.90037364015856, -4.173354547899815 55.8970184956837, -4.16311882005241 55.89623629258335, -4.160275245399937 55.890085043723296, -4.1616334580978815 55.887677749083124, -4.169687124032594 55.88622569629254, -4.164394273007761 55.883584063898454, -4.158430991530485 55.885873601700474, -4.156561819159134 55.88380073297444, -4.152906860713538 55.884616026857636, -4.149960147920996 55.88655665037488, -4.147786537180164 55.88362617943953, -4.141490747711156 55.88288658843276, -4.132683742164612 55.883904125741985, -4.128715224167308 55.883352723715724, -4.12870151283435 55.88523307360557, -4.118789029968458 55.88564114007716, -4.1073039834051706 55.88745449622584, -4.10614858632018 55.88448782049912, -4.102883403188409 55.88510716898706, -4.094956407914656 55.88524057509551, -4.092518407266761 55.88254816149733, -4.089342463162357 55.881524616463935, -4.081933546992354 55.88415577948563, -4.078432126673699 55.880542834154724, -4.080134120634383 55.877182540803624, -4.079316818399586 55.874094912461, -4.079833153672872 55.86865443490411, -4.077144472019011 55.864038255997166, -4.071716789621897 55.86127010653044, -4.077183943455356 55.85948119364833, -4.085962727901706 55.85870085634057, -4.088250294681472 55.85350609726893, -4.083606626479834 55.850895058082045, -4.083105892455819 55.848853541307605, -4.076683254202586 55.84647451673072, -4.081445770018711 55.84391715946013, -4.0846311793920655 55.84523829175279, -4.093789109433003 55.84375804824244, -4.097171915399696 55.84235946681853, -4.102657230243971 55.84233507990549, -4.107994455007316 55.83760003211527, -4.107044618673894 55.834656785691536, -4.111973932169993 55.82940554074444, -4.110655697199499 55.826030106554356, -4.113566459751757 55.826238055435816, -4.130121496562775 55.83327512715344, -4.14151830665799 55.830019785630746, -4.145381633447986 55.82838147098639, -4.154791634902589 55.82801827551366, -4.160775414122862 55.82717364627555, -4.172520712849905 55.82297089492626, -4.175371946959405 55.82382641379426, -4.17885509135867 55.830186142529605, -4.179384899164935 55.834716298199346, -4.186164833508483 55.83680772901788, -4.1908939292334635 55.83508061152865, -4.191222940074068 55.83244238964062, -4.194295888535541 55.831742304171506, -4.198702622362292 55.833571598090856, -4.1993288028613245 55.83532914563672, -4.195785592164497 55.83734899125846, -4.195555962939506 55.83928086531177, -4.203020918293225 55.844134462118525, -4.205547774972705 55.84424371138277, -4.205965375493781 55.84180250990006, -4.2033683976507135 55.84081904903175, -4.203519222709452 55.838234296028425, -4.211821515057951 55.83700410883781, -4.2157868855755956 55.835598924148, -4.219536928945055 55.833306020658696, -4.223262781298732 55.83521241382385, -4.222590697614082 55.838714364672796, -4.227590340117093 55.84081952925048, -4.231316265847753 55.83821933027728, -4.233648795510025 55.833609528267644, -4.230743436530124 55.83259639540001, -4.228882589886794 55.82446685061036, -4.234966486867555 55.8238449882841, -4.233765575865193 55.81866043167343, -4.228277854061327 55.817382187074216, -4.225501521201909 55.812176595173554, -4.21454339430116 55.815076146941784, -4.212190159042729 55.80952742450955, -4.211343709123963 55.80031619569453, -4.209048581324568 55.798869125784435, -4.22394378352432 55.79194799905429, -4.229936906015333 55.78591842853875, -4.2245205532137815 55.784272178086425, -4.226711921035752 55.78127914520122, -4.239349314558475 55.78511903608442, -4.24344831395414 55.787142884600826, -4.250754439153611 55.784889998881425, -4.25203391275124 55.78672069487358, -4.259339639289488 55.787032334247755, -4.261554541688557 55.785700088231366, -4.264642567687078 55.787654688633424, -4.268259300905547 55.791644082553276, -4.267352809473376 55.79311943477163, -4.263434652893247 55.79341996987784, -4.26031900955689 55.797989029576286, -4.261469134752639 55.79955416989535, -4.265307273531198 55.79998053661916, -4.265607471552846 55.80413082731414, -4.269303916606373 55.80397907571293, -4.267097479426738 55.80681403183237, -4.263037570817986 55.807473048476425, -4.259933873122195 55.81012033504973, -4.260696157110343 55.811018561672014, -4.266462927140255 55.81096807720312, -4.27197288382163 55.80873090027468, -4.27793652828073 55.80973592624521, -4.283050909113389 55.81176743528231, -4.290292080878492 55.812523419267556, -4.292775376825225 55.8138235334566, -4.29773089490306 55.809751261691815, -4.3055475309091 55.81044617013118, -4.307994260796993 55.80939536420492, -4.312643144164378 55.81108487633153, -4.315056714465973 55.80864509790436, -4.327274033399404 55.80763519912006, -4.324300528787552 55.80524769193852, -4.3228089486654895 55.80051422799253, -4.333462290063733 55.79287865421694, -4.3437089812737755 55.79502861102553, -4.355853581200005 55.79503459738497, -4.3605731196247355 55.7944334938633, -4.371613175260389 55.794932743875464, -4.377307671341144 55.798577832768316, -4.373787988839625 55.7996012668102, -4.367796683079157 55.80336232537044, -4.37428454535148 55.807285281907845, -4.375627957888171 55.812045306113525, -4.372189413875469 55.81584075782695, -4.368335332567233 55.81606512388796, -4.367467594323591 55.81770501524651, -4.372951419834071 55.81889000851321, -4.381428541393856 55.82315256694583, -4.377573001716842 55.82679171599517, -4.378607635004464 55.828882959854326, -4.37402882983707 55.83756822381792, -4.370254289968633 55.8394502945966, -4.368600076695694 55.846888107737556, -4.374893720094402 55.84865865129439, -4.373934982224244 55.85030572441056, -4.379230615659062 55.852673240973765, -4.380886960034554 55.85634676959745, -4.376559148266516 55.85646640823715, -4.3646861036984275 55.85532914376736, -4.361437545372251 55.85991063163431, -4.354884426954105 55.86522750442171, -4.356271448091806 55.873190145038805, -4.353448310316131 55.87374384400454, -4.364009949215096 55.87804864971319, -4.368146737120594 55.8801333424006, -4.383331268407447 55.88685692501738, -4.393200496429304 55.88915139111506, -4.390264282130858 55.89076514118697, -4.387184709851088 55.8904141195718, -4.383202682137164 55.894539382027446, -4.378942638581667 55.90009357185989, -4.375849319701435 55.90107007722203, -4.380695418759766 55.90400163435092, -4.389967827075877 55.91051685244942, -4.38853768394162 55.91268370415809, -4.388048654072335 55.916362001084295, -4.385167356325956 55.91789007523184, -4.3800067166524235 55.918792505630506, -4.379632170421955 55.920985552887636, -4.370754725415167 55.920713774420285, -4.364177608615956 55.92110271001419, -4.357032075079264 55.92059351920763, -4.351191461691374 55.917445342230884, -4.348688017258182 55.91516447933389, -4.347600830267073 55.911927153956405, -4.3494161922581585 55.91003477073496, -4.348941516655058 55.906207035681085, -4.340635557098258 55.90418037588373, -4.336795257671927 55.904198538416495, -4.332730134134357 55.90292214798071, -4.327968506741215 55.8999762699413, -4.319765576216369 55.901268114931966, -4.319535065578012 55.903033965328746, -4.313999395769538 55.90703161932967, -4.309472785398989 55.906946728612546, -4.3065775095662024 55.90569334818183, -4.3005809707640505 55.905529647055324, -4.30098400180318 55.90879451618922, -4.2966378617006145 55.909437330061955, -4.294117053515245 55.91135014406802, -4.290454148140002 55.91218862918251, -4.2919912650461205 55.91431972029262, -4.301279165886193 55.916369042519634, -4.2991399386874285 55.92075357935455, -4.298064975161725 55.929173277731884, -4.294906496366488 55.92841707746547, -4.284280132572802 55.92760172567136, -4.2779719095111135 55.92862364159613, -4.275825675540383 55.92961329773733, -4.270494274948894 55.92804818152265, -4.267493786976872 55.92792916865012, -4.269837157800857 55.92299859869388, -4.268809019488801 55.920222455176045, -4.263642722075092 55.91846973442212, -4.260266472863522 55.91292811003755, -4.255564737798813 55.91306017122075, -4.251098904964496 55.91102365779853, -4.249163264484382 55.90653024515198, -4.2467740238072835 55.90367097717764, -4.23771502519178 55.904768893996604, -4.237607156204253 55.901423930123016, -4.235586060963044 55.89749279297113, -4.228212818090759 55.896389837146735, -4.216598939912472 55.89773908976422, -4.209076912060093 55.89744297380167, -4.20075455502314 55.898405453592325, -4.1988541523200515 55.89812037300138, -4.190862170943987 55.8995875966201, -4.190072563580578 55.901244499674256, -4.182462015587166 55.9029677207868, -4.180429146101957 55.904913528813644))
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>