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/S02000785 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000785 geosparql: asWKT POLYGON ((-4.818604173078322 55.962201049450165, -4.817733700344744 55.96301197781386, -4.81576492919418 55.96238284946236, -4.81379075972128 55.9590302074434, -4.811707212651871 55.95747779544879, -4.809625058225749 55.95748936483043, -4.810424007693774 55.9579760199412, -4.808869016244441 55.95865484544588, -4.810069628054384 55.9579266336474, -4.80893992913562 55.95710050236239, -4.807107743998426 55.95746587441868, -4.807285513661408 55.95792923519037, -4.807044394330423 55.95747630182225, -4.805717984762077 55.957524514923314, -4.804669272105798 55.956982106634904, -4.802281917061257 55.95698252699919, -4.802173382373812 55.95658948984085, -4.804442720074899 55.95628738433486, -4.803311853920277 55.95471163581553, -4.805436582353593 55.95271514371534, -4.803459411703699 55.95297318356302, -4.80323125844268 55.952385636330014, -4.802559055988328 55.95245700185111, -4.798153736886638 55.953732578240114, -4.797726780085713 55.95218821451275, -4.799557554603418 55.951570699525554, -4.799749737082522 55.94850288481811, -4.801733964070415 55.944693942800576, -4.804900838892325 55.94527071953502, -4.804367978753006 55.943112711464714, -4.805356225524852 55.9429990176271, -4.805484490622901 55.94256996312726, -4.803953034408978 55.9419871819815, -4.805712741371519 55.941659535217454, -4.806399287886685 55.94258015297924, -4.809981791228598 55.942183213572214, -4.810948501374676 55.9424358744703, -4.813641653090176 55.94474781891274, -4.814292751494803 55.946573909093956, -4.818674850239101 55.94617319745558, -4.818544458956343 55.949570926706805, -4.81836921342263 55.94993671323347, -4.821149680160619 55.950208462955196, -4.820960170400779 55.95119873104053, -4.821622793954693 55.95149816093854, -4.821084552521204 55.95180712789205, -4.821858171083925 55.95231074740064, -4.821341799832473 55.95270011345256, -4.817413335289197 55.953095677737764, -4.814688374825425 55.95419693741669, -4.815823697692512 55.95515235818959, -4.815418260019835 55.95584693305773, -4.816973978840206 55.95696720425693, -4.81624631205645 55.957320143571785, -4.816654668048533 55.95786811107302, -4.817699331410916 55.95813185415122, -4.816918557300993 55.95917153808589, -4.818921570980966 55.959955575556656, -4.818569624380769 55.960329428408265, -4.819612142687733 55.96102895701288, -4.81862718713569 55.961283458081155, -4.818909395566525 55.96160537024239, -4.819808377288512 55.96136446078165, -4.818604173078322 55.962201049450165))
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>