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/S20001420 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S20001420 geosparql: asWKT POLYGON ((-3.196189539639212 55.851676180696586, -3.194701405952313 55.85211025989887, -3.192950886949247 55.8515868603531, -3.1996590550035315 55.84962356475815, -3.1996498784166225 55.848895321631375, -3.2011477968562865 55.84852889144589, -3.2022184428645684 55.8468654456855, -3.2014771330441407 55.84604909183241, -3.19461442082895 55.844071677948094, -3.193729432415889 55.8448979608864, -3.1943491058813076 55.84583543678194, -3.192167239711324 55.84675432693636, -3.1909194012848903 55.84797277735095, -3.189650200147611 55.84677054342117, -3.1916125797260464 55.84669068225376, -3.192221456065513 55.84520012464426, -3.1946735223340745 55.84295261165224, -3.194342401330692 55.84240406262534, -3.1918595962359744 55.8417261942997, -3.1952314197978637 55.840758981715524, -3.1959845727298504 55.841883842097154, -3.197532082630011 55.8423162352676, -3.1997513682921284 55.84425246694461, -3.202937783128986 55.84372550008965, -3.205123242304594 55.84280571163104, -3.207354994678937 55.843293363031606, -3.209834324640669 55.84253015913645, -3.2118370951232484 55.84109478311016, -3.2111643292948133 55.84018806241954, -3.2080134296356047 55.84035667053258, -3.20537988762647 55.83927254416725, -3.2060416003590175 55.838553510555734, -3.2063394822528286 55.83679609996315, -3.2073471885807847 55.835080457150006, -3.2078570110835702 55.832055093539594, -3.2075593280861847 55.83044883742876, -3.208829313850274 55.82918735453934, -3.211439774853355 55.827912661350965, -3.212872025255896 55.8259145444357, -3.2146556402568924 55.82455624234705, -3.2153521698590706 55.82314931999201, -3.217885428560457 55.823298500210356, -3.219206027840779 55.82107376765897, -3.221603555833299 55.82192297961538, -3.2221082108370216 55.823095827904936, -3.226246161237894 55.82257199763542, -3.229195215913248 55.82296508209678, -3.2286698494843904 55.82554024135541, -3.2291789226233543 55.82643291516628, -3.2288986749468362 55.827913656503796, -3.2274951909853584 55.828283621429435, -3.224359545263172 55.83010302827181, -3.2247187022775874 55.83086323545415, -3.227194923766786 55.83114446181259, -3.231221247883844 55.828992189266984, -3.2336140917791076 55.82884163019671, -3.2365424753758347 55.82813382039777, -3.2370435248675373 55.82884390875797, -3.239642366667943 55.8292400697837, -3.240915561275671 55.82811300283951, -3.242576923477902 55.82764694342986, -3.2470676331610915 55.82879659027911, -3.246841784796128 55.83005725720269, -3.238109145747796 55.83524897398282, -3.2374324702679576 55.83505809825809, -3.2279213624707306 55.841299610010395, -3.22346467316779 55.839735591562096, -3.22208889148687 55.8406747913272, -3.2190250660680304 55.83972575481569, -3.220415346646422 55.84179664297595, -3.216431795441326 55.8433530223682, -3.2132720903106016 55.84420358321437, -3.2115696870258317 55.84338469652625, -3.2112432755875253 55.8446998064681, -3.2083891706002525 55.84541929363563, -3.2106923077426432 55.846626378930516, -3.2133035716277347 55.8472217584262, -3.2129058781576627 55.84779360875245, -3.2071136060863537 55.85199401761286, -3.204930866558681 55.8513667598531, -3.20526512376307 55.85076810682221, -3.2081901614277135 55.84879464605175, -3.207714779725963 55.84833295022858, -3.204237806381427 55.84760977028548, -3.203501289566013 55.848397209172155, -3.202015390617645 55.84790833438838, -3.2005267341996384 55.84989971768259, -3.19849952695679 55.850580196058345, -3.198672101502529 55.851373463780995, -3.1975015273747296 55.85226545394376, -3.196189539639212 55.851676180696586))
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>