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/S02000128 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000128 geosparql: asWKT POLYGON ((-2.597177559899015 56.78485110829191, -2.592266340312601 56.78806060703061, -2.590609173658852 56.78840182191601, -2.588793269915762 56.7880869781102, -2.581248890715636 56.78606509522441, -2.574471292250089 56.78597073592962, -2.57234202890918 56.78587271974186, -2.570239644633728 56.78538824573287, -2.563935664767383 56.78630630123878, -2.56058656717453 56.78672566466736, -2.556168619635098 56.78681731059538, -2.552533868817676 56.785629655538855, -2.549785530582361 56.78348571448411, -2.542377661204297 56.78164055671786, -2.537729970204541 56.78279263246644, -2.533306471496369 56.784770075856905, -2.531565599831699 56.78550519176619, -2.528675912627883 56.78486165775538, -2.523638878644918 56.78279870002583, -2.518808553174011 56.7802585434488, -2.514774755978179 56.774894031355, -2.50864394543454 56.774065736748746, -2.505624631345527 56.77342220926947, -2.502453926214697 56.77001224432507, -2.492376544747674 56.766315236052236, -2.483018928318482 56.76501327256627, -2.4782881197506 56.76218368655785, -2.476541525423768 56.758596898975895, -2.475991229047146 56.756469859373084, -2.470270761614602 56.75283521777147, -2.465751546230768 56.75104645509729, -2.464120846167577 56.7487616991475, -2.460914676875401 56.74731823907375, -2.458592016234456 56.747245955209195, -2.456177043599049 56.74765010305614, -2.451383440012313 56.7501380675602, -2.452580849629838 56.7504481570003, -2.45175763083167 56.75134053295502, -2.450371615234071 56.75166895943585, -2.450629907143238 56.75068235754005, -2.445984979529553 56.751316403585314, -2.433142256821256 56.75077775559636, -2.432585055763385 56.750671893885176, -2.433055325727147 56.75032887435925, -2.430120729414851 56.751093664651016, -2.430782411303097 56.75175617534715, -2.429755435754668 56.7520382105188, -2.428197186077343 56.75162134557279, -2.429464471099554 56.75233567946117, -2.426827263495066 56.753386843221186, -2.425321113529887 56.75468564163432, -2.424503257904127 56.75467046350263, -2.433075241871333 56.74353713825912, -2.440545638539776 56.730879858478666, -2.445822599293143 56.71829285093225, -2.45273465366435 56.71838470713772, -2.454204321197551 56.71561236592984, -2.455659859464761 56.715741811574695, -2.458488650517936 56.71863317502924, -2.459999482854157 56.71926545181148, -2.459730397641047 56.71997616619272, -2.461599274897145 56.72181990677223, -2.460562713919092 56.722569387337465, -2.462723456046887 56.72288069837672, -2.463378703014108 56.72434660817761, -2.462762077443295 56.7260289584935, -2.45952333724177 56.72844858830243, -2.46077025252709 56.72884824817666, -2.459942632669228 56.72932744564324, -2.463524528870297 56.72955673001013, -2.463777481399922 56.72886404412565, -2.465454912230908 56.72839063733623, -2.466379155788291 56.72912384527696, -2.464845455489534 56.72932721631322, -2.466168164654596 56.73056203182422, -2.469196011773342 56.729598388029295, -2.470620628082785 56.73112924039415, -2.472576572611245 56.730759980396385, -2.473138058371567 56.72987096000609, -2.472490227348088 56.72903793382023, -2.474027380267413 56.729112947693174, -2.47646360582692 56.725330486869794, -2.474855293988341 56.722192315144824, -2.476234476335168 56.72134514398425, -2.477605397687609 56.72179885203746, -2.47885714821406 56.72105199899762, -2.482574466373004 56.72066336013753, -2.491798195116139 56.72209065014582, -2.495713116952238 56.72159000568781, -2.498571130664334 56.7227195425517, -2.500316898415194 56.72252388314387, -2.508270485170462 56.72344397063642, -2.509278771100661 56.723089486286916, -2.512116087118875 56.72385944307024, -2.512678908229954 56.72319232612754, -2.515917526774016 56.72343050501231, -2.515813433259859 56.72179588491084, -2.517490632374277 56.721375683235856, -2.5186223155098 56.72168540299438, -2.519188578841004 56.72009292215609, -2.518398328532919 56.71847014814446, -2.520651307795714 56.717167088573625, -2.521834442399856 56.7153024911084, -2.523522633805586 56.714522805102334, -2.526032099129359 56.71055937121355, -2.5289880690964 56.70935200756521, -2.532609668782525 56.7113399563908, -2.535760873470753 56.712386535886, -2.539702632159352 56.71272886744544, -2.541737557236532 56.71337585146656, -2.543641197813559 56.71611032433922, -2.549077504873424 56.71708996196779, -2.552418043762037 56.71650023065015, -2.551055300030082 56.716039102362615, -2.545513870902076 56.715820929938154, -2.543675948689589 56.71525400186618, -2.542420896286512 56.71318421631215, -2.540952265839949 56.712157472319994, -2.534724802651591 56.7107469533257, -2.53065421683666 56.705877175539804, -2.533938786959324 56.70599789110978, -2.535093734831258 56.70681944723363, -2.536529942227081 56.70674139410554, -2.538547892123196 56.70737053541652, -2.539053668624574 56.708473354345735, -2.540067460272554 56.70854981618994, -2.542024314549207 56.71059860865871, -2.542765647467919 56.712167550928676, -2.551684323937151 56.71213734965645, -2.55165630788101 56.71022392147493, -2.558367232059596 56.710050279248556, -2.564940385745218 56.71049678536954, -2.564171439081408 56.71150645740821, -2.568160772604708 56.71499201785567, -2.582446920225215 56.7302973338844, -2.592156254151611 56.74285583180895, -2.596188256726863 56.750724370465974, -2.601007331172919 56.75042273463409, -2.602507781408458 56.75426054051723, -2.601383863570141 56.75965623466569, -2.602258045231599 56.76113433766935, -2.595664673457379 56.76206438920753, -2.595935477925206 56.762638055579984, -2.593004894884871 56.76247235743146, -2.595202708621858 56.76594757836347, -2.598625018634178 56.76615578695838, -2.591365672956807 56.76849027112537, -2.581949323660806 56.768938844157496, -2.585020157670802 56.77283240250327, -2.591909614058252 56.77186558268981, -2.595666908698753 56.77459671391664, -2.593446115131349 56.77591892732282, -2.597344978331289 56.779287179999834, -2.600194226373211 56.778375126772424, -2.601331081015816 56.78090305840525, -2.597177559899015 56.78485110829191))
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>