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/S01002366 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01002366 geosparql: asWKT POLYGON ((-6.363903473667309 58.23797008618638, -6.364235184457716 58.237616995672774, -6.363283868897815 58.2370919751959, -6.361408280851587 58.237093184751025, -6.358064053880439 58.23596647428569, -6.358080760012617 58.234437081996816, -6.35900331561131 58.234729297814646, -6.358553208375703 58.23398027465543, -6.359557659205415 58.23382902610602, -6.357770918157614 58.233719233102065, -6.358263487759356 58.233009930276395, -6.358836520190533 58.233233159156, -6.360180188951881 58.2325127369965, -6.358696304291983 58.23223072751212, -6.359635612466573 58.23127232839248, -6.36057993293699 58.23146486416373, -6.361999303387587 58.23108357018843, -6.360552085797735 58.23068341694466, -6.361347946517466 58.22994574306005, -6.359913684002445 58.23006673857148, -6.359481067997713 58.22918222405739, -6.360729959148666 58.228662881795415, -6.362011416477808 58.226883381386536, -6.364797246284629 58.226248452301895, -6.366726043610719 58.2264072345536, -6.365882192824109 58.22495227114963, -6.367326594412464 58.22477689659271, -6.369974201401067 58.22482107184626, -6.369699774743457 58.225361071275586, -6.370358051443395 58.225446428542234, -6.369697175106609 58.22617054656742, -6.37081092274691 58.22607841043212, -6.371664867393066 58.22512283736593, -6.372039757831916 58.226090237585026, -6.374399319577445 58.22490314069217, -6.378463463338924 58.225375171016324, -6.378944105082758 58.22623100216075, -6.381782644251837 58.22602555740949, -6.382917244155042 58.227344533808264, -6.3840789773779 58.22708875510825, -6.384866838671021 58.227394412474716, -6.385115532672889 58.22830316872266, -6.388607079281009 58.22896544373865, -6.395704956881118 58.228873823829765, -6.396809087337419 58.229941924958936, -6.398886062272654 58.23074263468667, -6.403522822868451 58.23114916040236, -6.405555236289179 58.231726471756645, -6.406131109675802 58.231283886237655, -6.4088418416989 58.23114521452862, -6.409577399483469 58.231713342926994, -6.41314452291896 58.23174281494304, -6.413309455222955 58.23263842102369, -6.406762240523771 58.23512920989834, -6.406102466049455 58.23613227991199, -6.399036555586377 58.234599186322406, -6.397328565925497 58.23880937249744, -6.392652243717837 58.238236846316234, -6.391430500569614 58.23856708236048, -6.392054545958563 58.24111353563146, -6.393292353612528 58.24091801496863, -6.3951119417784 58.243490341833166, -6.392665351097039 58.243979332420864, -6.393652694182825 58.24437699779065, -6.39472785217004 58.24589578968307, -6.395678963529523 58.24586301253829, -6.39715448310613 58.24713364263139, -6.399137200051872 58.24610350628299, -6.400147064178694 58.247093902698104, -6.400046202234138 58.24803727569865, -6.404204560660149 58.248315997886706, -6.40427892615263 58.24996474600092, -6.399772245750137 58.25009735005073, -6.39896791737373 58.24813375433019, -6.398337785366474 58.24804664316862, -6.393265187722882 58.248545231536596, -6.394276009475268 58.2477549732152, -6.389623964248181 58.24503733195807, -6.389159695343279 58.243353592989706, -6.388414659711304 58.243397216527136, -6.385622058473911 58.23957223510313, -6.385328230332199 58.23816141504498, -6.384372900302623 58.23774459997441, -6.381796523184335 58.23897528978935, -6.382438786269828 58.239618720064136, -6.379623175586822 58.23974242565849, -6.378192832286264 58.24072683026291, -6.376172483313904 58.24024759013154, -6.37334338018363 58.24178354478485, -6.371343999739785 58.24147438239232, -6.369252129321005 58.23930677865301, -6.367344240176547 58.24015453391073, -6.366194119194761 58.23912373451588, -6.365032546266569 58.23883075197149, -6.364825216864801 58.239224555565535, -6.363971988564567 58.23880409732481, -6.363903473667309 58.23797008618638))
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>