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/S14000028 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S14000028 geosparql: asWKT POLYGON ((-3.820161724820584 56.098354323687154, -3.8090676753173978 56.10276069665879, -3.7989485840769133 56.10405969727339, -3.7903664953199305 56.093835060277726, -3.7739109599065497 56.08126733963163, -3.762081648362494 56.073943236603775, -3.7354686617309536 56.06397823591627, -3.7277446770355924 56.04743892534156, -3.7284547399664296 56.03233493463703, -3.7167473571779617 56.026677227380056, -3.7306502714240595 56.02426601295964, -3.7358942324818782 56.02165730283003, -3.7543013419422895 56.02027892143436, -3.7613623496860793 56.014283287853736, -3.7449513441322764 56.00638949071368, -3.7385113895119786 56.00584604476777, -3.7268562129963083 55.99732827938788, -3.718284715969973 55.99687743326286, -3.7088982446577266 56.00178856015291, -3.701212983145143 55.99995474236456, -3.6788242455873807 55.99897396233111, -3.6622712182640975 55.992262807709245, -3.6548026018169684 55.98553386730434, -3.6719475289802594 55.987967768531114, -3.6735404510286718 55.980089574079585, -3.6977942078471426 55.98324627094085, -3.7153860125144766 55.97857247556894, -3.738229099741202 55.98684594625499, -3.752923314212315 55.98936966107332, -3.760340946175685 55.977053378371416, -3.7522935613598674 55.97314760563689, -3.786021725952965 55.96549765356907, -3.822475379669984 55.96014981529561, -3.8603478951780574 55.959238675525675, -3.8678215035080803 55.96260517364104, -3.8985729440449908 55.96114031718717, -3.911083749701173 55.959293350738335, -3.9241969477317102 55.96482989043643, -3.935875975696876 55.96252788638076, -3.940505377168232 55.96859294024051, -3.9497298026360306 55.9714488623153, -3.9432682557750547 55.98045490334877, -3.9582454929875976 55.98657228502606, -3.964818264926909 55.987107523921026, -3.985239176101651 55.982787471016124, -3.9893320592832913 55.987290319953054, -3.98026392421423 55.99063382766432, -3.979641301356951 55.999591878545424, -3.9913202566682378 56.0059685484239, -4.004806210945882 56.00720491730457, -4.019390145146733 56.00594958023046, -4.0303499686327084 56.009739017283, -4.020953113384413 56.02254676135514, -4.020129802539987 56.02804306015321, -4.010407682959478 56.03088717421942, -3.9946296854353514 56.03870836462834, -3.978374847055175 56.04034962500125, -3.973357881245058 56.048615006020015, -3.961275615560898 56.04818673101658, -3.93933867149634 56.05074606051762, -3.9200141963930255 56.04834701112855, -3.914634228084825 56.04930052633505, -3.896737740200674 56.04542615923827, -3.8908214360297833 56.04857957362694, -3.8797525971748823 56.04701076892959, -3.865618384659019 56.04963819575189, -3.8244025668686246 56.051161357976085, -3.8117691409906507 56.06227127358409, -3.8021415206318467 56.06334055259727, -3.800228404279611 56.0680943498233, -3.8077858573123664 56.072911024428464, -3.8178569888234652 56.075719557223415, -3.8358972234338164 56.08465356138, -3.823147767332748 56.09108620871608, -3.820161724820584 56.098354323687154))
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>