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/S14000041 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S14000041 geosparql: asWKT POLYGON ((-3.3733756399558876 56.03187568437925, -3.3808865218209454 56.0383980399039, -3.382432402377227 56.04217264784043, -3.382522658878372 56.04649101180731, -3.380568177235138 56.04898932752843, -3.3690028802589143 56.04992600893479, -3.3710639625189764 56.05421080209424, -3.3750163797282915 56.055807582177, -3.3732351893048804 56.06045139011562, -3.367512082406918 56.0610172432148, -3.3663029235234334 56.0623847601286, -3.3605849208352474 56.06392696084109, -3.3616273718619096 56.0672994151141, -3.3348992595536275 56.072107546826054, -3.337232376663109 56.07546083277938, -3.3410531990857546 56.07925977375178, -3.333373405238418 56.082024244835196, -3.332070847964655 56.08185680261756, -3.323079069525104 56.0846823998435, -3.320829993614193 56.08850733256621, -3.325641748760957 56.08784833567385, -3.3404630096588415 56.09640867558131, -3.3479044499154917 56.098210108973674, -3.351223494666453 56.10131245327327, -3.35458911809018 56.10366570915846, -3.363606955705598 56.110928247420425, -3.3634551569266136 56.11172873242286, -3.3643814243411216 56.113136409261735, -3.373050478146388 56.112119307321244, -3.3903693283994256 56.10932061531082, -3.4097997582983672 56.11016058959248, -3.424703723722633 56.10776829220488, -3.4271664171782317 56.10657442282705, -3.432560305030339 56.11429885911339, -3.4400812406871704 56.119050112963414, -3.4397792392747806 56.122978532470995, -3.4362739744589343 56.12319366757554, -3.4370073532860084 56.128839750392764, -3.442541347980105 56.12981217411285, -3.445348079655674 56.140895399319504, -3.4488418102458844 56.141700912017065, -3.452027409418233 56.147092583730654, -3.445539229307026 56.147187540265264, -3.4344742144386147 56.14506473932659, -3.4166206315901255 56.13840762301806, -3.4011550006902556 56.141580675770705, -3.380966284874275 56.1430093674498, -3.36951995684297 56.14699693483118, -3.366587641600977 56.154989593567045, -3.3716865826364506 56.164435174473006, -3.358309239804307 56.1671240082657, -3.3393602130778266 56.17391620022124, -3.3242773637115453 56.17191056971996, -3.3134154912401192 56.167159128397245, -3.3067471050100714 56.144272288216236, -3.297178416643255 56.14283304342019, -3.2898244447009617 56.133161850928616, -3.2748281749463315 56.11935073188957, -3.262567499211055 56.120840660385376, -3.2523058661446473 56.11442629499756, -3.2416471512280003 56.119241326970624, -3.238848443278705 56.12514491655392, -3.2152042250561834 56.123315904627674, -3.2012436223221945 56.128542617793066, -3.2099621295882446 56.13823345837643, -3.197849951504593 56.14124923372452, -3.1778971724562215 56.14345646941269, -3.1412741621902733 56.14442289424018, -3.1279082947952435 56.15087745899913, -3.116550469078594 56.15266982066777, -3.1087936423433895 56.142080650900176, -3.1143110212913845 56.136813350118985, -3.102893617404849 56.13595261178203, -3.1150481358316124 56.128869237764455, -3.123852294472778 56.12139286903517, -3.1404963536467103 56.11962744837134, -3.1534750684760064 56.11334755721842, -3.1597655153028756 56.10108132490649, -3.158494204691941 56.085035749296246, -3.1642968035843206 56.0748529692997, -3.1739245922812627 56.06823485828077, -3.1776926825974194 56.062062897479244, -3.185090843927339 56.06402274688525, -3.2167521106693564 56.063856586214904, -3.225477650327564 56.06075116003299, -3.2244051029604646 56.05716227117743, -3.2352303376338067 56.05541004928177, -3.248181134442533 56.05789985630806, -3.250967903331464 56.06099136343076, -3.2842708953382806 56.05617718588343, -3.3105868281381836 56.043574171257866, -3.3186938938398285 56.034876958246905, -3.3295427809566465 56.039480594952295, -3.3433171220035565 56.03843550305868, -3.3415588748918816 56.03397624051357, -3.3570935004439746 56.02608319368239, -3.3694929433875185 56.02759948029015, -3.3733756399558876 56.03187568437925))
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>