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/S01004245 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01004245 geosparql: asWKT POLYGON ((-3.125530954966887 57.5813129126111, -3.123434884132668 57.58004698693946, -3.123692392864497 57.577008400714284, -3.128566880151074 57.57295846162317, -3.13425376573333 57.569799348188425, -3.136835133458669 57.56733271997744, -3.142210943940602 57.55856155506115, -3.139875745117409 57.553363432468174, -3.140706856130752 57.55108318129316, -3.133063198947564 57.54977758555699, -3.12772096032657 57.54993321586365, -3.12196449963567 57.55074807235056, -3.122613074940992 57.549017540012244, -3.118855269517611 57.54689494069107, -3.11776572928669 57.54514389277413, -3.117681717433784 57.54349173469823, -3.117091581631415 57.54276932205136, -3.116088377104043 57.542742263046286, -3.115160343480472 57.54080111835907, -3.118805809540659 57.54092157457882, -3.13013032511457 57.542482584676904, -3.130992387436959 57.54442421432742, -3.140491722445626 57.54152701103508, -3.143288529813643 57.539615239081556, -3.149669878486086 57.543330227710825, -3.150266628514487 57.54106102303351, -3.153238475514879 57.54047694826962, -3.159137485507735 57.53948866197761, -3.162410054909583 57.53838956374319, -3.164659286534977 57.536635066454885, -3.173415738201034 57.54038981199938, -3.176481477312675 57.539103621549486, -3.180862051583515 57.54129056374503, -3.180770406540096 57.54000681603414, -3.181943776771108 57.540130583489116, -3.183252182984146 57.541843110404436, -3.186787232876364 57.54163924302953, -3.188462095369105 57.54023107996803, -3.190248135438443 57.54018730183966, -3.19337477288017 57.53873842456564, -3.194332477344903 57.5353336885147, -3.197794737054338 57.53496849847303, -3.20012241750075 57.533625844506986, -3.201084656557814 57.53241293051975, -3.206357947108234 57.53126667562639, -3.206451592076807 57.531746161563234, -3.207446591738176 57.53163629944528, -3.207440537214325 57.53103283043664, -3.208877002840668 57.53067662454219, -3.20890568365485 57.53002955188359, -3.20997141080647 57.53004399833797, -3.209643589450674 57.52961823279575, -3.210636667250765 57.5293481947347, -3.208455969152628 57.5272986126148, -3.206212491784104 57.527872779222704, -3.20617763433816 57.52732472923262, -3.200798809903322 57.52930743111326, -3.199067235161482 57.529467627008216, -3.199769146122355 57.52999097063816, -3.194092819106561 57.53158094503053, -3.193995033363632 57.532165783469885, -3.192445508352374 57.53279129567736, -3.188905382914682 57.52973444552326, -3.188362220633752 57.527412891884694, -3.189436890047263 57.525543228353754, -3.191206978282504 57.524520404540695, -3.197592042744136 57.523696392431624, -3.200715668489766 57.52169936707444, -3.20154553110229 57.51951750766945, -3.196766806816073 57.51430768587188, -3.200841827097929 57.509283226864056, -3.199988518014294 57.50770003466823, -3.197981065310829 57.50632505912965, -3.193926009750517 57.50655505108178, -3.190979529432798 57.50730156590184, -3.186770498673956 57.50720647983416, -3.183232025265989 57.50617070290582, -3.179301659828602 57.50384489656685, -3.178482387547663 57.5022445418006, -3.182508810645534 57.49980832001722, -3.183222286463532 57.498651765870285, -3.182818812620466 57.49701160347138, -3.180969749395665 57.49555566529742, -3.181662297961598 57.4937525161557, -3.189118870729907 57.490637091233026, -3.194194735342535 57.49128085116289, -3.197524158551273 57.489533465293185, -3.198852700464657 57.48629582185223, -3.19588380686581 57.48324271986562, -3.196702753334517 57.482273732587736, -3.199560206325587 57.481420115811325, -3.20319924388511 57.48100814686645, -3.209299101823888 57.47885667377296, -3.225658873101477 57.47195912076372, -3.230448734821826 57.47042960367339, -3.227758318580647 57.47005694258986, -3.229124332086878 57.46830712192866, -3.23417443377049 57.46924596549259, -3.235995111235876 57.468356737978645, -3.238639170197303 57.46568966055514, -3.241444955685372 57.4648535539994, -3.244799245069334 57.465934426805916, -3.249295703474204 57.46823463122998, -3.254555497440165 57.46992519360383, -3.259145254984972 57.469088896697045, -3.261396883919176 57.46911130751731, -3.263879841330598 57.46810724414363, -3.265949521628216 57.466244867871396, -3.268290219402943 57.46643694627189, -3.270006205676356 57.468324169699706, -3.270068685806754 57.47060534311925, -3.26928434758329 57.471547522521426, -3.269875221093971 57.47271840775349, -3.271930272559503 57.47330111670394, -3.269848105072933 57.47426305428334, -3.269719127973723 57.476870341829404, -3.266782937566546 57.47828332584657, -3.264952870308122 57.48034094124635, -3.267588164696996 57.48082653625542, -3.267361348710659 57.4824638047617, -3.266213797642369 57.48304128953946, -3.267815747164017 57.4854686939663, -3.266370333926805 57.48563593475519, -3.264605342351702 57.487656953277224, -3.262412949901607 57.48890066496651, -3.26634835145768 57.48933733505366, -3.266330348308371 57.4902628103777, -3.264660048434717 57.491168929947214, -3.261684014015695 57.49630132340619, -3.266182372786683 57.49803496833188, -3.273759505120244 57.50334874614162, -3.274693741695605 57.5033393097113, -3.283564125918409 57.506438491700266, -3.337263821881632 57.51986804461269, -3.36026270219446 57.52501233987671, -3.359146686016008 57.52553642593991, -3.358551124240411 57.526584928590864, -3.352244534057019 57.5289973610879, -3.350750464375965 57.52878877284483, -3.349982021809996 57.529686363097504, -3.348743324969469 57.52960977180994, -3.348071337443843 57.53040750045408, -3.347810310971666 57.53012281597201, -3.346531122980217 57.530307157079875, -3.346853155457701 57.53088764889991, -3.346021720987995 57.530986354011624, -3.346065229717216 57.53171355399923, -3.34491374361864 57.53128563793621, -3.34407617712915 57.53167186757682, -3.34292966937392 57.53137863307226, -3.342811299238528 57.53179313357764, -3.340424311855953 57.53228564909594, -3.338472481829724 57.531893122635786, -3.338365817708048 57.532172742069015, -3.3363734604527 57.532041134301046, -3.335600325703513 57.532821898221954, -3.334562556821073 57.53276100788069, -3.314462569926175 57.54462347410649, -3.30715773040401 57.552919157115745, -3.301835781141465 57.5569178602546, -3.301615481113771 57.559174965349854, -3.297253531701905 57.56294796190718, -3.294471999031096 57.56511455159506, -3.273749418429744 57.573913351587386, -3.272715475711466 57.573034423533684, -3.270729057175682 57.57265017524777, -3.270108875922338 57.57212639688384, -3.264430995489888 57.5723271029768, -3.263927597658125 57.57179314222213, -3.26067386796892 57.57150226326171, -3.256884083444492 57.56827010796581, -3.253637017370586 57.566703352279596, -3.240936172458686 57.57273082181481, -3.240569430725027 57.57472871609821, -3.205727244978495 57.573862385869674, -3.201147031157093 57.56933355938812, -3.184797052552167 57.5755337091623, -3.178546860499909 57.57564608409905, -3.176280177214692 57.576394852399375, -3.169302032134857 57.577277117978035, -3.164321141238148 57.57735007959713, -3.159600710290381 57.57667485129311, -3.158522788397366 57.577484246146795, -3.157278285430434 57.57726209249831, -3.157629553760887 57.58095095447387, -3.150473792863575 57.58048632650005, -3.150382550705413 57.58077461848775, -3.148913560632522 57.58032984673691, -3.148177624741059 57.58085756133208, -3.142332821813385 57.58059614518588, -3.139341222653672 57.581728089917135, -3.132738637316872 57.58137546414287, -3.13179885741396 57.581711263037036, -3.131405153689683 57.583467209607214, -3.132895881374627 57.58355013543276, -3.132742995709149 57.58497655206404, -3.13099378683528 57.585232942325995, -3.130144139403232 57.584431735726625, -3.127053292907571 57.58420900688802, -3.126712458775588 57.58139152784538, -3.125530954966887 57.5813129126111))
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>