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/S02001242 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001242 geosparql: asWKT POLYGON ((-2.137923297456645 57.139474396128485, -2.1389148299118523 57.13979441653172, -2.1415497723882715 57.139232874092016, -2.1407580948587954 57.138175511335845, -2.1419935167238795 57.13783961539704, -2.1408139231933156 57.13690709807438, -2.139201676335036 57.13614953775424, -2.138549480713613 57.136649115968254, -2.1337968541996513 57.133627001206385, -2.133123077541188 57.1329844978857, -2.136541104663737 57.13194801895926, -2.138388631658372 57.131143742048046, -2.139314135538532 57.13098603347801, -2.1400263277365217 57.132385039573265, -2.1406667995507624 57.13297269292105, -2.143106973593784 57.1324448062895, -2.1426287276632694 57.13142429443319, -2.143872783180041 57.131255373393515, -2.1451270679725556 57.132940653908065, -2.1457051933813562 57.13292201648043, -2.1486421257573363 57.132020260582195, -2.148276323226886 57.131427813373634, -2.149652198664798 57.1313665382609, -2.1511326069998833 57.13104711092891, -2.152171645548651 57.130641624599086, -2.151109627231676 57.12984839610363, -2.1511829308206134 57.129175243830645, -2.1537284877094915 57.128480234446165, -2.1537747528646896 57.128141660409, -2.1551329989145067 57.12776340672643, -2.155530611293625 57.12805037114193, -2.15645510656258 57.12783010497592, -2.158880022096494 57.127183793637386, -2.1654281071522896 57.132071131185114, -2.166586870570168 57.13302174428951, -2.1660591196147854 57.13322007207391, -2.1624286202397744 57.13411415411294, -2.162363011265098 57.13422203568977, -2.1591121883070503 57.13508858858225, -2.1565694227642687 57.13543313962109, -2.15583623123651 57.13542859235568, -2.152887615210342 57.136012602701804, -2.1528895069702165 57.13647073532168, -2.1534868488292886 57.13708983425826, -2.152926408595331 57.13740492609909, -2.1540534466707317 57.13825693328923, -2.1546309750600936 57.13807656089697, -2.157185694275952 57.14024000787453, -2.157019577230515 57.14029239866542, -2.1527734960998006 57.13917023415099, -2.1519629376282987 57.140163890262194, -2.150904397145613 57.13990466218789, -2.1506229898792752 57.13920546522601, -2.1492977908522333 57.13894540314688, -2.1476312387988528 57.13906656561162, -2.1466613460523285 57.139439225415764, -2.1468776797126767 57.13993474541955, -2.1451551713297863 57.14010908416087, -2.142550116589484 57.14064181472927, -2.1432604403664333 57.14151262751488, -2.1421235106316057 57.141720649333124, -2.141462841536826 57.141367439082984, -2.142627543862877 57.14110887105334, -2.1423941440058445 57.14075993036591, -2.140444755890287 57.141177587808855, -2.1398218455537816 57.14107633644495, -2.1394562297116435 57.14051979632309, -2.137161325989961 57.14101639752259, -2.136725202102985 57.14038095686181, -2.138547260580155 57.14000233173528, -2.137923297456645 57.139474396128485))
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>