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/S19001333 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S19001333 geosparql: asWKT POLYGON ((-4.324466193097415 55.93183054085526, -4.3214754246190665 55.93134771299179, -4.30978380108206 55.932619126414636, -4.310523021012386 55.928363158068066, -4.317752784007168 55.92155853246044, -4.3191759589759835 55.91939269869364, -4.312436918654887 55.922332558676345, -4.313092148743941 55.923389764085975, -4.312096196087324 55.924136460225945, -4.310503123465346 55.92399560444885, -4.3094544689848915 55.92170549070564, -4.30696727795364 55.92213859356128, -4.305971877013689 55.92079739713434, -4.3085087761885115 55.91926065027692, -4.305730161215585 55.91774889712104, -4.3024026229040695 55.91672381391269, -4.302119827204292 55.915234095414355, -4.305904207645137 55.91494680218559, -4.306046519493004 55.91337504321976, -4.303876330527771 55.913208966038276, -4.30337765237048 55.91181624742769, -4.303360094738377 55.90982498951234, -4.30884983785018 55.910147682349496, -4.311283488038181 55.90921207478446, -4.313192567087077 55.90709029064982, -4.315257812698513 55.9065891125256, -4.317373592320721 55.90442733715302, -4.319535025851894 55.90303399663507, -4.320220979666278 55.901876052826054, -4.319765531624087 55.90126809150601, -4.327968460994018 55.89997628069292, -4.330817880886298 55.90144844248595, -4.332730112174162 55.90292212862423, -4.33488663014689 55.903852795753174, -4.336795275736658 55.90419854616234, -4.340635504225072 55.90418040115571, -4.34558575967825 55.90564101995356, -4.348941482745013 55.90620705699972, -4.348623263120054 55.90877639799636, -4.349416228266177 55.91003481049195, -4.3476008737783935 55.911927185481986, -4.348411637462306 55.91268555365646, -4.347520038882044 55.91384578395185, -4.348687999861322 55.91516445629805, -4.348070455361821 55.91670233264522, -4.350467371051492 55.91724091093974, -4.349924708573583 55.9173266410851, -4.349121384079408 55.92124259334177, -4.352477109852809 55.92115155291855, -4.355687653839024 55.92130586461358, -4.354994522475398 55.92228080037702, -4.360711361709396 55.923375644148315, -4.361522018783898 55.923905079961074, -4.360220709441414 55.92467214226999, -4.359769891327493 55.92579339768288, -4.358478768869054 55.92562041883645, -4.3578348768482496 55.92714266230097, -4.359909001437006 55.92913412604447, -4.360173249497587 55.933428439933664, -4.3591603513527355 55.93443360304331, -4.360048475739093 55.93539861526043, -4.360074653427933 55.93705811609048, -4.355245175456146 55.93638630531504, -4.346994104380137 55.936355350643254, -4.344202466153246 55.93467392602099, -4.343214953322382 55.93290418617525, -4.336218396101857 55.93317195525938, -4.329940501539919 55.93127769744276, -4.324466193097415 55.93183054085526))
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>