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/S16000091 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S16000091 geosparql: asWKT POLYGON ((-4.0834553733422965 55.850905701708385, -4.088404399117184 55.853845965287334, -4.085997146761084 55.85865624352184, -4.071716842974077 55.86127010060782, -4.076740442991229 55.86353556124905, -4.079772718188707 55.86826992723632, -4.080134174033818 55.87718253487298, -4.078432178486257 55.88054282825273, -4.081933600364703 55.88415577265539, -4.0893425165254165 55.88152460962655, -4.094956461288081 55.885240568252755, -4.106148639689392 55.88448781364562, -4.10730403523429 55.887454490296506, -4.1414907995214865 55.8828865824709, -4.158431043297432 55.88587359482474, -4.163118871899686 55.896236286601045, -4.173354601345871 55.89701848966342, -4.180429197972781 55.90491352281493, -4.193494447794818 55.90829970439967, -4.19450277606756 55.91311953619079, -4.17160301888187 55.91673493146343, -4.156382895520525 55.915045077761896, -4.155401912566388 55.91319831211784, -4.131752119230471 55.9142059626004, -4.124694917238227 55.911568903103834, -4.115870727589658 55.91451138341435, -4.11137696808203 55.92327456271209, -4.075517115889554 55.92463913132737, -4.057934068465235 55.92357815818314, -4.043278854770712 55.92433181901912, -4.036207882628783 55.925803251553184, -4.03301200539256 55.92942202353872, -4.026211558551559 55.92825258823399, -4.01895460133026 55.92953111784271, -4.012224772657672 55.928447801601536, -4.00263448041763 55.93024331962703, -4.002054472016132 55.9225996031431, -4.010499887156194 55.91814109455094, -4.00876801787559 55.912164357612625, -4.01931513254055 55.91021327809092, -4.014189846117302 55.905082211990674, -4.0204804470208995 55.8883829976421, -4.02048505621339 55.88071981426778, -4.01256163327167 55.87751008749816, -4.002829681112871 55.87646820810538, -4.008986474076195 55.8723012624365, -4.0082283623185 55.8660084286697, -4.004588717199245 55.86432068938394, -4.003936936879941 55.858307396372474, -3.996344650906551 55.857450377772935, -3.993348443111256 55.83791382571344, -4.051438571015526 55.84448519979863, -4.076495254423147 55.848400999420235, -4.0834553733422965 55.850905701708385))
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>