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/S02002168 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002168 geosparql: asWKT POLYGON ((-4.060727843014946 55.84625221451648, -4.061058863511115 55.84558374714194, -4.0610231398337575 55.844890221834284, -4.060003544816831 55.84403555929569, -4.0590294092727035 55.84373732465351, -4.057524050961109 55.84366365957581, -4.057120945490603 55.84298738698461, -4.058906681216823 55.84262498500897, -4.0593481009463765 55.842213177261286, -4.0591338111135284 55.841785386697815, -4.057312706482951 55.84177991504623, -4.056644676445049 55.841530463524535, -4.05746956703962 55.84112123641539, -4.057288944871049 55.84042326806619, -4.05578468162109 55.84036753862, -4.054896126400669 55.84017568617287, -4.053796676233243 55.83961888062512, -4.054809760118242 55.837328235218536, -4.05511199131248 55.83639751884674, -4.055983870321135 55.83627510102453, -4.056605481174211 55.83504246772311, -4.0605853322472525 55.83563705099414, -4.060120367224031 55.83655975983094, -4.061310471224355 55.83675484209034, -4.06283976020045 55.8336528988775, -4.063937150932017 55.83393681971673, -4.064821668500019 55.833891370212626, -4.065253319904102 55.8343171639131, -4.0675486198791075 55.83487653217681, -4.0689232551252354 55.83367583574066, -4.0668984533631924 55.83313400161173, -4.0676125390686515 55.832185579341655, -4.0672430238679365 55.83130226834618, -4.06924832820982 55.83182987411898, -4.069770126984169 55.83100397755773, -4.070647138122977 55.83007129835127, -4.071953908431093 55.8304364506354, -4.07267830284666 55.82957604064876, -4.079735341588035 55.831570897492796, -4.080222838511491 55.83113127038382, -4.0818107404888995 55.83156274078418, -4.082180780826908 55.83101724876765, -4.0823740006670945 55.83015121381025, -4.083214704486046 55.83004710288331, -4.083517149911104 55.82938061660406, -4.085480372329181 55.82882239826054, -4.085722807196366 55.82798248366759, -4.084470298517908 55.827335529997335, -4.08474722579599 55.82700686099727, -4.089431830985669 55.82792310917424, -4.091466206067813 55.82840348632009, -4.093759387856948 55.82909981984756, -4.0957306236708755 55.82985240303755, -4.097950093547579 55.830919975288204, -4.101076195956612 55.83260022280316, -4.102974716658007 55.833396385600366, -4.105273041184902 55.834172195026994, -4.10704468350412 55.83465681638394, -4.106684833194466 55.836034422325035, -4.1076629428343825 55.83696311189438, -4.107994464375364 55.83760001163204, -4.106251882516921 55.8381574153022, -4.105567849862215 55.838148458288934, -4.104278491549619 55.83781373709149, -4.103246714921771 55.837753199183474, -4.102711016254123 55.83797715267798, -4.102721269216127 55.83899522127578, -4.104587541634801 55.83901092956495, -4.105778160049569 55.839143330199796, -4.105398822713524 55.83977802690723, -4.104674836907772 55.84009418477816, -4.103824022215647 55.8407827784347, -4.103202263273684 55.841927590764065, -4.102657249775941 55.842335040378984, -4.10113342069112 55.84271877143524, -4.0995676047702325 55.842650248430154, -4.097172031264597 55.84235949575279, -4.095737744228611 55.84298338627635, -4.094697505789404 55.84324286138605, -4.0945319942339 55.84372918750989, -4.093789202626995 55.84375801196172, -4.092144260397429 55.843138932054835, -4.0915606173812655 55.843216262347845, -4.089886905294356 55.84423239817786, -4.0876572031060725 55.84483106121421, -4.086112083387222 55.84488154351283, -4.084631278446043 55.84523827515638, -4.084262393020052 55.84461363244819, -4.082908377410765 55.844444246777876, -4.082136942841991 55.84393785470052, -4.08144580342266 55.84391719969889, -4.080071856291651 55.84462975425715, -4.078534927304547 55.845043081196806, -4.077425097892784 55.844570238327606, -4.076862196081467 55.84389942245888, -4.074716230762384 55.84412436999009, -4.075783304041729 55.845683598157464, -4.0766832838893485 55.846474547150365, -4.078202860732868 55.84741049892788, -4.079380376443797 55.84792263302118, -4.081522895232776 55.84855593140417, -4.083105963314708 55.8488535647342, -4.082669383258295 55.849352551825376, -4.083606576792838 55.850894142601646, -4.084202053747324 55.851274093863545, -4.087243592442039 55.85263888605021, -4.088250318106069 55.8535061295838, -4.088435915900483 55.85419588388617, -4.087778436119298 55.855761367555075, -4.084153107568574 55.85623758504979, -4.083836149534527 55.855092616714856, -4.082314474382786 55.854722953460445, -4.083794913922117 55.852531994986215, -4.082625810911012 55.85211066366038, -4.083223109309418 55.85167554069062, -4.081545232927276 55.8510836125338, -4.077811929447412 55.850424729609166, -4.077585769179744 55.850878631759294, -4.076546721728352 55.850501438102555, -4.074028839839649 55.85125013287233, -4.073204808514763 55.851043679598085, -4.071213742343733 55.85002551128146, -4.0721307684125065 55.84970050649493, -4.072126019250896 55.849356498567985, -4.070052307039189 55.849441500858006, -4.06954885543591 55.84901827909339, -4.06859784885555 55.84886115892474, -4.066334292484679 55.849555261675235, -4.06612894809503 55.84990021931778, -4.064168487292184 55.84923131781806, -4.059195408722393 55.84808427633543, -4.059554899899664 55.84793446461563, -4.060727843014946 55.84625221451648))
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>