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/S01000273 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01000273 geosparql: asWKT POLYGON ((-2.561360817431111 56.82023395741515, -2.56250538664003 56.81976798169135, -2.561690607396181 56.818963122293745, -2.553958298164668 56.817847783873844, -2.554573312418577 56.81734196718164, -2.553181671368404 56.81739306235319, -2.55156455550176 56.81436374817687, -2.552092763207208 56.81352593175382, -2.555125495154918 56.81148216611612, -2.560362417779295 56.809858318973156, -2.563038645153371 56.80855916635433, -2.563553586167752 56.80929971274495, -2.565701921551624 56.80862481057202, -2.56837710968684 56.81075389893601, -2.571025754759525 56.80984934242593, -2.568716000909671 56.8070788786233, -2.573636378531897 56.80525359053415, -2.571262565761125 56.79345991548341, -2.571928434111162 56.79095939413459, -2.57109336554537 56.79094525420521, -2.570781673617129 56.7898237163975, -2.568050793766731 56.79000687584865, -2.565622903704956 56.78965856248067, -2.563435116645228 56.78785375882421, -2.564657812964574 56.78698211364062, -2.563935672923667 56.78630629940516, -2.570239639723896 56.785388245755314, -2.57350731788686 56.78608297452089, -2.58124888588911 56.786065100637146, -2.590609171965633 56.78840181833055, -2.592266340284276 56.78806060523401, -2.597177553338426 56.784851107424956, -2.600331108452019 56.78356535367109, -2.604764707973771 56.782947403948775, -2.61506429221803 56.78331810908001, -2.616062692214943 56.78376935203584, -2.616172973697745 56.78464605605217, -2.615449460329434 56.78731646752942, -2.617180290557502 56.79332200700405, -2.626373492695096 56.798783205658545, -2.628148729142695 56.80311341809525, -2.631821421521202 56.8092307517801, -2.643813785713309 56.81232272897444, -2.651881996762493 56.8100528705106, -2.652975978258352 56.81080178292812, -2.652522631727634 56.81204804887429, -2.650509508539185 56.81402185699145, -2.649822098218818 56.816891245638956, -2.650671474412357 56.82272228062104, -2.654352601880559 56.82663922912374, -2.658007486952956 56.828742787090185, -2.658620363998834 56.83005117525566, -2.66580427718631 56.8341096271065, -2.666440605479249 56.83579516727935, -2.665753830188233 56.83773032811919, -2.670353651489017 56.84217032517293, -2.66980363933416 56.84256885050216, -2.670175699538573 56.84343055943425, -2.667476406946427 56.845186579083595, -2.671285281231251 56.84641488792835, -2.671830444651176 56.847561867552564, -2.670476899962404 56.848880763050914, -2.667757595642061 56.84992846189288, -2.669909287798706 56.8547321874917, -2.663166036872393 56.85544196344124, -2.651910596834222 56.85412674179216, -2.648193551070595 56.8563201400109, -2.652105540025122 56.8577910584139, -2.646831731652959 56.861025655690014, -2.640242498642122 56.85740327047678, -2.641597107194235 56.856066735793476, -2.640527449264785 56.854886385530854, -2.637745446889957 56.855215056316496, -2.637222343147123 56.854346313719844, -2.63468404454398 56.85550915104357, -2.63292751652759 56.853451826454695, -2.632289452393507 56.853535914306754, -2.63240909671092 56.85285255062934, -2.628684155985541 56.85170348851198, -2.627490947873262 56.84996666378431, -2.629516571916327 56.84952525012479, -2.631221893804031 56.84856437031128, -2.631223309314458 56.8476749799183, -2.628362913280532 56.846234059760626, -2.621971335713387 56.84437949977777, -2.624204277397995 56.843604743463715, -2.620213993513242 56.84120802985338, -2.618235656965839 56.841514304163134, -2.615497590405835 56.8404767485139, -2.613803755421499 56.839137538777194, -2.616706487436463 56.83848967184646, -2.619946758365773 56.836942115568924, -2.616354800152806 56.83519064756319, -2.615051409458141 56.833318260805534, -2.615495109192862 56.83302206482664, -2.614299495750587 56.83237938300009, -2.607860179037411 56.83273597367625, -2.594950605124195 56.82959835073392, -2.595003464093738 56.82879855364319, -2.589490638358142 56.82834858325386, -2.587582849921105 56.82999260451433, -2.584033532274401 56.82831135543051, -2.577737098584081 56.83016434628976, -2.579035763280832 56.82830769323576, -2.576275254438449 56.82780840418389, -2.576709489126451 56.82727636172459, -2.570923197281532 56.82605424291941, -2.572907260340465 56.824005860148866, -2.567028552976771 56.822047324634006, -2.56622233000414 56.82290443104457, -2.560626520526688 56.82120483690246, -2.561360817431111 56.82023395741515))
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>