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/S02000014 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000014 geosparql: asWKT MULTIPOLYGON (((-2.178872894864382 57.149120369187585, -2.172779331347764 57.14534586903761, -2.173164602089227 57.144214670177064, -2.170569123023009 57.1439846774043, -2.170472859886913 57.14461361689722, -2.167357586039885 57.146378488791235, -2.167558259732333 57.14689923962975, -2.165512835215984 57.147755341419256, -2.162998587289361 57.1472555930706, -2.16259300803675 57.14521697071232, -2.160608087321703 57.14478834599884, -2.159843522913324 57.14376525968789, -2.149224034308453 57.14524259781211, -2.145375705059146 57.14577712013974, -2.145146412218972 57.145277643421494, -2.148147561006776 57.14486795586322, -2.148906107977673 57.14426382017504, -2.147101860628883 57.143502402174036, -2.145598726590833 57.143656864631794, -2.144851624657437 57.142768408603004, -2.145892378497884 57.142695340860925, -2.145674246729215 57.141851189241066, -2.143260427126596 57.141512622140176, -2.141807519052345 57.141819696877356, -2.141491809635985 57.14136191682911, -2.142829087409758 57.141063964264426, -2.142563396477416 57.14072290648959, -2.14391714124015 57.14008159389853, -2.146773889111892 57.13997244761069, -2.147811117310267 57.13903699689254, -2.149297779275561 57.13894540046575, -2.151962932678736 57.1401638920648, -2.152773501064019 57.13917023594155, -2.157483064592495 57.140489447095476, -2.154630968439966 57.138076558210244, -2.154053435120096 57.13825693689664, -2.152545805083113 57.13726166077109, -2.153486838897633 57.13708982977893, -2.15288761025396 57.13601260270786, -2.159112181675553 57.13508858320083, -2.166586867274258 57.13302174609049, -2.158880012201519 57.127183797243156, -2.156455118113531 57.12783010226657, -2.154882522401233 57.12711693620784, -2.156186185919662 57.12682785938035, -2.154825042760555 57.125221580477266, -2.1615616024341 57.123396198806056, -2.163738351673555 57.12593547416586, -2.17060329799919 57.1240971222643, -2.172752293888544 57.12547226289359, -2.173706786492697 57.12517399158219, -2.174225353642383 57.12576750412364, -2.178282740315693 57.12609767456292, -2.180411983930951 57.12919377894214, -2.185218377362452 57.12910591762562, -2.182414072836531 57.1265229127799, -2.183306339805928 57.12659347913061, -2.182270703583711 57.12424142360273, -2.176637848511714 57.12406085715988, -2.176432356163684 57.122525036212295, -2.177970847950924 57.12250417649578, -2.177878934113276 57.122109693709056, -2.180703785880646 57.121288266493444, -2.18330042203681 57.12205704301302, -2.18647076143059 57.1219445637092, -2.189627454183993 57.122397963412794, -2.191630201045913 57.123248302959205, -2.192057282043138 57.12279849652658, -2.198389655230255 57.12375977573757, -2.202748841926899 57.12390452817423, -2.206990988684979 57.12642183079815, -2.209930593293957 57.126345079344894, -2.212501525964695 57.12822718722028, -2.214265647100277 57.12765824428883, -2.220013696357332 57.12729492521713, -2.220959777856493 57.128410179447734, -2.214853295895201 57.1300479134769, -2.211586749059462 57.13001636533809, -2.211506717052888 57.13046565159843, -2.209526086909234 57.13075643740679, -2.211834428424908 57.13289052499363, -2.211393595892248 57.13457917539206, -2.212000714095713 57.13549140888539, -2.213253432821181 57.135271192561824, -2.214618440713795 57.13712262765355, -2.212356130395012 57.137444047229394, -2.212368130485677 57.1395370759009, -2.210981195160253 57.139710099326045, -2.211490751361638 57.14138548976144, -2.219647479725054 57.140831747019746, -2.220537544188276 57.141695765735754, -2.221623589458496 57.141501497641045, -2.218478776620694 57.14410756557464, -2.21189875938904 57.144137187945375, -2.212202964269345 57.14531345383963, -2.208882334462936 57.14548970413117, -2.209553917098628 57.147357054139874, -2.20608090464827 57.14691366358021, -2.20082951210955 57.14764083196171, -2.199390795229338 57.14747245810323, -2.197186858986178 57.14636204695069, -2.191277951502332 57.147790525656426, -2.184485216543397 57.14772884200419, -2.178872894864382 57.149120369187585)), ((-2.167558259732333 57.14689923962975, -2.171127952102465 57.14498675823811, -2.172279559869889 57.144994525863446, -2.171975140586484 57.14605972127462, -2.170377345687819 57.14621135407024, -2.169551560767916 57.14842419930064, -2.167728771804815 57.14822090749019, -2.167558259732333 57.14689923962975)))
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>