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/S01004213 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01004213 geosparql: asWKT POLYGON ((-3.194003205892587 55.893156804557435, -3.187451877776207 55.89209640912644, -3.182331109493127 55.891938054196466, -3.165230420331884 55.889897598395486, -3.162273285436724 55.888985180770945, -3.161892413865873 55.889223959984896, -3.163247860014521 55.887663953872526, -3.164099517476668 55.88719399864577, -3.164643238564158 55.887418491330216, -3.166100389701339 55.8864053848464, -3.166080524408618 55.88564722631697, -3.165588519942172 55.88564683112278, -3.166131843645778 55.88494410093398, -3.168051263720226 55.88491769199042, -3.170221974828999 55.88420710070253, -3.17088875181966 55.884511247775635, -3.171613498521743 55.883757518732516, -3.172934596326887 55.88356522546374, -3.172654772055246 55.88223803090567, -3.170981084351431 55.88133743985246, -3.172532338655855 55.88135861365583, -3.173114142696146 55.87997828053413, -3.168528900982 55.87899754707772, -3.171848749356172 55.87458042827738, -3.173622268007612 55.8744411735959, -3.174306079588691 55.87335344862561, -3.175203572072959 55.873596508615165, -3.175621481109308 55.873026490464405, -3.179831806626635 55.87483722490182, -3.180232424607992 55.87381801450511, -3.181975898027518 55.87596680399242, -3.183234022007012 55.87646426089711, -3.183587809210558 55.87814767011089, -3.182432960811486 55.87827176416925, -3.186783055048986 55.88070122617118, -3.189022603984947 55.87944394510161, -3.192598486995927 55.87992580650723, -3.193268607898029 55.88124247037815, -3.195965986471963 55.880375221483085, -3.197380605039588 55.881461325522245, -3.194867260686271 55.88249215763214, -3.1964346550578 55.88313828421509, -3.197814519778863 55.88475432197163, -3.199136348877934 55.884588696641394, -3.19740131497973 55.885872553400475, -3.198295140279503 55.88793182315105, -3.200848647717623 55.889866917847264, -3.204337588795289 55.889067695016195, -3.202476798812853 55.884043929524665, -3.207269802841963 55.88188539762344, -3.208947915290514 55.88341444751449, -3.213645759942051 55.884869667936194, -3.214132192096418 55.88644745636629, -3.212336541055496 55.88758917162464, -3.206844639877495 55.889663082610994, -3.200756292772006 55.8903824825728, -3.201817226689466 55.89400749756779, -3.201470677730478 55.89467391980731, -3.194003205892587 55.893156804557435))
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>