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/S02001000 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001000 geosparql: asWKT POLYGON ((-4.435340266557376 55.84738568623521, -4.430629850165714 55.8474249514858, -4.426825548041139 55.84739222676632, -4.426264526310647 55.84813131824714, -4.426326420708004 55.847330176822375, -4.420177908703895 55.84762209374537, -4.419244403802749 55.84725398483886, -4.419173206198577 55.8463655933492, -4.417034780789444 55.84665929389736, -4.416322974626776 55.84548688747729, -4.415329300197169 55.84620746136446, -4.41563471369316 55.846643398917216, -4.415328232772561 55.84696245929051, -4.414766832677908 55.84674427941974, -4.414873080756078 55.84606362376328, -4.413726103136369 55.84560080008827, -4.412627841365858 55.844122678469546, -4.413556544869885 55.84384621128159, -4.413152285642547 55.84330218023288, -4.41205840889781 55.84326384576651, -4.411711574406569 55.84273724644253, -4.410855630884204 55.84312735383115, -4.40715979341764 55.84223323474655, -4.406018300033049 55.842372413326316, -4.405245190586985 55.84097644998365, -4.408062282577504 55.83874625972066, -4.409926229544713 55.83815249486934, -4.412698091726115 55.83803522387537, -4.414667672987179 55.83837405625479, -4.418319553235342 55.84008188387412, -4.420764556358929 55.84035733581101, -4.423067175629417 55.84014122082959, -4.423234256688764 55.83942278248102, -4.424598346927896 55.839197794919336, -4.423243896149605 55.838138028259536, -4.422145062369871 55.83852087314451, -4.422040893259712 55.83802232350049, -4.420982387284052 55.83666802419198, -4.422026042934113 55.836243019185794, -4.423284449438677 55.836707736731896, -4.423037839850252 55.836251772510586, -4.423668621113738 55.836190303770934, -4.424569285162861 55.837073709010525, -4.425094915656578 55.83628139618811, -4.427326934769334 55.83665671717528, -4.427331769498393 55.837576457181704, -4.434619110887242 55.83917603888085, -4.434211399110439 55.83955261397952, -4.435350555876411 55.8398895699249, -4.436553516496494 55.84131995740111, -4.436116403948682 55.84166299413187, -4.436799619004558 55.841604516680995, -4.437018710068216 55.84255289622507, -4.43640256180437 55.84215166073564, -4.436518520220393 55.84247292616121, -4.435226231224012 55.842498524681815, -4.435976435517325 55.843399545322, -4.434910598138174 55.84445392520116, -4.435258762741113 55.844850591448434, -4.436333709022553 55.844624702446474, -4.438536633374826 55.84504518709281, -4.437035202565462 55.845552149343185, -4.437688922946903 55.84636846564118, -4.435012865161312 55.846493381018796, -4.435340266557376 55.84738568623521))
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>