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/S02001280 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001280 geosparql: asWKT POLYGON ((-2.1314769816854966 57.21793478751276, -2.1314906681708745 57.217763248852286, -2.1287782592371647 57.2105984563783, -2.121002586657039 57.20707591315694, -2.119092962151409 57.205209292517715, -2.120465502111586 57.20481273158257, -2.124881995888528 57.20395502971059, -2.1241194743702896 57.20361443562544, -2.121701037199826 57.20303291660968, -2.119790264363363 57.20163614122957, -2.1188571340344 57.20077059834338, -2.1217869062043206 57.20012345690085, -2.1213315111220603 57.19912429486617, -2.1224374085113773 57.1981941852754, -2.123205802366669 57.19806626960439, -2.124126982156536 57.19862100952091, -2.1242848288587566 57.1990732430763, -2.1252770069012645 57.19893293869609, -2.124764798692286 57.19822164122875, -2.1263539262447875 57.19802840691738, -2.126407153403684 57.19764813212911, -2.1275856572281078 57.19771847804657, -2.1273551365828927 57.19691385853777, -2.1288769191418657 57.19670467099217, -2.1291643188917253 57.19630821815565, -2.128983588469692 57.19573764575037, -2.125615494082769 57.19576185364867, -2.1246388869038784 57.19288566186089, -2.1238297730228486 57.19126317260668, -2.122986189229793 57.19068577965854, -2.1221975150532764 57.19047403255482, -2.1210236790385157 57.188265607138376, -2.1232500444783478 57.187190431362886, -2.1226300845662345 57.18663712341517, -2.123098866165481 57.186426588190066, -2.1248784828014977 57.18785981904023, -2.1264518935790666 57.18724584571179, -2.1283445894116455 57.18713852426078, -2.1290119758739627 57.186763087731734, -2.130087949465819 57.18667943985075, -2.1291350121188297 57.18480805271984, -2.1282556738532796 57.18407235280607, -2.125043460786818 57.18388536754983, -2.124691395626183 57.18341864017498, -2.126950521816085 57.18301024259895, -2.127189248544998 57.18351620858712, -2.1290978767318314 57.18371553788242, -2.129016732650428 57.18409852036102, -2.1355824339765364 57.18495597396478, -2.135631768497332 57.184289752850276, -2.139077578181471 57.184851309918564, -2.138954718441535 57.18532360882458, -2.1408234287626 57.18532152286511, -2.143560953897554 57.18490015201057, -2.1450316300679972 57.1844762592861, -2.146746932658694 57.18373387737223, -2.1473239627150114 57.184109047787636, -2.147968432249229 57.183913726453476, -2.1493092307530404 57.18273752322184, -2.1494222652712565 57.182054682730524, -2.1489752747177397 57.181965385146206, -2.1491969984635335 57.18105084348153, -2.149735434913534 57.18044568316852, -2.1517844772332055 57.17900926600311, -2.152100221875789 57.17758692283651, -2.152143781904925 57.17612264435049, -2.1543109508849465 57.176245756339554, -2.156248336010811 57.17679131768776, -2.1576738857264472 57.177526136664255, -2.1585880428183892 57.17856700878651, -2.160576524967603 57.18019031496028, -2.163727491157934 57.18021187602346, -2.163781485229637 57.18138509887615, -2.165211336635412 57.181477004694315, -2.1656254694957675 57.18183071974777, -2.165571124721514 57.18254975284469, -2.168247351948863 57.182447205985675, -2.1706461573899247 57.18219826109776, -2.172806527220423 57.18269189842026, -2.173609219182957 57.18307227830607, -2.174135862161506 57.18383455267145, -2.1732434093085806 57.184852832311535, -2.1698723763656775 57.186600968131195, -2.1685464197546316 57.18969289813364, -2.16800381792122 57.190439212376894, -2.1693321935828904 57.19147046585848, -2.171224054957007 57.192680596099514, -2.172558660568295 57.19146606662782, -2.1727875322200356 57.19087287589828, -2.1756913627647667 57.19082875837668, -2.176246400402825 57.191038728683864, -2.177998315764514 57.1911800889899, -2.178267932900486 57.19164672120625, -2.180411188552136 57.19344923099199, -2.180061631586636 57.193812168955375, -2.1801997691254074 57.19434532475799, -2.17540071035442 57.1952123109508, -2.1738595667215375 57.19477608008092, -2.1718518593585414 57.19603038933108, -2.1714791761926526 57.197737651839034, -2.171818340189747 57.199497846032905, -2.1717455986571106 57.201653853363666, -2.171368828456494 57.20248978285308, -2.171209106772816 57.20373863037026, -2.170589402638692 57.20574267109637, -2.1698995751507972 57.20691138980281, -2.170100444059912 57.20739619620018, -2.172043936958477 57.208848780926, -2.174410253332446 57.210049154017824, -2.174641338869682 57.210276191436805, -2.1717548739330668 57.21254475623112, -2.1711318560206734 57.21207310568436, -2.1694327916047915 57.212326036385946, -2.16809927302348 57.21178616010663, -2.16690898487183 57.211060132404505, -2.1664516111537098 57.2120587440267, -2.1650778714099475 57.21324900146002, -2.161075742899544 57.213874027713054, -2.1603448227149378 57.213690814381074, -2.1604104968252082 57.21318319495957, -2.158116452514032 57.213320852328145, -2.1546410569319385 57.213639588718316, -2.1514458842218316 57.21286556690359, -2.151976118779878 57.21217144335091, -2.148090291263146 57.21127420689586, -2.146048262316117 57.21232490979799, -2.1456983630758244 57.211758494153486, -2.1438936909451574 57.211717460230986, -2.143014988276537 57.21138699558371, -2.1402191629585716 57.21277801575684, -2.1407137920145094 57.21311162641574, -2.1365726336411837 57.214749301342025, -2.1377251398947137 57.216570675264535, -2.135004535525734 57.21695810324583, -2.1319118336099883 57.21795041463931, -2.1314769816854966 57.21793478751276))
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>