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/S02002186 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002186 geosparql: asWKT POLYGON ((-3.9987709253605854 55.8690679004587, -3.998592312620127 55.868702339322155, -3.9967404639649318 55.86905593459618, -3.9949842518955685 55.869093410042886, -3.9939474570884608 55.868822636451256, -3.9941114741640655 55.86828076520269, -3.9926597632248266 55.86864578678409, -3.9917652200515064 55.86803118963502, -3.9919567285851323 55.8677135484149, -3.9906957369183598 55.866808309019135, -3.9897624742431863 55.86671698313877, -3.989214995056863 55.86632820266945, -3.9882673528766586 55.866376125023855, -3.988255055652636 55.86687913042632, -3.9875036646434836 55.867505559479994, -3.986222685716896 55.8671591949616, -3.984107428376394 55.8676399898845, -3.9845099093154537 55.86801711205327, -3.9856036163511117 55.8677034387984, -3.9860513829198654 55.86821156371492, -3.984078755943357 55.868823860567026, -3.983136303529758 55.86899431179048, -3.9826508222256005 55.868573822076804, -3.9831453488353885 55.867833692443014, -3.9823494940006157 55.867029396354624, -3.980910426788659 55.86703814150659, -3.980130656774554 55.866790074787275, -3.9801876651418246 55.86637594149877, -3.9808989275322637 55.86618457491935, -3.981233125532361 55.864501607059545, -3.9823529415105647 55.86436084419423, -3.9826861700748406 55.865017653778686, -3.983552146057234 55.8644191803348, -3.9835411717378912 55.8638421935908, -3.98241411003767 55.86371555416302, -3.9811631201827273 55.86304116465612, -3.984798625316997 55.86242812172122, -3.985413802836894 55.861330783513225, -3.9874269859164935 55.86094652204344, -3.988678462188743 55.8609561347379, -3.989308839362962 55.86043811217805, -3.9877031243512766 55.85859775148895, -3.9879182665204382 55.85776769735139, -3.9875417482424758 55.8573152342974, -3.9881953885997516 55.85686246318538, -3.989440484818863 55.856635437438804, -3.989389663034185 55.856146347971965, -3.991317556466884 55.85620455681381, -3.9923468226693695 55.85641233894941, -3.9955521571935266 55.85741921179744, -3.997506629698995 55.85771081324504, -3.9993219106147473 55.85770849154105, -3.999012496052659 55.858582068546646, -4.00136628679342 55.858784503281846, -4.001230677834031 55.85940629399852, -4.002302186571658 55.85954166307844, -4.002066863899646 55.860241430456426, -4.002986074019548 55.86039091924897, -4.003020487100807 55.86193420258982, -4.003985622131565 55.86180231118384, -4.004844649862305 55.86192826842578, -4.004816135240255 55.86076449618439, -4.005641080136814 55.86097179551336, -4.0056578646162455 55.86146513729527, -4.00884297755708 55.86200191143589, -4.0093963116984925 55.862505129558734, -4.0089139109701435 55.86337575222999, -4.009381098379367 55.86373604719676, -4.008829808447745 55.86484199348017, -4.0077067696576965 55.864947336687585, -4.0049741413079385 55.86414100095284, -4.004289485838229 55.86418079804348, -4.00380637297483 55.86476062147822, -4.0064314550355835 55.86597210580835, -4.007335376016996 55.8662323966308, -4.0055897744785725 55.867483084436614, -4.0041265548468585 55.86748810919436, -4.004368084698279 55.86852544249098, -3.999173581180463 55.869438808092475, -3.9987709253605854 55.8690679004587))
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>