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/S16000084 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S16000084 geosparql: asWKT POLYGON ((-4.683144357404065 55.43545073524368, -4.67759244909667 55.43837610449765, -4.660726449834633 55.43914697268296, -4.650957161898104 55.44082884621035, -4.647716950621876 55.44533148310375, -4.642580843493304 55.45720364790045, -4.6401116151611275 55.46827823221523, -4.628357258761078 55.47750670105147, -4.627894328597398 55.48961559648522, -4.6223738560998076 55.49152010348507, -4.6203127133181665 55.49819618384975, -4.622836296787025 55.50728281855941, -4.630757838439286 55.518257112203635, -4.648738780827849 55.52658672371492, -4.658811051902817 55.5388215297369, -4.664202448159597 55.54196549297168, -4.670836923078839 55.539980941135255, -4.67405585590913 55.54419657962756, -4.664515614958302 55.54781041585696, -4.659240315185019 55.548016770967514, -4.658263330122127 55.570194488196044, -4.654702294982866 55.57059979284674, -4.652088074282808 55.563075290316945, -4.647831047227367 55.56335888730233, -4.6386079034900956 55.56955700430456, -4.625520925925117 55.55628053012521, -4.628091023403047 55.54983795015119, -4.6218252530093835 55.54805856276815, -4.614148922492748 55.53854270515227, -4.611482933305091 55.53185270664001, -4.6222533766028455 55.526971706164815, -4.619287060606242 55.52375614996642, -4.621399231789501 55.519517626143504, -4.614389185315413 55.5144649050556, -4.588537841979914 55.50551868386973, -4.575341326675477 55.50938889771001, -4.569767497988066 55.509606690050546, -4.565223661913951 55.50403916651053, -4.56553208152931 55.4982656238664, -4.570564509491746 55.49034886702865, -4.580289316302308 55.484362702826715, -4.586207541168172 55.476266506219886, -4.584023905862005 55.46943087477129, -4.594779104017039 55.45607242456975, -4.600378604661517 55.44201552270554, -4.6078345510455465 55.43581628135167, -4.615376614943475 55.42712928702828, -4.61639379460481 55.42148049055671, -4.624522959769406 55.42132960806495, -4.638979894583773 55.42773846650431, -4.64974701920023 55.42657522718081, -4.65656468911478 55.42867629897606, -4.6752228868053685 55.42953061433141, -4.683144357404065 55.43545073524368))
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>