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/S13002873 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S13002873 geosparql: asWKT POLYGON ((-2.563889994565528 56.6593987467788, -2.5581467875095476 56.65954408457143, -2.552135581872901 56.66099308520543, -2.5504647775856673 56.660412029809116, -2.5450838592228275 56.66215069046496, -2.544454741292561 56.66332406643643, -2.5375134872026592 56.66553192805824, -2.5353394354496483 56.67064769715716, -2.529657096318819 56.67141493459416, -2.52457502461292 56.66913571953186, -2.519168947020472 56.667921302185555, -2.512736440464051 56.66811600398632, -2.5062852556062247 56.66619113853189, -2.5009376912636876 56.662923742267395, -2.4964332905771456 56.660924953742, -2.4987398788306177 56.657585384414226, -2.5000103138617633 56.652022847177406, -2.5025481705944346 56.64750455344662, -2.5039798196942122 56.64125496224067, -2.500955654721581 56.63328952447983, -2.498514384579797 56.6309913110092, -2.4945801905730995 56.630179654697656, -2.4865861246748695 56.63050854939787, -2.4813547829776836 56.627900160450466, -2.4786687349607788 56.62467814845636, -2.481291977435581 56.622704171282024, -2.4806838293675955 56.6211505088109, -2.4862778948926114 56.61833476365894, -2.4858659239769754 56.61518392613972, -2.4935259316997267 56.60959898546943, -2.4929601930430647 56.60769395025945, -2.4996179724547627 56.60473326290262, -2.50404094854848 56.60037615942356, -2.5035404439910494 56.59874043404337, -2.508814882149399 56.59687095680902, -2.5160732465226494 56.593166582143354, -2.5188517775332957 56.590226278165304, -2.515855529340991 56.58914175911139, -2.5165131025403964 56.58637913698548, -2.519553762680482 56.58526597741427, -2.5202532363247885 56.58154277659044, -2.523816728651097 56.57931506388081, -2.5327713885358447 56.577355346328474, -2.5356379652745162 56.57529104329169, -2.5351570626422713 56.57086042261112, -2.5382311346004074 56.56878716953447, -2.538829254254576 56.56587908456753, -2.5441169390534197 56.564006231189055, -2.550824481890512 56.56034090525871, -2.5558903678502656 56.55908663365362, -2.5646258966852615 56.55922598491644, -2.5719130562415558 56.55675446625388, -2.575850406004004 56.55410480246586, -2.5783910445462985 56.55365102549897, -2.5834112429156764 56.556411727520334, -2.5838195911440742 56.55124213518402, -2.588001079195095 56.54994938429697, -2.5980422209716294 56.54905163732106, -2.60188855518754 56.5503617890262, -2.600880411940127 56.55222996934285, -2.5949757377892775 56.55461485232561, -2.5885800474763485 56.56021184760575, -2.589306078784773 56.56504448102465, -2.5878769182531296 56.574033517130616, -2.588910733655265 56.5788736672393, -2.5877084820530416 56.57964484510246, -2.590508142158864 56.58297995101628, -2.5902784716308704 56.58651802239192, -2.591292861719863 56.58892085613741, -2.5979886454260654 56.60019079709203, -2.611700229593604 56.613254140523814, -2.6113883606915937 56.618750319643496, -2.6154812719189757 56.6237324188122, -2.6191917164234164 56.631963060504475, -2.6197713267488756 56.63490148993782, -2.623326606756199 56.63687104136326, -2.6162301239783905 56.640877235519305, -2.611598328856801 56.64260978004846, -2.6027594410484136 56.642630454460395, -2.5952054776776277 56.64037045963118, -2.5892434826047164 56.641344931089435, -2.5833897041155223 56.64501992932526, -2.582698502962796 56.64862756089282, -2.5759074526709878 56.65233891612366, -2.5718566385602903 56.65258221748103, -2.569358996819399 56.654058972165046, -2.563889994565528 56.6593987467788))
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>