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/S01002394 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01002394 geosparql: asWKT POLYGON ((-3.902685525728291 55.96221906666582, -3.904328272492979 55.962067873706175, -3.905758940371307 55.960536004821314, -3.907395519990435 55.959935535958834, -3.911418524755686 55.959325002193495, -3.911782075606502 55.96020004815272, -3.913040396622854 55.960701738300486, -3.915515869404379 55.96086996634324, -3.918671696502446 55.961836346329505, -3.921684130527346 55.96443146095359, -3.924590397025431 55.96488036075961, -3.925847590720723 55.9647079433119, -3.929964346537538 55.96307985098344, -3.933179925721522 55.96298450407748, -3.933679457792262 55.9633630904481, -3.935069176693429 55.96231679354123, -3.937897335805054 55.96312607223635, -3.938052205368392 55.96430088713941, -3.940975730359596 55.96509062323129, -3.940080765579176 55.9664167700625, -3.940618980300489 55.968205623831146, -3.940478203007233 55.96859426687553, -3.941560853409741 55.96877491768037, -3.942081692756728 55.969575507765605, -3.944711203178302 55.96960592694876, -3.945603549597054 55.970463545219836, -3.946666427272422 55.97024905105736, -3.949739907039549 55.97145858775553, -3.94871293392489 55.972706006205904, -3.949366347872826 55.9732708169161, -3.949038246818871 55.97407581963227, -3.947057102998896 55.97483505898673, -3.945965898855416 55.97608346730239, -3.946094456886752 55.97736652769702, -3.943221693924448 55.98048529347954, -3.944591814571041 55.98159599741517, -3.945384280085615 55.98237431439975, -3.950431799738162 55.983930101698334, -3.950646621643137 55.984376034007724, -3.953007725800058 55.98414991990483, -3.955252899742205 55.9851208249179, -3.95434370374361 55.985520312654366, -3.952617061481441 55.98482423809438, -3.951190251341885 55.98503993407904, -3.947262358780059 55.986445345974325, -3.946281871141695 55.98719964136729, -3.946715564043902 55.98749364898935, -3.945087705398701 55.98795969597113, -3.943990735679395 55.987824230932375, -3.944197992026214 55.989717140243584, -3.941893004150249 55.99024715078472, -3.939916085866026 55.988733176410555, -3.935901115273123 55.990198217861646, -3.936303317277801 55.99048870237894, -3.932463168104544 55.990933458664564, -3.931554833713297 55.99138445124415, -3.93194043584226 55.99171110150435, -3.934017311982644 55.99228572501261, -3.928472009233163 55.99352459541005, -3.929142647119347 55.99485140444163, -3.927751174918881 55.993956542717115, -3.9236222991165 55.99602986623115, -3.922596261382562 55.9954290959873, -3.920447785465895 55.99615768772026, -3.91970731703372 55.995681696257265, -3.918421244512744 55.99622208655125, -3.917990216461991 55.99597802408939, -3.921738850423759 55.99437819560346, -3.919406490908366 55.99302852779223, -3.917885313208485 55.99094717704046, -3.915011109087226 55.989239486998905, -3.915835708843493 55.98905592700306, -3.914669690340427 55.98622530338961, -3.915782592033335 55.98571374711694, -3.915197650665283 55.983934509809224, -3.911301192722202 55.98456115440207, -3.910801225898486 55.983849981694725, -3.912846607226534 55.981122279289906, -3.915521544607816 55.97946314521874, -3.913876661098552 55.97859306094618, -3.908793297465415 55.98087336019063, -3.906827361839588 55.97926625929275, -3.909744339161902 55.97795022490594, -3.906668587652002 55.97577215876633, -3.90741528019274 55.97498775768809, -3.905315824516262 55.972054668918624, -3.902685525728291 55.96221906666582))
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>