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/S02001788 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001788 geosparql: asWKT POLYGON ((-3.1976659180958533 56.129742745840794, -3.197928428704583 56.12953136540221, -3.1975271424958542 56.127866178101236, -3.1982359072105 56.127437204822826, -3.1977786667199823 56.12674032439646, -3.1968500556453527 56.1267855279223, -3.196445104739797 56.126178446526744, -3.195122572725723 56.12503216494034, -3.1937281189423197 56.12467725607675, -3.190049830185767 56.12485651676893, -3.1898618344704506 56.12449891896456, -3.191675455245552 56.124364625163075, -3.1911818506996705 56.12296769089039, -3.190511426160241 56.122614737055535, -3.189130129873261 56.122681964778636, -3.189562099197284 56.12312706634064, -3.1889937102628285 56.12347397831574, -3.185741367685895 56.123900596118304, -3.1850704239849064 56.1235296565025, -3.184959296043171 56.123054505930895, -3.1843130828317117 56.12244072395205, -3.1832525716366176 56.122477844722916, -3.181419731152502 56.12198323363127, -3.181606105455892 56.121765805320315, -3.1829080674209513 56.12173537140375, -3.183026871753096 56.12088962239507, -3.182243170565874 56.12051076251565, -3.182924412669201 56.12017178585205, -3.182792555082234 56.11954408299089, -3.184433786276185 56.11956429413726, -3.185243628632613 56.11922407099422, -3.185015813493218 56.11861526160348, -3.1861798355254907 56.11933189031521, -3.189000957820417 56.1200595209815, -3.19066717358163 56.120366936390276, -3.1918469332530193 56.12025946842094, -3.195341505990388 56.12068119165135, -3.196420081048839 56.1212278324013, -3.197413863047804 56.12318596864541, -3.1994001981310842 56.12264926129587, -3.199903370321028 56.12225800825091, -3.201344576487618 56.12239584820739, -3.204219668835542 56.122400239631496, -3.205808244276303 56.12259213740477, -3.207058565735187 56.122966289458084, -3.2076823106572294 56.12336452803397, -3.2069470357760808 56.12402763897616, -3.207700857156272 56.12446953250076, -3.208192484154799 56.12525725913386, -3.209066932079637 56.125155201354275, -3.2097367540002835 56.12664465059302, -3.2097981137495624 56.12812692106061, -3.209123803608887 56.1292122746921, -3.208426782730317 56.12984717362049, -3.206640694247979 56.12978726319155, -3.206699860630759 56.13051740088071, -3.20585928839513 56.130593466545676, -3.2054667692900973 56.13018541463119, -3.2058718521813603 56.12975303529868, -3.2045297764739717 56.12955020634033, -3.2045933432865255 56.129010472060656, -3.2039920776501596 56.12881865929774, -3.2041622094953452 56.12798317948181, -3.203127968198516 56.12793754085745, -3.203467216894662 56.12901246445121, -3.2022050279449 56.12929431332361, -3.2019475191586992 56.1287756757656, -3.200264579338985 56.12845735946871, -3.2002970993141915 56.130022702485746, -3.198578910056453 56.13012923979616, -3.1976659180958533 56.129742745840794))
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>