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/S22000057 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S22000057 geosparql: asWKT POLYGON ((-2.996309772659744 56.19239054459643, -2.996672060102962 56.19051191210838, -2.9966772106006134 56.19048520828166, -2.9969569188770553 56.189034627730585, -3.006440821456578 56.18233720441243, -3.0176622060752156 56.174408811124685, -3.045761746785328 56.167705856979936, -3.0462019915977843 56.16760077060186, -3.0467620117518757 56.16726788997766, -3.0472993707279774 56.167032598922475, -3.0478796903348115 56.166603506688126, -3.1234630529899747 56.121597822340526, -3.1493369694968183 56.117936136084126, -3.1493847500594243 56.11792453014211, -3.1741403444023697 56.062760848636074, -3.186422159856585 56.06295737787577, -3.217517613411308 56.063449486207666, -3.2189415173951677 56.06347183428362, -3.236096520987917 56.053687197787596, -3.2509684057954846 56.06098563324513, -3.263430704994374 56.05888171316234, -3.2841792283490783 56.055375605621585, -3.2861233037063737 56.055046908530585, -3.2911424728788563 56.052079402744916, -3.321487560197106 56.03412447446347, -3.3221473267765096 56.033733829069284, -3.323341411289253 56.03410891893446, -3.336650056158472 56.03828819397376, -3.33944601095052 56.03916590101234, -3.357089249080764 56.02607979455197, -3.3902226321247944 56.02458252205949, -3.391359966775797 56.00609951320814, -3.438459951299447 56.023780756204296, -3.442802606295793 56.01790906104011, -3.4559453984923785 56.02037847527098, -3.4628698694817532 56.030126934507244, -3.4632468480620546 56.03016411511941, -3.5056011987867786 56.034333302938435, -3.520964523584655 56.041591476765866, -3.521125507424757 56.04166750618884, -3.521289292676876 56.04165534821844, -3.546861473560954 56.03975438263608, -3.575523847448128 56.05886472598235, -3.5920386333900587 56.05215369985791, -3.6069096259061704 56.04610762191164, -3.6074333870171005 56.04589461134809, -3.6176606084446346 56.055966028876355, -3.664953328538406 56.05052121569398, -3.665287263992028 56.0636697993879, -3.6520926543140533 56.06011957870067, -3.646386375843756 56.06603664181693, -3.6536209688991015 56.079230435208416, -3.6427144389696764 56.09298924927224, -3.6244889302857133 56.09305953452535, -3.6269924742427966 56.081911170565846, -3.609119706520217 56.0756667521981, -3.576048664348087 56.08773895896946, -3.663912302815139 56.12310416803447, -3.5809517067565806 56.13930976102304, -3.5774580243469147 56.15455003264612, -3.546081395374189 56.15853911712518, -3.5400064522400734 56.14646232555052, -3.4537026575638534 56.15037781327368, -3.4166186805692353 56.13840144605157, -3.369912079969767 56.145840349648054, -3.3716850438658876 56.16442838201376, -3.3440222014244667 56.17331597717339, -3.3164176015556923 56.16648511610355, -3.2965586331264354 56.17056407008244, -3.309125202301864 56.18530482203206, -3.2609445263099897 56.19607383840419, -3.2749170135022214 56.21413082537574, -3.264885928466043 56.22004268217977, -3.2912544415922422 56.22973201981508, -3.2799641867296208 56.23363566986437, -3.2897758568677546 56.24026091755327, -3.272942475950489 56.243040935315015, -3.2843917128701507 56.25149441058137, -3.2707625571624512 56.25322550394834, -3.274738392811158 56.275410628500644, -3.2587676584588507 56.27436187101501, -3.258286136263358 56.2827998619703, -3.271629732977269 56.28374782101324, -3.300974114384577 56.31346371697217, -3.266845764014692 56.32809277789179, -3.2567138446132446 56.32144839482147, -3.2206703766181533 56.328978933054934, -3.2054491950530153 56.333525486330245, -3.1914546634137695 56.31632381871952, -3.149362873254754 56.3100204348249, -3.126185546251182 56.29470755317859, -3.1388884353998017 56.2844833345759, -3.12494766318364 56.28171769115224, -3.124446484565005 56.267391324443246, -3.070030827247543 56.2707399854486, -3.0780149905558187 56.26411176008236, -3.0639769836071795 56.242283244252185, -3.0263361208147463 56.25506315274276, -3.045831189853661 56.26737982509592, -3.0106616986124686 56.286827324622685, -3.015139751974606 56.273547222842375, -2.9910234885490445 56.27454225763582, -2.992501794666688 56.266156707128125, -2.9747048284274307 56.27041312479498, -2.9710721509084426 56.25833038294663, -2.952937876122563 56.265200857423345, -2.929624775072359 56.263458332113686, -2.9220759136494174 56.25409609443303, -2.9309601217945422 56.24907004536459, -2.973555718090428 56.23916442794919, -2.9813117132746596 56.24638181993118, -3.0082546613094325 56.233423682719746, -3.0111913489045246 56.22290837154325, -2.983358502927715 56.21587892384962, -2.997583753140939 56.208481289672505, -2.996309772659744 56.19239054459643))
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>