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/S01000336 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01000336 geosparql: asWKT POLYGON ((-2.123099313382608 57.11604018235971, -2.122996748967222 57.11498926482326, -2.12451119487831 57.113604375255385, -2.121143389390354 57.11376039279635, -2.122082841260652 57.10823489213365, -2.119430874600341 57.10496761196339, -2.11888010473228 57.1030906729551, -2.118636771514506 57.09042958691202, -2.09574909612104 57.088324354341324, -2.089295094940405 57.087305056917195, -2.085910214465948 57.08624742443405, -2.086899468722612 57.08595029907278, -2.087984779708246 57.0844224114129, -2.09014477063952 57.08389086817445, -2.089088891465718 57.083891625237854, -2.089763707094158 57.083226393537885, -2.087339224969555 57.08352455377044, -2.088177394193833 57.08216751531839, -2.091736373828003 57.08036833207693, -2.090810872340356 57.07967730932942, -2.090940968742501 57.07891365026529, -2.092604763958371 57.077996150207696, -2.093194852687727 57.07650451549093, -2.094317392573228 57.07685401587086, -2.093704218651502 57.07572260353843, -2.096371559901983 57.073888019271855, -2.095842508538183 57.073403338470115, -2.09381312994025 57.07313538363833, -2.092705945706779 57.07228281239768, -2.094170440682281 57.07095221532943, -2.096149687826864 57.071013591145494, -2.096299402470041 57.0715075476825, -2.099714943232733 57.072088772611146, -2.100586896763241 57.07128857602871, -2.103833178891668 57.07030676204998, -2.103655434762355 57.07161844488551, -2.108579244292424 57.07157349655591, -2.109349759051567 57.07068449994988, -2.111076590168655 57.07143240938861, -2.1117505648763 57.07069519342957, -2.114654310299944 57.071051889593974, -2.114441795161418 57.071662936814455, -2.116405353294019 57.07195756657935, -2.117958245655769 57.072764591714524, -2.119618525760663 57.0710472394685, -2.121450236988643 57.07136886655, -2.121795639931814 57.07108107058123, -2.123554176194825 57.07355271353602, -2.12625846795451 57.072713561719006, -2.126386953382348 57.07296297058648, -2.134223635869988 57.072141259538114, -2.133000011368989 57.068150138456666, -2.136051904437876 57.06642886660301, -2.140253141789157 57.0673606849857, -2.141493635162217 57.067245508450455, -2.139293970441936 57.065539398248404, -2.141188213502023 57.065005074716936, -2.14206482852139 57.06565626788841, -2.146461950938629 57.0653287946178, -2.148246773951553 57.066255523559164, -2.154017930971785 57.06758422106487, -2.160237882116815 57.06681333940537, -2.165056670106609 57.06740219919365, -2.164635418348453 57.06850008226888, -2.165500821671254 57.068729659005406, -2.166280455150137 57.070670716517036, -2.171747133170872 57.07086537158191, -2.175956756781816 57.072147106563754, -2.173684974383547 57.0729945159862, -2.169957056228263 57.07292483109311, -2.170467138445631 57.075156458727236, -2.172178798659233 57.08146026818816, -2.166612819575441 57.08369561490735, -2.162211784893798 57.08459970797789, -2.162636887801919 57.08748273429478, -2.155889077684949 57.089163559825145, -2.157407984625504 57.09172944590253, -2.153602618392898 57.09294126564323, -2.157668245919339 57.09403273848372, -2.155627899255868 57.095620992128396, -2.152778205027219 57.09668527094135, -2.154546339156177 57.098003215329605, -2.156934795357102 57.096895322917554, -2.157630895421141 57.09758614563461, -2.161804875747574 57.09722149597003, -2.162438836103212 57.09877475206681, -2.166121673748331 57.099281954422665, -2.172914234651742 57.097673764692665, -2.175016196476455 57.09892847733063, -2.175345078196618 57.102161930763955, -2.172888003914744 57.102668400886294, -2.174174281163566 57.10436985753895, -2.171383467797112 57.105482968179835, -2.172527169389243 57.105955798653795, -2.17358647454239 57.107572878757146, -2.172765774965703 57.10831893235817, -2.176392394576384 57.1096314756698, -2.175212323193492 57.11249266366218, -2.171346032827866 57.11416767089599, -2.167229621882426 57.11439002451082, -2.160272209978877 57.11300674720358, -2.149761048204257 57.11459179894256, -2.1449490434403 57.11700492455064, -2.14118556329315 57.11738651414983, -2.133738212613949 57.117394692142625, -2.129577381744402 57.117524830583456, -2.125550422870751 57.118184699078654, -2.124061665065802 57.11742262355291, -2.123099313382608 57.11604018235971))
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>