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/S02000449 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000449 geosparql: asWKT POLYGON ((-3.736270347740022 55.994609805647336, -3.737171054360505 55.997743068920606, -3.74104730609046 55.99782024564243, -3.741198597681133 55.998344053182194, -3.745367413843757 55.99865094817978, -3.745408090343458 55.99741925730915, -3.747718919641693 55.99745840152834, -3.748129265225941 55.99838660523806, -3.752895442158666 55.99810380044821, -3.753018850239562 55.99693383421789, -3.755608644258202 55.99719332525001, -3.755470527149313 56.001140462149316, -3.752428491969774 56.00266646642627, -3.746898376695954 56.002017073889625, -3.735384551818616 56.00167645865142, -3.7338525471827 56.00256069178064, -3.723633117231761 55.99887588325618, -3.716951340523097 55.99682142415913, -3.71676840531808 55.997758540334424, -3.708999511948459 56.00201065947966, -3.705644410886482 56.000770561312294, -3.699918757316158 55.99979237676261, -3.693843131238129 55.99944721168667, -3.6924980255294 56.0001160336303, -3.690960217178548 55.99978667662335, -3.689412367193403 56.000329098237756, -3.689697448964799 56.00134960998873, -3.692816963188321 56.00374206320885, -3.691829703062478 56.0050046868197, -3.685431594151664 56.00576627898997, -3.68169139629524 56.00569154735354, -3.681218414220846 56.005104593584, -3.677728771798473 56.00122551943014, -3.673078262255721 56.002340097991656, -3.669387509728476 56.00265971484897, -3.664845272185063 56.00219994170008, -3.663653257251747 56.00170382712073, -3.664987954543728 56.00067038485138, -3.668664862142207 56.00040499892253, -3.664532851407411 55.993442717970765, -3.663287610606456 55.99356736198576, -3.661911968191617 55.992148140285245, -3.662125393685135 55.990770380591265, -3.66095132344411 55.9903099505969, -3.661031123862843 55.98955403531911, -3.656227268677823 55.98893574202557, -3.654591775016557 55.985551969596, -3.664483298784186 55.98669481330091, -3.66122600494411 55.97839964439185, -3.688032210069103 55.981180546375874, -3.696730619522376 55.98305815744388, -3.697055477794813 55.98210161554354, -3.700479930196153 55.98322901208211, -3.702850800188329 55.98319627746401, -3.703621433337685 55.98373637646463, -3.709064795824284 55.9838021985601, -3.714723035701291 55.98452338631593, -3.711817647316362 55.988059397251256, -3.708183655706277 55.98962847886365, -3.707379449297818 55.98943874518842, -3.70758572139764 55.989968914026235, -3.713519607785322 55.987757180250334, -3.714213769630437 55.98795307903147, -3.714158802180947 55.99024645149859, -3.715552521502395 55.99094595690715, -3.715006342580087 55.99164550087287, -3.719241717131599 55.99226118893794, -3.726807852774192 55.99203787083563, -3.725812493472992 55.995415889213575, -3.726864609986317 55.995419549236956, -3.728122588793537 55.994553561734016, -3.728877324499279 55.99457892055939, -3.729490283281919 55.995379084947075, -3.730424872625118 55.995123342616445, -3.730177154805566 55.993167820626375, -3.730765202408081 55.99305172776475, -3.734140992513565 55.99360636192387, -3.736270347740022 55.994609805647336))
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>