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/S16000109 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S16000109 geosparql: asWKT POLYGON ((-3.42535054740812 55.993828191712545, -3.411162047960248 55.992213171378964, -3.4071765833441696 55.99389233199418, -3.3945192242789286 55.989898259899746, -3.376769872491201 55.99148157325756, -3.3684973980747137 55.99524252839382, -3.3659836873906577 55.99870471819564, -3.3519674377839266 56.0015854584432, -3.3450798469830985 55.99728660299827, -3.3337926081632454 55.992651366107495, -3.3301930425117856 55.988159722709014, -3.317557167736004 55.982949009925186, -3.3085281803081865 55.98303081913539, -3.289357179611748 55.97903480343945, -3.2839982599602595 55.98000997063859, -3.2631753748190198 55.97914148571048, -3.252806200693912 55.9829511425056, -3.2560802796860497 55.97764963220996, -3.2485671173976014 55.967193114198004, -3.2394757491936925 55.96816908487384, -3.244174137350822 55.96120558629608, -3.2379944903982696 55.95896127825835, -3.242196619151761 55.956083156092575, -3.249791585196547 55.955132935170454, -3.2638407567657532 55.94814440300151, -3.2673575881700003 55.94467398207838, -3.264833064615193 55.94159540190893, -3.2695148938315146 55.938814787572646, -3.266768844989829 55.9345838505251, -3.288934332419728 55.92974783331852, -3.315613707541094 55.926696427338044, -3.3170555668033566 55.9221629663157, -3.323862043738105 55.91959285884108, -3.3342932173441895 55.91902204747957, -3.343743940429415 55.92226280861579, -3.3497887442480883 55.92679023200456, -3.356753868859977 55.929484908920976, -3.3701659190358644 55.93036437412823, -3.382798342373583 55.92875399260171, -3.393907330855817 55.92601662762401, -3.411848002232141 55.92503323561362, -3.428188199975427 55.9253749251432, -3.4280879638885113 55.92548038329681, -3.4293207896723072 55.93236000703765, -3.424201101378737 55.93483104385147, -3.4329067736676313 55.9373980067328, -3.442628334847681 55.943184640679526, -3.4495325200314544 55.95094013332965, -3.4346835413852417 55.95094407795428, -3.424916896622634 55.95375948726808, -3.4259530564859633 55.959331191697416, -3.443376639665281 55.96148982325643, -3.442107282827327 55.969139883353236, -3.4448589742444753 55.96988496876036, -3.4482693398300746 55.977579843852546, -3.44193780912156 55.9778041939553, -3.429705621162748 55.9818168353229, -3.4319558242257617 55.98922644637445, -3.426709163777484 55.98993065905521, -3.42535054740812 55.993828191712545))
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>