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/S01002428 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01002428 geosparql: asWKT POLYGON ((-3.922105298181644 56.000056733089025, -3.920921218728791 55.999140587512755, -3.91811523597364 55.99850130365831, -3.919340605816054 55.99798796860253, -3.917637684718242 55.99695405822346, -3.920447785465895 55.99615768772026, -3.922596261382562 55.9954290959873, -3.9236222991165 55.99602986623115, -3.927751174918881 55.993956542717115, -3.929142647119347 55.99485140444163, -3.928472009233163 55.99352459541005, -3.934017311982644 55.99228572501261, -3.93194043584226 55.99171110150435, -3.936209140720597 55.991250361901784, -3.936634276183331 55.991852280949104, -3.941019674967639 55.991879091260294, -3.942124794193152 55.99281426429249, -3.941963165057205 55.99342790102396, -3.943878278288426 55.99115807968497, -3.945106806392357 55.991212554275435, -3.944514425114036 55.993145039230434, -3.948044330465675 55.9935116712736, -3.952558644511153 55.99273029565787, -3.950851490793124 55.9914029151344, -3.952049749574496 55.99119761673826, -3.951833224361528 55.990806631186985, -3.952496548592678 55.99086298992399, -3.952576393136543 55.98962665649974, -3.955053992535303 55.988886451899276, -3.955506668280166 55.98993612735646, -3.957454644728917 55.98840195046627, -3.959777436527667 55.988365036903204, -3.95434370374361 55.985520312654366, -3.955252899742205 55.9851208249179, -3.958567530920847 55.98663187160094, -3.963265501214019 55.98664698899911, -3.965052593066684 55.98711277458453, -3.985482326171678 55.98284017012161, -3.98524833366859 55.985468061190865, -3.987983109707482 55.985028561133916, -3.989478704135522 55.987305027563515, -3.979014895638038 55.99085249562959, -3.974903680603808 55.991035267510654, -3.975679142208413 55.99272132339406, -3.978097375142443 55.99266456332741, -3.982066089512006 55.99533276125001, -3.980949446781821 55.997013250743684, -3.981097231889561 55.99896098744578, -3.979745394350604 55.999494947892096, -3.977686207983273 56.00030984435879, -3.976416642966656 56.00025831330326, -3.975186420800785 55.999406324588854, -3.972893668271155 55.99910154858296, -3.970724156585448 55.998066839732196, -3.9668578997561 55.998667792440365, -3.966242325814526 56.00012446536363, -3.964190867466447 55.99983365186066, -3.962788868443009 56.00033227662539, -3.96193787955855 55.99841369425627, -3.958248409294037 55.99839148299872, -3.957002399684972 55.99912121625762, -3.954139295770783 55.998950959435824, -3.943293099259609 56.00139604107513, -3.941575743492454 56.00136918283996, -3.941541130726595 56.00003971008032, -3.942570140891238 55.999133823693754, -3.941418935934319 55.99888236210767, -3.938874303222664 55.99898531981525, -3.937426177148697 55.999529312102574, -3.93703769983657 55.99881648927775, -3.933527264658613 55.99916816112061, -3.932480881543381 55.99812415213734, -3.927696616101668 55.99865738042322, -3.926675877426103 55.99812515764616, -3.922105298181644 56.000056733089025))
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>