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/S01006865 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01006865 geosparql: asWKT POLYGON ((-2.5030297204862313 57.053947223199685, -2.5013938514514815 57.05373800539528, -2.501054464611028 57.05283437876739, -2.5004000061531455 57.05279375023991, -2.5002338135319784 57.05376100664687, -2.498612195614682 57.05367968047015, -2.495631938848246 57.053729008021584, -2.4953599938584614 57.05419615464334, -2.4918794363989245 57.05499237209315, -2.4922817788734184 57.05469445069684, -2.490592659543355 57.05286849085962, -2.4891967652524434 57.0521346999619, -2.4879304548870507 57.05204900840565, -2.488524067709214 57.051636041238346, -2.4924662850825507 57.05119922918711, -2.495143449198169 57.05046999980246, -2.4935404552759017 57.05016192230395, -2.4908567606426995 57.05039705351508, -2.488894682131152 57.04910214468142, -2.491617412073317 57.048085365091744, -2.4943147737975444 57.04765253080029, -2.500528170766443 57.047618863715996, -2.5009402558877505 57.04954591939032, -2.5012179878008944 57.049974532624105, -2.50251012922393 57.04990829431091, -2.504328595366578 57.05008403585641, -2.5065041350333095 57.05055927440824, -2.5074503570660727 57.0503500314136, -2.510107270434434 57.051434038859455, -2.5101440132781128 57.05189788519216, -2.513388292599105 57.05275439752238, -2.5137303607825454 57.05265389994253, -2.514541280768994 57.051395451592036, -2.5164782133688433 57.05317329370201, -2.519233052737848 57.053783760833944, -2.5198333211215505 57.054344945209415, -2.5212703399131544 57.05472911616873, -2.5216940864829085 57.05422177174476, -2.5225525315962156 57.05440773106682, -2.5220755578226095 57.055039188928156, -2.5204570385390364 57.05453428259231, -2.5175661558124687 57.054272165848495, -2.5154238599254346 57.053737997702086, -2.5139556308915987 57.05427785538678, -2.513792063025692 57.05351657284696, -2.5131541597262936 57.05291790088919, -2.5118727011861983 57.052986882104854, -2.509646213638914 57.051774505469986, -2.5075789118439498 57.05090558513452, -2.507222074436206 57.05136508909963, -2.5049189980529465 57.051742532127875, -2.506194745258036 57.05355716214662, -2.503975460864351 57.05344288085663, -2.5030297204862313 57.053947223199685))
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>