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/S19001297 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S19001297 geosparql: asWKT POLYGON ((-2.5879933755225024 56.57827535699634, -2.58554395197361 56.57653862799256, -2.583305835744559 56.577435252315674, -2.581773056069275 56.57694596009101, -2.5832446837876257 56.57540501635199, -2.58017947072953 56.574526968199265, -2.5757545022542883 56.57359523564326, -2.573696022465933 56.57845611965318, -2.572865247160158 56.57734931148067, -2.5709204442312186 56.577340775303405, -2.5716100829614215 56.575863653489826, -2.5734192359421826 56.57604161407393, -2.573752219342439 56.57501200106958, -2.5694847955538664 56.57442441601612, -2.570681199988539 56.573230417045025, -2.5723233026728347 56.5727967008476, -2.5676502979681586 56.57195317043581, -2.5637531690222164 56.569446595477025, -2.558883817506439 56.56525421848831, -2.5647746339720037 56.564185434988644, -2.563400960485802 56.563585041085055, -2.563720366753983 56.56273089202337, -2.5630103473403385 56.56114057402671, -2.5677427961400086 56.56051274787198, -2.5703005030270467 56.55975889902452, -2.5761545974659077 56.556990035913216, -2.579986809125373 56.556208515701925, -2.581374991427032 56.555388984921194, -2.5834112428741918 56.55641172482532, -2.5849745324619464 56.55587967909387, -2.5858862568382235 56.5549796609737, -2.5869556676434766 56.55281483760687, -2.5909318402999815 56.551604779654205, -2.596459673363741 56.55111744052241, -2.5985558342888893 56.55159882874379, -2.601981616756192 56.55161102193527, -2.605157963664105 56.55076723126036, -2.609523043460485 56.548905048610166, -2.612041243957534 56.54761424434722, -2.6142163264864853 56.546067395300916, -2.6178722823082934 56.54842631839052, -2.6180617255155294 56.54907223009335, -2.626087220133702 56.552437022319836, -2.62384719425275 56.55371504234565, -2.626096006235999 56.55616685274322, -2.615147992155775 56.55857331910458, -2.6146008297814802 56.55903506683023, -2.6171079478494215 56.560423852168434, -2.61926185634368 56.5620647692335, -2.612264254641375 56.56467178755857, -2.609488335086207 56.566166325417676, -2.6089326487241213 56.56754445286317, -2.611952047372701 56.56858889779995, -2.6154523259153155 56.57068286784505, -2.6191265535162684 56.56990909052738, -2.617234849379807 56.57141011067999, -2.6186543940657083 56.57279305685495, -2.615727943615997 56.57378833848639, -2.6138933826350184 56.57288315056504, -2.608786713502723 56.57516279070373, -2.607247927902841 56.575628502615004, -2.6021931724489265 56.57608432448662, -2.5986731783751083 56.57684700001459, -2.5946111892914465 56.576273549290725, -2.5897507378375013 56.57753389179423, -2.5879933755225024 56.57827535699634))
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>