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/S02001396 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001396 geosparql: asWKT POLYGON ((-3.7861868292240914 56.155152970190606, -3.784965058754706 56.154876000512125, -3.7860383439510708 56.15431162695561, -3.7851622569122174 56.153821067065486, -3.784707810612611 56.152353911329264, -3.782284291148612 56.143825136764086, -3.780591179524141 56.14375969271082, -3.780208516752704 56.14343437539428, -3.7789195975695913 56.1433299241472, -3.7802652097112532 56.14252554773903, -3.7807163138887687 56.14298508328453, -3.7818624808937313 56.14356375037027, -3.7836837412415214 56.143804937188435, -3.7848255411405916 56.144129917268074, -3.786874488335539 56.14388462852314, -3.7873112103759756 56.14462415856348, -3.787444014353798 56.145404029587176, -3.7881446558411827 56.14593305685013, -3.789488211094244 56.14990343033475, -3.794459391328439 56.14936246069261, -3.795955599951818 56.14912008161082, -3.7975987361220414 56.149089703938536, -3.798775745337723 56.14900157195196, -3.798847219879581 56.149494121774495, -3.8002684615472573 56.14943229798899, -3.8027670898422197 56.149503685554606, -3.802817246280629 56.150230840280244, -3.804649166277366 56.15015916232402, -3.8055110921940694 56.15034426350526, -3.8072839638082123 56.15037375781643, -3.811679712946098 56.15032632038601, -3.8175215447650674 56.15035685572829, -3.817695777744845 56.15302318466532, -3.8184756863594593 56.153173458349904, -3.8187126713106947 56.15591240452056, -3.8142683184109805 56.15597346235035, -3.8138790417093764 56.1558072460256, -3.811223215208468 56.15579709102138, -3.8097637120915913 56.15595765637751, -3.8086902858933662 56.156227683513386, -3.8075324741157566 56.156020917824335, -3.807082010339222 56.155686024904746, -3.804987755700156 56.15566272215383, -3.805009804399206 56.15542942534073, -3.8029379427833927 56.15544178622186, -3.8003331532958406 56.15574745859772, -3.8004456495282626 56.156098417095926, -3.7995963455929402 56.15636552071984, -3.7978628324438177 56.156521443878475, -3.7969124903366476 56.1568587698606, -3.795934196814564 56.15689356353294, -3.7945319775204713 56.15659106706923, -3.7937374585754338 56.15764836417933, -3.7921236662734676 56.15765701870449, -3.7926808956283735 56.156743233191634, -3.7922469269134442 56.155758342644134, -3.790973514660233 56.155875656040635, -3.7861868292240914 56.155152970190606))
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>