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/S01000269 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01000269 geosparql: asWKT POLYGON ((-2.349790143768675 56.78880673460103, -2.353606569074384 56.787340628639086, -2.358397598959442 56.78684183265268, -2.361719134426046 56.78332863689891, -2.365367805672286 56.783228159798334, -2.366771124971909 56.7828018115941, -2.370806036240384 56.77872928296051, -2.375901530762784 56.77721570629317, -2.376235956469794 56.773529462265536, -2.377524895526546 56.773166220363066, -2.382408121873417 56.77389697825776, -2.38746617939413 56.775696053799365, -2.39025797631777 56.775094425117665, -2.39614697913053 56.774950080991516, -2.400683726481791 56.7738216114667, -2.405730462315976 56.771460556848865, -2.414521817581247 56.76454117572236, -2.415611164861535 56.76690024776045, -2.41832279876889 56.76801413591353, -2.415944877280002 56.7704476775851, -2.415652692824489 56.7718231842651, -2.412419802827972 56.772741236192424, -2.412030783543265 56.77307491642911, -2.412901310236917 56.773039671759264, -2.412122305080089 56.773614373590405, -2.411003200687521 56.77304522168602, -2.410378000000427 56.77357349001999, -2.411351975944122 56.77384076763656, -2.412267400621926 56.774929432172144, -2.414525102520612 56.77532632191229, -2.414205051575016 56.77564534632512, -2.415057311581186 56.77616319106899, -2.414621055710001 56.77667627528972, -2.416596666211629 56.776833803454494, -2.417785088888837 56.776276462308886, -2.418869774651433 56.77674375508753, -2.418671939474587 56.7762702837132, -2.421556672874257 56.77513488554703, -2.422954972846897 56.775499850801275, -2.42393041850134 56.77493955267623, -2.424850442213878 56.775268815736815, -2.426165884206172 56.77439291241463, -2.423711692542478 56.7729549112365, -2.425306228014621 56.77216790326182, -2.42469458907497 56.77161300120106, -2.431717460931277 56.76910938991247, -2.436315973549453 56.76640726700273, -2.436668263085008 56.76716066114112, -2.439420349430549 56.76608193349106, -2.439447393487581 56.76559671931073, -2.441243800692398 56.76533883086714, -2.442827197777956 56.76783067619827, -2.446769253927751 56.76640617584642, -2.448574331110275 56.766875827064744, -2.456590549258068 56.76420552284446, -2.457012189013138 56.761212413604696, -2.458918366796638 56.76059451992412, -2.458141226934812 56.759905635089005, -2.458995226495517 56.75885140311115, -2.456251124776132 56.755106295820866, -2.45134006037245 56.753336421645834, -2.450371613598902 56.75166895944174, -2.451757625893824 56.751340530277794, -2.452580856181125 56.75044815787488, -2.451383448230958 56.75013807112391, -2.456177046836373 56.747650100349176, -2.459424766261643 56.74715305015126, -2.462933448611371 56.74793961570063, -2.465751549567782 56.75104646047506, -2.470270759934462 56.752835214184344, -2.475991232386105 56.75646986475055, -2.478288114832298 56.762183685678345, -2.483018926717363 56.76501327526758, -2.480534095643947 56.767700015774096, -2.480130568497123 56.76940847106369, -2.478695406151951 56.771049018641186, -2.474744976897085 56.773049515410975, -2.47153841562291 56.775684915736015, -2.463618112708479 56.7782839468531, -2.469260133800265 56.78323978648217, -2.464067579444115 56.78420250474722, -2.466891925622991 56.786267183329535, -2.465456884144986 56.786694780449366, -2.467512052740921 56.790064943718185, -2.471464418257373 56.78981426409948, -2.469899555850627 56.791060981580856, -2.461415210604984 56.79211795465471, -2.461404800700105 56.79260310873948, -2.456711245547472 56.79433626068074, -2.45665874848641 56.794057959388276, -2.450153432469192 56.7976481094068, -2.447732600923649 56.79234750135308, -2.445275726204271 56.79361400602348, -2.443900987598403 56.79226237896523, -2.445297996398051 56.791350051713174, -2.44361926279955 56.789199962997294, -2.433178814379036 56.79370152939932, -2.431886979822507 56.79525119270344, -2.431229778160112 56.79504684276858, -2.421653086119901 56.79800824856426, -2.408694880798742 56.79581446662895, -2.405060896981728 56.794344028616855, -2.405265760686484 56.79360670795319, -2.401529435503493 56.790187054227836, -2.40247659738079 56.78997737671073, -2.398266794206079 56.786444414867326, -2.402893612968765 56.784876941176094, -2.399353288711686 56.78187903552403, -2.396030630143245 56.78272355560085, -2.395040883956548 56.78185265103988, -2.382305420856361 56.78636828188928, -2.387148726884506 56.788869907418565, -2.385522354863637 56.7903840388414, -2.383530584428491 56.789310215916764, -2.381158649785416 56.79056910374427, -2.382901573653652 56.79152214280858, -2.380724158783445 56.79270175211255, -2.374034799279707 56.79256953153439, -2.37591830749535 56.79457867286726, -2.375329446923529 56.79518983176322, -2.378562729622931 56.79775979703802, -2.378794415894463 56.79869974115689, -2.380703200810821 56.80034656896172, -2.382022418657176 56.800704794533154, -2.378936216351782 56.803979068008076, -2.378859051606272 56.80545707570839, -2.379958879817004 56.80675750540324, -2.37810624580049 56.807309824912885, -2.378666072754012 56.80802806868606, -2.373733168858905 56.8097733293455, -2.372036537273736 56.808793709411475, -2.375440233184169 56.80548645203836, -2.365168595467273 56.80145645909217, -2.35843449504667 56.79756813763816, -2.357064953837452 56.798111177792705, -2.354721720184642 56.79716096813949, -2.360458920553842 56.79525979908565, -2.355778054947176 56.79272905025223, -2.35507942675697 56.79301404361931, -2.3533904710274 56.790553850388854, -2.353696995251956 56.78999053285231, -2.352807354485778 56.78938216895159, -2.350601242531592 56.78936555018066, -2.349790143768675 56.78880673460103))
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>