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/S01005700 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01005700 geosparql: asWKT POLYGON ((-4.041978846028577 55.67500077608868, -4.043034505526957 55.67389574847549, -4.042482015439116 55.672754580362664, -4.043001155461058 55.672035945907005, -4.046079803301238 55.671607181999846, -4.046959432067414 55.66987594625157, -4.046301300596759 55.66884440357473, -4.043890492883783 55.6680487739907, -4.043472680083184 55.667354728466854, -4.042278811784564 55.667329670806865, -4.042815201930035 55.66694327684404, -4.041404444722827 55.664890705167494, -4.041963695131546 55.66433317654203, -4.042605211007357 55.6644393318076, -4.042546279090916 55.66361348626417, -4.048195077404181 55.66400462282565, -4.048676198961208 55.66347532865494, -4.049726201317775 55.664095886008816, -4.050978260558679 55.663715470696, -4.051497874555808 55.663922477329045, -4.053699304879182 55.6622409754806, -4.05700153438677 55.66124198736586, -4.060064084071992 55.66234097133492, -4.066097734444837 55.6618890017754, -4.069414072593308 55.660862476786306, -4.077922953843319 55.6607424827113, -4.079473469541627 55.66103745694255, -4.080875683751191 55.66268521313237, -4.082122383898035 55.66217652959359, -4.083273881633952 55.662969533004784, -4.082604356984634 55.66351486285671, -4.083785859622543 55.664098408643085, -4.084585932247812 55.66369014975406, -4.086871873177307 55.66495136967524, -4.086600555088101 55.66525779000611, -4.085770846712884 55.665109387839564, -4.082700947847779 55.66687950397814, -4.07999015367246 55.66458070433197, -4.078073724251189 55.665634671708645, -4.074960659056337 55.66594420253942, -4.074219040687497 55.6666410573168, -4.074949845575757 55.668731730040456, -4.075730700085037 55.668763458412954, -4.075646295140793 55.670472474750845, -4.073371249300939 55.67075359797042, -4.06721511883973 55.67194717048548, -4.066698385248289 55.6711373600164, -4.061717074176301 55.67164638784153, -4.061062637775328 55.671185981294904, -4.058873485189396 55.67162260124997, -4.059507105213645 55.6723172129801, -4.060368444055615 55.67230258517555, -4.059886556451302 55.673182608554164, -4.061628229819208 55.67303652250236, -4.06254892356533 55.67391078964024, -4.06408929598612 55.673165246940705, -4.063299039859487 55.67396108920678, -4.063654472978978 55.67436852660173, -4.062729453064315 55.6749233169184, -4.059706591153564 55.675809911982334, -4.05681797825079 55.67410584979835, -4.055864855635941 55.67473295671165, -4.05614124002963 55.67515072687344, -4.054884510551865 55.675144817602586, -4.054669344947602 55.67468108334543, -4.052541977508845 55.6747526367631, -4.052389293575761 55.6751775915881, -4.051472367253182 55.67528279748115, -4.049675286481294 55.67498030127127, -4.04805939425376 55.67601385410546, -4.044548523243749 55.67609037764994, -4.041978846028577 55.67500077608868))
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>