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/S01000321 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01000321 geosparql: asWKT POLYGON ((-2.094170440682281 57.07095221532943, -2.093424845352872 57.06958733994513, -2.09467535243052 57.06846350896278, -2.092249771557392 57.06791735217427, -2.093255701355156 57.067907623341185, -2.095280911478378 57.06666642300302, -2.096253291121835 57.06645008276793, -2.096518726745761 57.067060730908615, -2.097342479089127 57.066763648757714, -2.097637228859681 57.06597290333782, -2.09925326265588 57.065971630467985, -2.099251243519287 57.06521704897572, -2.100570039576083 57.06507226466494, -2.102385129349569 57.065510964577854, -2.101657085067744 57.064604262354635, -2.103536891957451 57.06460271941164, -2.103981203310723 57.06427895776663, -2.103237931824786 57.063830416215474, -2.105875384182173 57.063540749786476, -2.103696716920338 57.06278799269055, -2.105658848367642 57.06277736730913, -2.105278820500591 57.06249920980726, -2.106168972656373 57.0624176095963, -2.105096426255828 57.06214003779262, -2.107090881034599 57.06191376747325, -2.106858907403129 57.06151870789268, -2.107517705706286 57.06126661509161, -2.10703809730404 57.060763970873005, -2.105025234228591 57.06028957187871, -2.106805485002271 57.06014433315469, -2.10489117464606 57.05952611836666, -2.105368821568396 57.059355036037545, -2.104823778954073 57.05901413402295, -2.105515697799588 57.0588249067471, -2.105036864880258 57.05857378205147, -2.106025281652943 57.058294469200504, -2.105217227113566 57.05822328687302, -2.106915360733415 57.05822184588911, -2.105035135823406 57.05796292975846, -2.105049738943746 57.057298165710094, -2.10661606891963 57.057341754295, -2.106894627074988 57.05674862789524, -2.107093522684601 57.05711676840383, -2.107999038166374 57.05669378055851, -2.108825449050097 57.05741171544434, -2.109417236422998 57.056827295276584, -2.110094204021468 57.0571770417645, -2.111115865909305 57.057014442065814, -2.111161371062242 57.055693879025036, -2.110550521087025 57.055397976863894, -2.111369509725935 57.05363655257363, -2.115899573371799 57.05259937209074, -2.117181261245288 57.05130460217742, -2.116026758595032 57.051099066501635, -2.11633910169038 57.0508292824463, -2.120324073918249 57.04958583385414, -2.120750147488463 57.04882185457515, -2.123417780154847 57.04805568181237, -2.121369142393987 57.04657546679274, -2.121891922915904 57.045164599702815, -2.120043736342347 57.04444773501296, -2.120966035930541 57.04425819943255, -2.120765611536291 57.043440925105074, -2.123185902696531 57.04274685956307, -2.123566881810556 57.04333038945091, -2.124918158585177 57.04330209875961, -2.124650768989218 57.042197435073824, -2.125490927177653 57.042106761909835, -2.126164085952847 57.04136946531686, -2.128225141531709 57.04169974468266, -2.127463204352753 57.04056864612613, -2.127922911825053 57.04007410118683, -2.136807052652806 57.0442553361559, -2.138159509136611 57.0440694506763, -2.14166775152803 57.044811378731495, -2.134976826237533 57.05314706608761, -2.128652963721275 57.06054596974445, -2.125631503495816 57.06417823036325, -2.123622209617229 57.066491254937645, -2.125088528316462 57.06682384903018, -2.124877639891606 57.06738877637262, -2.123622682946567 57.06774118734963, -2.124424284833023 57.06826743540911, -2.122395396891245 57.06790943858661, -2.122565408786095 57.069454370525555, -2.121294317011817 57.06907831930102, -2.119618525760663 57.0710472394685, -2.117958245655769 57.072764591714524, -2.116405353294019 57.07195756657935, -2.114441795161418 57.071662936814455, -2.114654310299944 57.071051889593974, -2.1117505648763 57.07069519342957, -2.111076590168655 57.07143240938861, -2.109349759051567 57.07068449994988, -2.108579244292424 57.07157349655591, -2.103655434762355 57.07161844488551, -2.103833178891668 57.07030676204998, -2.100586896763241 57.07128857602871, -2.099714943232733 57.072088772611146, -2.096299402470041 57.0715075476825, -2.096149687826864 57.071013591145494, -2.094170440682281 57.07095221532943))
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>