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/S02002476 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002476 geosparql: asWKT POLYGON ((-4.58165906226059 56.00420128678524, -4.580156869406137 56.004835236383705, -4.579511376456351 56.004251544161626, -4.578385824737873 56.004638871785545, -4.577169756269186 56.004865505358744, -4.576872682866833 56.004353195035144, -4.575659568104832 56.00465093748832, -4.576528174865555 56.00541930091093, -4.577152498225655 56.00622984269315, -4.576813304644968 56.00672794445062, -4.575985438958856 56.006938718664735, -4.574954513713618 56.006831795162086, -4.57576940004301 56.006059769423956, -4.572228115252396 56.00684378610949, -4.573103446181759 56.00722098925942, -4.573734553064254 56.00715364069264, -4.5746461076161795 56.008465078238046, -4.574863704387903 56.009126557212134, -4.57381621998655 56.009012707169624, -4.5736990921299885 56.00845789315763, -4.569746095768005 56.00667086048101, -4.569890007018414 56.00762958165471, -4.5670111446670525 56.00800415728946, -4.568287554308944 56.00620692004762, -4.567595401741371 56.00582157110033, -4.567830270453092 56.004634551326085, -4.568887610402747 56.004855194221086, -4.569251735947552 56.00429933247957, -4.570606824545778 56.00357286498867, -4.570335267094304 56.00298245870313, -4.571068545963725 56.00290197269847, -4.571414145854417 56.002080138041514, -4.572385695564745 56.001523517652195, -4.572885656995331 56.00221356594512, -4.57366800486932 56.002202847820094, -4.574769351640178 56.00139788049528, -4.575146770654514 56.0003204135822, -4.575057616066947 55.999944777313935, -4.572174431894495 55.99965217128549, -4.572470068477971 55.99937275403923, -4.574486367662412 55.99925038455382, -4.574811646261785 55.999718833277484, -4.576000341954092 55.999632342255744, -4.576311007841101 55.99985085555412, -4.578253484876913 55.99882004097224, -4.578115675447007 55.99865846010679, -4.580062840674374 55.99803348269324, -4.580950906564849 55.99812274346382, -4.580138195658548 55.996036548195335, -4.57952971487441 55.99412583481102, -4.584118049578887 55.992987833660926, -4.583663548124878 55.99230034543663, -4.582428120838307 55.99221808936778, -4.582580804566801 55.99147251675078, -4.581188076923409 55.99107207683751, -4.5810802706328895 55.990772928049424, -4.578430072835894 55.990859206705935, -4.577971632979152 55.98975427508032, -4.576611841142663 55.99004336303917, -4.576583050820213 55.989369867096386, -4.579300129578957 55.98886050535877, -4.578992683976958 55.988231895542825, -4.5800633431561 55.98830663342846, -4.5816645301384975 55.98806178439313, -4.582566415457905 55.98820198272618, -4.582861830363835 55.98874673472197, -4.583590920325636 55.988656874865306, -4.5836730574003495 55.98820574692081, -4.585470807356091 55.987926391294636, -4.586054447407035 55.98875189867422, -4.58652376692704 55.98887397526524, -4.589021922564654 55.98834513808534, -4.589438840897641 55.988345365064156, -4.590634171757513 55.989434766719164, -4.594348538300832 55.99317657422449, -4.595245548274592 55.9945957871111, -4.59603534945837 55.99623236566108, -4.597290588817822 55.99854343342643, -4.598277896121251 55.99987085035991, -4.599848334902366 55.999551492586704, -4.599555257527559 56.000331716091594, -4.597857384161066 56.000257864681224, -4.596482029425095 56.00069966626156, -4.5949307326290425 56.0016095645384, -4.594086439757722 56.00146914841842, -4.594202860861032 56.002248664653195, -4.598422278195694 56.004112561412065, -4.598306010854559 56.004369349956846, -4.59662897967594 56.004868716526495, -4.595124529817628 56.00546203685652, -4.593867868411925 56.00722168427011, -4.593125969614361 56.006979305714644, -4.5937248462599305 56.00636449986317, -4.5936042483359625 56.00601092854774, -4.592260405146851 56.005572808162725, -4.591070600335677 56.006102510723764, -4.590117346530036 56.00590314820961, -4.590018576537567 56.00646221704885, -4.591835053970687 56.007137686449376, -4.591969935173559 56.00733348717605, -4.590886586848244 56.00809778497674, -4.590735589027098 56.008511715666636, -4.58849040053126 56.00790096053107, -4.585921840783636 56.006471393734564, -4.5838717151802415 56.004803058930236, -4.583261774037331 56.003968717415404, -4.582323211115572 56.00343283302461, -4.581364985916286 56.003597256125246, -4.58165906226059 56.00420128678524))
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 lang='en'>
<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>