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/S22000054 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S22000054 geosparql: asWKT MULTIPOLYGON (((-4.691981589514221 56.27883950032864, -4.701111159893055 56.29957407474154, -4.720519313932954 56.30289244364951, -4.698906639840793 56.28184391972788, -4.71619624173899 56.24529297253644, -4.694424119336124 56.21720969029747, -4.708132697059716 56.20259739920892, -4.663781022673584 56.16497481966478, -4.670930540282093 56.157288150287584, -4.630740951943234 56.09792597621785, -4.640518376083467 56.0695729752376, -4.630296583560779 56.07111087137417, -4.626850973708359 56.05887020308464, -4.642461711454033 56.04771494983519, -4.628025469135004 56.025698690202645, -4.582358105943291 56.00332284776833, -4.581347740887229 56.00359057469623, -4.59378054166605 56.031964925262336, -4.581616187887649 56.0447028786645, -4.564442464802195 56.050882384301445, -4.565603194570385 56.05265413906642, -4.535552797633327 56.05421671616365, -4.514732521118399 56.063687363108016, -4.506307522841349 56.05859070816502, -4.504863137855737 56.06757359250745, -4.475624998641865 56.061291993210304, -4.481847666921231 56.050744868560955, -4.49890478287809 56.047632134808495, -4.482394658579791 56.011521136326984, -4.469704827901368 56.001959635304566, -4.447801606971358 56.004095363015345, -4.427668713923739 55.981175012903705, -4.464123039308278 55.96412401025127, -4.470930383509838 55.92905248697202, -4.50556492361086 55.930960276037275, -4.56343834211777 55.93532943177239, -4.574041928122833 55.9380104609231, -4.577483454303643 55.938770225218086, -4.607211896201198 55.94609577985727, -4.609555985132087 55.946795362919616, -4.654636691265558 55.96023580884257, -4.666437301916127 55.95836444950906, -4.6919009598268575 55.98352686888732, -4.700175456079396 55.991695533056394, -4.700897417674453 55.991889124517265, -4.787563220571868 56.01508365171735, -4.793224035159345 56.00130720054303, -4.773596238696023 56.0010714028124, -4.771013964346754 56.0010401497805, -4.767799827784621 55.98786054864376, -4.77961779374164 55.98717047934002, -4.819982745541064 55.98480500632205, -4.837068695197639 55.98379968390517, -4.852033175633203 55.98941879713134, -4.8664066163713 56.01227395125127, -4.878809590114905 56.053145912738955, -4.874151885580678 56.086210256359195, -4.8172768912689135 56.15033889148806, -4.849203274054951 56.156490216004975, -4.835975823735704 56.1767403667487, -4.853864379222661 56.17763165486507, -4.874242357822295 56.19778805160789, -4.857064574344122 56.209835711614645, -4.84822827806564 56.24506802710066, -4.805338686370432 56.277301152815824, -4.823560724971332 56.27419904781419, -4.838418351803605 56.29374137385381, -4.781363979626281 56.312231363704754, -4.785787258474775 56.3237346898821, -4.739897067992861 56.33191421044886, -4.721580320751671 56.32143827507096, -4.715903708525148 56.329984645855916, -4.695701000926387 56.32247189035389, -4.660785192572915 56.32383392728511, -4.6821084423511765 56.29783925267132, -4.660305512542569 56.28114298954766, -4.691981589514221 56.27883950032864)), ((-4.582482950209315 56.004352343378144, -4.581914659197254 56.00381103790403, -4.582528486328567 56.00421746692951, -4.582482950209315 56.004352343378144)))
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>