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/S01010540 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01010540 geosparql: asWKT POLYGON ((-3.6890101492529417 57.27306506306567, -3.6801657066449502 57.267873826692274, -3.686357686613006 57.26620062759271, -3.6892426165534533 57.26112159564998, -3.6896569062706592 57.2592921784836, -3.6889894927080182 57.259203984991096, -3.693781427831904 57.25327507413704, -3.695731094372864 57.25018951902818, -3.70070125616215 57.24790342983613, -3.7036124119377236 57.247276055692794, -3.7055061443580914 57.246097116040836, -3.7088639333370343 57.24287335994955, -3.7096262164574587 57.239750386728964, -3.7116555331022463 57.236577379108724, -3.715120950720799 57.23286506441961, -3.7079656467281525 57.22932382841279, -3.7054320873036493 57.22755236740909, -3.7115062834113055 57.224936086533, -3.723308329901823 57.22511609146591, -3.7304756604226608 57.22551085316764, -3.7388749337879896 57.22651022132059, -3.744180021877529 57.22653500407178, -3.7527585344196295 57.22474441926748, -3.754651787135053 57.22445254567973, -3.7603629915935577 57.22491048714381, -3.7602670061353267 57.22571296700557, -3.758764341021072 57.22657157952962, -3.759348076577139 57.22708535673667, -3.7585245331195876 57.227643122312884, -3.755845802313004 57.22776557257235, -3.7561268960562217 57.22850208404052, -3.755976943233105 57.22979361104673, -3.7603733818260543 57.2302083815533, -3.762810252981578 57.22399275775586, -3.76113046663082 57.2210693607615, -3.7733466538046123 57.216782441450924, -3.7704211277558057 57.21932146812497, -3.7724843618876744 57.22124464102301, -3.787708994777479 57.226580636995585, -3.803892343183355 57.23225598509686, -3.793621426862745 57.234275209678046, -3.7867158190599888 57.23708700897091, -3.7817017048626265 57.23409351920467, -3.7642994336807187 57.23872649937912, -3.765035003808996 57.23970179795729, -3.7662840922820657 57.239934021022734, -3.761381857478115 57.24179179247865, -3.7584332427412726 57.243415558866545, -3.758076073763757 57.244187512665256, -3.7569044759636885 57.2453960559554, -3.7551434691797865 57.24774768843039, -3.7583302023287457 57.24818829260237, -3.7614544479264 57.24836015406004, -3.7615713843124374 57.24768466757118, -3.764479417496226 57.24784151609237, -3.764324761326802 57.248595405252644, -3.764614521341941 57.24939208129022, -3.7628916815377664 57.249301347785945, -3.7626388467794105 57.250761626435605, -3.76019787475987 57.25104363850561, -3.7598998621517308 57.25079245296549, -3.758386241828542 57.25125391229032, -3.7548618567478718 57.25102421239078, -3.754293196245008 57.251479192659374, -3.75618919428491 57.25265663048128, -3.7527314853587868 57.25419637094269, -3.7567591015835955 57.25759733760205, -3.7535618880558124 57.258066128860534, -3.7515526205802527 57.25615055536352, -3.749846121343198 57.25658509222215, -3.7490009345675137 57.25652839835556, -3.748323723824719 57.25703402467614, -3.7523436056051227 57.26051048425778, -3.752479750742931 57.26216023116918, -3.7459332397933185 57.268501061178526, -3.7555067005630196 57.28012307694814, -3.7525480787517114 57.2862828516911, -3.7443051764392807 57.28915974170171, -3.7427367818016344 57.28842072121925, -3.740439347587146 57.283189709582786, -3.734668051729861 57.281472586579625, -3.7288216119894195 57.28167892247485, -3.715109006552909 57.290598913066056, -3.707330225593433 57.29082239190731, -3.70737484278991 57.2829045130123, -3.7035533594967203 57.282573869540066, -3.7026307978699986 57.28302980714141, -3.702057458268559 57.28206648682121, -3.695935566312807 57.281920314364314, -3.695079015230334 57.27923968734573, -3.703221413539989 57.276983093663226, -3.7009672643337885 57.27543660513101, -3.695608620114378 57.27669178475148, -3.6890101492529417 57.27306506306567))
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>