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/S02001747 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001747 geosparql: asWKT POLYGON ((-3.4496989651749703 56.07368509538096, -3.452557115175279 56.072788865034, -3.4519506355888474 56.072041206643284, -3.452069147401838 56.07134790754629, -3.4532364692325834 56.070777055833894, -3.4535980087930445 56.069712498574866, -3.4534801929427537 56.0687164775468, -3.4506518359764096 56.0686868586939, -3.4490682810927833 56.06886720564561, -3.4489009717510624 56.06868945700199, -3.4452510273793773 56.06890850222068, -3.4453383700543303 56.06833395200264, -3.4461022880316814 56.06816517451859, -3.445664176544652 56.067694292745024, -3.446875854966732 56.06732895035852, -3.447541656798216 56.067995542596854, -3.4488518871511733 56.06780943875372, -3.450987168723938 56.0672509341861, -3.4510386572874547 56.066797541166345, -3.45390878522676 56.06644704059693, -3.455624403581245 56.066363915303505, -3.4566014773340954 56.06501352041374, -3.4569565635339052 56.06335596393551, -3.4573106915568856 56.06252509967333, -3.4584973417176257 56.06119916961796, -3.4597193118795935 56.06038499362001, -3.46190276033948 56.05957533977786, -3.461309695724339 56.05821487778555, -3.461526333083666 56.05763195296603, -3.4639856427709383 56.05787993380751, -3.463833190247237 56.059177102349466, -3.4673917902754643 56.05857771904375, -3.4698781991566525 56.0580808670784, -3.4704902532710395 56.05854981650616, -3.468948880129968 56.05940384704609, -3.4677746250869297 56.05977724922069, -3.466987347804964 56.05976864050568, -3.466615980610699 56.060621574549806, -3.466645303708533 56.061219960866865, -3.467880770678383 56.06299286591203, -3.4677335399491906 56.0633450593452, -3.4692433663156232 56.0654567007466, -3.469005611710389 56.065540402604015, -3.46817619728864 56.06569404478193, -3.4670157715376053 56.06559103201347, -3.465556792728759 56.0656622826252, -3.4620446433398193 56.066234100555505, -3.462978809423266 56.06714855414575, -3.462359426055877 56.06733560932402, -3.4623907197493624 56.068161912018056, -3.4636212252999132 56.06841690305277, -3.4636929034244788 56.069036064830975, -3.4645520303955637 56.06966385505533, -3.4642427778400884 56.07040435054149, -3.4657326572866545 56.070719133961795, -3.4655884471620846 56.07115215934462, -3.466508511847309 56.071267028744685, -3.4670870129363895 56.07208173161632, -3.4658446578796873 56.07206884554994, -3.4656168517163577 56.071520809956226, -3.4647489319023737 56.07188996650762, -3.464936776216991 56.072606591011414, -3.461520753086137 56.07283808402068, -3.4614291248726357 56.073665931690776, -3.4624147360470663 56.07388551150535, -3.462820505388057 56.074842160378466, -3.461125362910787 56.07505094919201, -3.4594371816751495 56.07512136086354, -3.459221384553418 56.07463111174875, -3.4564703085162316 56.074737561295166, -3.456166544964872 56.07553882979889, -3.455387133162913 56.07583760435378, -3.455962754979426 56.07615481086187, -3.4577922400661505 56.07613270239606, -3.4575698910625126 56.07669059747128, -3.4542684837850857 56.07681224717388, -3.4543678171776695 56.077902375402374, -3.4498345767645473 56.077991537278784, -3.45079785020209 56.07611774423005, -3.4505927887038585 56.07568508738012, -3.450752864236545 56.07479591759621, -3.451403379505814 56.074150287541016, -3.4496989651749703 56.07368509538096))
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>