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/S02002042 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002042 geosparql: asWKT POLYGON ((-4.773664454915758 55.95981951780047, -4.774530333256123 55.958979334209005, -4.773759773076722 55.95877439332236, -4.773190453093857 55.95913567860332, -4.7725687364465905 55.95884066482278, -4.772923904874625 55.95832617480693, -4.770497900917735 55.95660505720071, -4.7712229503614285 55.95619565104376, -4.771942554037379 55.95663487515349, -4.773195178120268 55.95588651896968, -4.772879767172366 55.95519743999405, -4.773760124782562 55.95470379368028, -4.773399683639265 55.953942023094406, -4.774434614582974 55.95340276011672, -4.775091301687229 55.95375805377019, -4.776184567170288 55.95342500076293, -4.773742124960844 55.9520107336385, -4.776462133768254 55.95083193431749, -4.776455587325668 55.95150998160082, -4.7770968624388885 55.95188290620241, -4.779173283275443 55.95074498548293, -4.777154901570428 55.949586929096746, -4.776479083351081 55.949087243557756, -4.777341649143974 55.94880975266971, -4.778778890630122 55.94884917310916, -4.780622296051985 55.949935056183804, -4.781615537917931 55.94943314757428, -4.7845744394212 55.95070558772533, -4.7901762089969875 55.95152259887856, -4.79044923416199 55.95130967140486, -4.790310394689179 55.94968928301421, -4.79014073214494 55.94924481486287, -4.789517797686859 55.948993923097056, -4.789951562403675 55.94786631146443, -4.789310867331557 55.94764454030223, -4.789449144110048 55.947051217004834, -4.790494927047205 55.94704797012068, -4.7902590157483695 55.945806112127514, -4.789107008782809 55.94478001052299, -4.789818882820636 55.94463485846409, -4.792218217041093 55.945175200813395, -4.792406406542588 55.94534172795795, -4.7939363760323515 55.94859024704716, -4.79469475382894 55.948658366766544, -4.794150531934436 55.95073301262366, -4.793828460328146 55.95155344148904, -4.795945771924041 55.95196824123524, -4.796923904129942 55.95234732142766, -4.797726760633157 55.95218821135921, -4.798153740409602 55.95373258265443, -4.801473903127651 55.95266731417578, -4.803231257102815 55.95238563995602, -4.80345941643723 55.95297318255636, -4.805436575501074 55.95271513757933, -4.804476507687672 55.954108098808355, -4.802850074096045 55.95500299831283, -4.803871574025133 55.95616578339726, -4.802173383908559 55.956589488907056, -4.80231478351208 55.957001192101934, -4.8016267773138965 55.95747293566296, -4.802166752126949 55.95860312013657, -4.802093137279094 55.959601647495084, -4.801454292323942 55.9605617978735, -4.8001249790625975 55.9614585444683, -4.798657115405112 55.96263437861352, -4.797102040156411 55.96224813554757, -4.795796270310211 55.96256272787429, -4.793488021730326 55.962953981074314, -4.792164828553484 55.96340555787945, -4.791401614020437 55.963355443326364, -4.7906713584544205 55.96285064726587, -4.78876676664612 55.96299536194332, -4.784672483448915 55.96246239121639, -4.782442708347746 55.96207324109012, -4.773664454915758 55.95981951780047))
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>