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/S01005501 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01005501 geosparql: asWKT POLYGON ((-1.183281260440995 60.164363021957314, -1.182679388798964 60.164175698158154, -1.184466010251245 60.16187903879809, -1.183346934665304 60.16123462287501, -1.179436400215969 60.16127332999637, -1.176449221643771 60.16041075756094, -1.174442326383924 60.16069458943312, -1.171335795562135 60.15957075740205, -1.164454485913929 60.1595455207208, -1.162963132831765 60.15726435629922, -1.159918893467646 60.15796343897196, -1.160269126400924 60.15765138560004, -1.158868912837347 60.15675355991722, -1.159632051175045 60.154378893425395, -1.161042996823829 60.1541453914525, -1.162114506547589 60.15309261365048, -1.164480555826301 60.15354753613042, -1.167424635926008 60.152515486534035, -1.168153633439466 60.15075114918336, -1.167746219183809 60.14834214694869, -1.168437037105554 60.14808608542411, -1.175915970690997 60.14716304181348, -1.177555554149419 60.146409989823205, -1.176738069951444 60.14597390923257, -1.17592151897982 60.14623764543783, -1.175127526200864 60.14564182950736, -1.174346041837413 60.143696251805615, -1.17285950607382 60.14276124886488, -1.170855833058861 60.14332057556086, -1.170053280446295 60.143032763581694, -1.16994978902182 60.14093908696036, -1.17174011504064 60.14134537799339, -1.17262788411003 60.14112644043114, -1.172511816589581 60.140012285315684, -1.1752118199287 60.13788307403595, -1.175132522865335 60.135296547957665, -1.183204534217912 60.13501432162813, -1.186366931015467 60.13598558382193, -1.187565105971761 60.135579888179244, -1.189168071382072 60.13628111279813, -1.190568833992436 60.137887985207705, -1.193735705291895 60.13648857162547, -1.200933426468439 60.1388577989785, -1.210072630478883 60.139146059722705, -1.210027869731821 60.141013473927416, -1.213856894892162 60.141269703838304, -1.21464443727034 60.139200173234116, -1.222587894372661 60.139049471694406, -1.228408778976587 60.136506458097934, -1.228318738573812 60.14112125350064, -1.230104699664337 60.143286652704845, -1.225822908586519 60.14542569686271, -1.223764135511939 60.14514197521217, -1.220583753534724 60.14347867370076, -1.218281098487872 60.1428971709698, -1.20745977174215 60.14553480116886, -1.199172847265931 60.143996974133984, -1.195471127030126 60.14444444968906, -1.184958599812476 60.14402778693717, -1.185533496145544 60.146185018622006, -1.18451081067714 60.149428854671186, -1.178426599294277 60.14774663649397, -1.175874637984924 60.149297116969365, -1.174956897494395 60.15144510783669, -1.196510893753375 60.15655059924899, -1.193422677986395 60.157549953534556, -1.195009021648206 60.16014402309329, -1.191453892453432 60.16094470789659, -1.193161025895775 60.16181658428177, -1.193212718944559 60.162913324163746, -1.195765625760333 60.163420114377246, -1.188419152637938 60.16596191031167, -1.183281260440995 60.164363021957314))
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>