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/S01005549 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01005549 geosparql: asWKT POLYGON ((-4.520846594320316 55.44932116768967, -4.521296356052217 55.44893437584543, -4.524454291472001 55.44860856446306, -4.524076913166814 55.44745864421172, -4.52570322190669 55.447117636437426, -4.524353701156491 55.44629158167392, -4.523925673149361 55.44528470378099, -4.520986836888083 55.44210043548807, -4.517285962696054 55.441771478734736, -4.516994123287294 55.44055102759059, -4.516402334718324 55.44094368715875, -4.516772715670236 55.442254533918394, -4.514868839408773 55.44251440096639, -4.515054813370289 55.44453253022442, -4.513687971906559 55.444697833621696, -4.513429585215682 55.44422279194129, -4.511954067546796 55.44419266059979, -4.507345147809587 55.44471188983491, -4.507138515127861 55.444389575190925, -4.503547761409314 55.4451538274465, -4.503743198812423 55.44548820418707, -4.497091420634857 55.44679732304978, -4.497414189666037 55.447150273887274, -4.494280494415249 55.44786139794569, -4.494255979816353 55.44697203371451, -4.490406401346038 55.4446779347976, -4.490569541614152 55.44420109817289, -4.492673540113244 55.44382553284201, -4.492773436213375 55.44269717834964, -4.493824893241121 55.44239667468755, -4.494310176435835 55.44131714228581, -4.4917828462204 55.44060463929799, -4.492036967472035 55.439619711291016, -4.493316869243229 55.438604883744965, -4.491981893796562 55.437993909299486, -4.489141403915785 55.43807872568355, -4.487563333749899 55.436367063384886, -4.485700725364499 55.43591057049648, -4.484668819069843 55.43659669224496, -4.483118635006461 55.43683491582017, -4.482628611718527 55.43532580658188, -4.480547897284014 55.434666928408426, -4.477418280360826 55.43517979894513, -4.476573800002812 55.433812675952076, -4.478183779441408 55.43276435715713, -4.481114165372861 55.43210270268383, -4.480983948559881 55.43153906845408, -4.479782455870898 55.43103311846906, -4.477991079565142 55.43170761868024, -4.477232265666219 55.430949982466714, -4.475178846561926 55.430721910975834, -4.47332849945599 55.43020206586988, -4.47206209918079 55.43042542230971, -4.47108457447885 55.42995980255836, -4.471516806721741 55.42878256253658, -4.46851083600255 55.42823206829392, -4.466647978507382 55.4297887161104, -4.462876614198311 55.43090740006785, -4.458565993329469 55.43022113979587, -4.456338538898739 55.43075128273934, -4.454329074966456 55.43071972525369, -4.454642014401973 55.42914046440588, -4.453716915865146 55.428242210008364, -4.452674424724795 55.42901815547232, -4.451857051766427 55.42883680237379, -4.452130193814045 55.426359501450456, -4.454110279900631 55.42541194069828, -4.45321443335598 55.424728818882556, -4.452696730520988 55.42351677528315, -4.453240857395725 55.42260701292517, -4.452323183721481 55.42182545008776, -4.455174674411263 55.42142666052677, -4.455062669585811 55.41962222824173, -4.454467927004604 55.41895104638058, -4.453022231744273 55.41882725793573, -4.451091446547712 55.41776040223511, -4.451644640734195 55.41674260250952, -4.453207071000705 55.415947240579584, -4.45015407951324 55.41512759660705, -4.450897536977429 55.414375640217514, -4.451106989979864 55.41189062474576, -4.452937103740698 55.411593250671615, -4.453771032355856 55.411028217718574, -4.454362871246506 55.409362457043066, -4.455869845826305 55.40920635220261, -4.456315347314558 55.40874797707034, -4.457174083667987 55.40883858040464, -4.458229907620351 55.40828703197827, -4.45954215282119 55.40855725609829, -4.462473181328588 55.408183649778984, -4.465287671636947 55.407461771219936, -4.466975447327214 55.40589967404235, -4.46656008267195 55.404811457949336, -4.465264491134765 55.40404660600685, -4.468154361494493 55.40276584893952, -4.468829084785527 55.40345332714357, -4.476798225982074 55.401053960544786, -4.48152462445479 55.400859219927845, -4.484406971478713 55.40022539890341, -4.48745033171779 55.40162326159102, -4.501359398511648 55.399043699479236, -4.501958138741307 55.40251902196678, -4.51001801067536 55.4053200178638, -4.516789297460126 55.4089560079368, -4.521611006255481 55.407517329842555, -4.527590835957782 55.40912863518262, -4.522939784830984 55.411013459138736, -4.525250966307693 55.41380614848815, -4.527138789839829 55.41417162588238, -4.52838797038198 55.415916559688554, -4.529220616402113 55.41608809746517, -4.531328768293857 55.41816578595152, -4.530794273384345 55.41944425169723, -4.531503764407329 55.4216407704996, -4.533578539687099 55.421957338241235, -4.535576753849163 55.42403723585233, -4.540034762864555 55.423315493328126, -4.541864243933698 55.42423925893589, -4.542741032181722 55.423618785854195, -4.542374000149028 55.42282642671065, -4.543324861976992 55.42163812177287, -4.545788270110427 55.420625061179486, -4.551475657762385 55.42255592067026, -4.552073683003207 55.42349624075648, -4.550959992926276 55.424607115094645, -4.552157992684046 55.42627198811593, -4.553562991820852 55.426979725199885, -4.551349350633714 55.4284371624313, -4.547090011309069 55.42881360488743, -4.545439523819542 55.430429983164224, -4.540808953788798 55.43044539614991, -4.537591427390732 55.4317886026363, -4.537825359943712 55.43297024963708, -4.536464737975654 55.434184983019286, -4.536998275995702 55.43634017833246, -4.538818544502239 55.438342822654604, -4.541433204025989 55.43892670483242, -4.543109782556392 55.44041093240279, -4.542490464523343 55.4418440174289, -4.543464682801172 55.442731613075516, -4.542237949120097 55.44381778161268, -4.539578754441128 55.444268532152925, -4.540526427987406 55.44744880588487, -4.540207780702824 55.44864192511612, -4.538142318431991 55.44997018176898, -4.536481395798341 55.45021137755955, -4.536135521403275 55.4509825834122, -4.522988697117274 55.452899400415895, -4.520846594320316 55.44932116768967))
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>