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/S02002021 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002021 geosparql: asWKT POLYGON ((-4.132947856891934 57.703770770750424, -4.1342638681240444 57.702981563995095, -4.135920544885237 57.70224290682976, -4.138072054102036 57.70110232874803, -4.138588738924452 57.700700058892934, -4.1410361072499935 57.69942863897547, -4.142980608885742 57.69814142819655, -4.145395952515992 57.69673119629243, -4.146511826944411 57.695964725120916, -4.14618610835039 57.695536278657876, -4.148134881327605 57.695291163398416, -4.1516675257993585 57.693798937820496, -4.1534508859667545 57.69286111747506, -4.155062142148853 57.69188037935253, -4.157454438777715 57.69018188235628, -4.158162632242766 57.68942315320775, -4.159139718557743 57.68872901507963, -4.160808798458129 57.688161419985356, -4.161218163708759 57.6882460739929, -4.166144335661245 57.68690482286834, -4.168304953705152 57.686228077296356, -4.167787155638678 57.68514257943237, -4.170268977259707 57.68493020946721, -4.170352060730018 57.68519743550791, -4.173653409302986 57.68511200066164, -4.17640954700081 57.68550218292773, -4.176495082081192 57.686956283324065, -4.178929585365111 57.686904491624524, -4.179079574527305 57.686776117881784, -4.179053670282997 57.685031673335814, -4.179820539558775 57.685063386473225, -4.179984314890568 57.68765004736363, -4.179592281575412 57.688469047916215, -4.179871241709526 57.68886766879739, -4.184341553402815 57.690163475054945, -4.186307191319646 57.690624596773645, -4.190261021860949 57.691306458965045, -4.192151835316424 57.69135996696493, -4.194346061324069 57.69154745286336, -4.197278866180354 57.691684338439096, -4.199224711695074 57.691925471567586, -4.2015642262466315 57.692459826539256, -4.203710296523039 57.69268123390281, -4.207426008982889 57.69266677440753, -4.210488141027889 57.69189450279462, -4.2116504514093815 57.69144649882841, -4.213311407267419 57.69108680026496, -4.21453553543827 57.69121273429155, -4.218809824661686 57.691088425199574, -4.219933232305198 57.69074705040716, -4.221293871655976 57.69091005823309, -4.223374311677566 57.69096336421399, -4.22694532112549 57.690722866406624, -4.227612303424928 57.69127468894711, -4.232907280803893 57.69033495274199, -4.233196929578987 57.690443538262514, -4.2356361702314835 57.68946564955693, -4.237241872753132 57.69064998244704, -4.238409677851434 57.69008612708945, -4.239227463894931 57.69072854269615, -4.240791610043825 57.69004298937893, -4.241934139688053 57.690981178097395, -4.24011715362675 57.69163281982523, -4.241186677186625 57.69263294901861, -4.242184785440324 57.69380503565748, -4.239957176801829 57.695241876541, -4.2415249174878795 57.69636884912117, -4.240791579861618 57.69663389993514, -4.2410360454908265 57.69712275691641, -4.237725101295347 57.69803774214065, -4.232646823417541 57.69896307172934, -4.228852412134387 57.699209671109465, -4.223819076827364 57.70005298430189, -4.2241623745498975 57.70045128962281, -4.222245151844272 57.70093422229036, -4.225201800095448 57.70289492826762, -4.229057216566005 57.70187108901659, -4.230757953509085 57.70329192864773, -4.232434432317364 57.70246068178032, -4.237733826927771 57.7014429614172, -4.237308667406818 57.702169300652415, -4.238573377076282 57.70402503621764, -4.237411926324419 57.70486088115063, -4.2360514912469 57.705334032531624, -4.236402287842888 57.70679469393431, -4.232473672403392 57.70760084309449, -4.233228789507851 57.708414151699024, -4.239350236935375 57.70717239278789, -4.2406595532862195 57.712667563071335, -4.242776618405557 57.720563939588914, -4.238909053038712 57.72401081916156, -4.221171177456075 57.71134884406684, -4.220237814702091 57.711034076348845, -4.21355475323588 57.71336017086049, -4.210619514022887 57.710653058349486, -4.215457090636834 57.70904998519831, -4.215471198561919 57.708456722581325, -4.214571175522097 57.70856232665464, -4.214341769768813 57.70782956412946, -4.213204792958362 57.70790336427646, -4.194576215303385 57.7110221207537, -4.196445461156911 57.71333479518325, -4.193382865983339 57.713495738136224, -4.186853634802351 57.71429556544207, -4.186332839635621 57.71279665470843, -4.181040879531709 57.713775455042686, -4.177454085357008 57.71465372504003, -4.1755120350009935 57.712249776072746, -4.17620199580756 57.71143825079826, -4.171585735913165 57.70724068898287, -4.1608518750445 57.71050092491352, -4.155146456800978 57.706050569415176, -4.154889860155217 57.70532379909192, -4.153488521185762 57.70234748628455, -4.152086482280945 57.7014432492782, -4.155770695189827 57.70015531402251, -4.154438429331618 57.699122507885754, -4.152595903318819 57.69923610566225, -4.150435955963572 57.700531791698886, -4.148728064030014 57.69930411960326, -4.147918338990462 57.69807376076232, -4.13968417336433 57.702997595972455, -4.1357587794215105 57.704602606976366, -4.134193911481509 57.704306980980796, -4.132947856891934 57.703770770750424))
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>