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/S01006172 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01006172 geosparql: asWKT POLYGON ((-4.116260619468675 56.281047690674235, -4.121442918081341 56.27719356801239, -4.127811898744385 56.2705868741935, -4.137126884111672 56.265303937477306, -4.141225416779575 56.26062290332465, -4.141228324737226 56.25027016943818, -4.14446028188442 56.24975586127999, -4.144108617260893 56.24693114035986, -4.149161954613106 56.24522583260757, -4.147699034634931 56.24215980555033, -4.145573343598036 56.241954055593084, -4.142267306098784 56.24315267385107, -4.138898471995101 56.241782086992174, -4.138968774806838 56.239570138617005, -4.136724883461654 56.238108142216376, -4.138463843909306 56.23572356658835, -4.131451366828736 56.23497290744398, -4.128870728317083 56.232375264508086, -4.124191242986347 56.23091001628822, -4.121752888252757 56.22707860911954, -4.118428163481511 56.22383749299436, -4.120734632108473 56.221497362903875, -4.116907284581937 56.221122597593826, -4.118841350775761 56.218465355180165, -4.118112567972213 56.21753423808318, -4.11680789988621 56.217565567554864, -4.115993889275571 56.21684259159714, -4.116129339759127 56.215788829825954, -4.114241512652886 56.2154796310092, -4.112123957002605 56.216270704378495, -4.110452511011849 56.21608356488784, -4.109799960212647 56.216535048999255, -4.109499894203038 56.21606387459714, -4.101364099784959 56.216516946209886, -4.102151648854979 56.21381654521838, -4.099408058863014 56.21318916214559, -4.09857295679032 56.2105972067271, -4.098812720753448 56.20908337649506, -4.104970267173126 56.20728020671895, -4.10410307974209 56.204401256551705, -4.117569609897862 56.206813410688625, -4.119252122220467 56.20545454557173, -4.117320953702804 56.20464288484955, -4.117806237069994 56.20350224592935, -4.126469976581922 56.20535756079677, -4.133344933250949 56.20634448189392, -4.138065389784125 56.209462218077796, -4.141810785121156 56.213010066278514, -4.144219072862652 56.21426241683044, -4.152270734706208 56.21405062271737, -4.150002271102291 56.217891496403055, -4.154267683570992 56.22089519462896, -4.174738982612782 56.23229076208297, -4.176894043919835 56.23390640013113, -4.176313114803105 56.236126745925645, -4.175118714725586 56.23691059290788, -4.175795604100892 56.23711107087478, -4.175825634910475 56.23772755504381, -4.174948741228465 56.238304604438746, -4.178106318780105 56.238357086500635, -4.179285793848588 56.23789362526114, -4.180247337927017 56.23670978288078, -4.180265440104225 56.23510815759516, -4.183197614124873 56.23364512379506, -4.182087619789989 56.23274398661576, -4.178878041951082 56.23168570294086, -4.178597001208152 56.230778231424694, -4.179275842845186 56.23039193081791, -4.183853809747432 56.23032015838306, -4.184919142660326 56.22976213053456, -4.186224127128881 56.23142859244775, -4.186553643518911 56.23437943560509, -4.185370009829846 56.23456211976679, -4.185694391268326 56.23685701582437, -4.187787127025897 56.241295455652356, -4.183858342807904 56.242020924817076, -4.183339489479154 56.243396080835815, -4.185032143838663 56.24421095165144, -4.184372582331346 56.24481573113607, -4.192165154512242 56.24589109198284, -4.192584780176063 56.246737403489554, -4.199173551335783 56.24735735783488, -4.200480422109404 56.2459411542994, -4.198958140261854 56.24219374916873, -4.199409381649388 56.24161955413157, -4.196469639621339 56.24129434711511, -4.195987045730092 56.2404761264536, -4.196814433522369 56.239706538032515, -4.19958105581676 56.24090850512132, -4.200967154551245 56.240360643802326, -4.202035686737462 56.24070109879505, -4.201366376134835 56.2414455420796, -4.206295032415376 56.241802472613415, -4.207563842103205 56.24225613726221, -4.206233292796913 56.242660240919605, -4.20743618670982 56.24296157719213, -4.205144680805941 56.24461675118434, -4.21653514322845 56.24640939739884, -4.22025772144031 56.24686387379493, -4.222025665753683 56.2461760833341, -4.224302774061278 56.24591947018727, -4.225147466808938 56.246012120358394, -4.224813622931155 56.246934790099694, -4.220314869051069 56.2489657813586, -4.229542012544505 56.25829897606033, -4.223488124618215 56.26248797631122, -4.223075302738829 56.26901088199296, -4.223850905340297 56.270965070274734, -4.223177004524783 56.27271164631612, -4.219037499020815 56.273747543659915, -4.218322257422767 56.27478486960296, -4.219228385519707 56.27704229726735, -4.217102317824295 56.27854526332435, -4.212034299240455 56.27974133835646, -4.213502565184365 56.281386636897025, -4.213255486617111 56.282981725813, -4.207707545097674 56.2817507688261, -4.20294338780333 56.279535040861624, -4.198924122757759 56.280154718033494, -4.194365190656042 56.279534669112834, -4.188839691258539 56.28517736487807, -4.189564734129186 56.2874022470865, -4.191672918728853 56.287535682260895, -4.193236170989525 56.29028490576272, -4.195481029068562 56.29110783813215, -4.191478483814199 56.294018589554284, -4.170878603831274 56.29866825292375, -4.155209795286587 56.30026308318524, -4.14939826801815 56.298378070567324, -4.141404290662431 56.29762693542267, -4.133318599807375 56.29290477385455, -4.119534279983751 56.2885314419806, -4.116260619468675 56.281047690674235))
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>