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/S14000017 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S14000017 geosparql: asWKT POLYGON ((-3.5565425645071715 56.157342334359235, -3.5460883137310057 56.15855007606376, -3.5400111296627954 56.14646833794529, -3.5277759931779165 56.146870053758356, -3.511279962412961 56.14793197975276, -3.4988779624694195 56.149812801679566, -3.491640854596143 56.14978357813953, -3.481207865936446 56.14723139234158, -3.478967075675422 56.1469347565165, -3.4650282873884493 56.14716822862709, -3.460309985598825 56.14626712753443, -3.453705018316039 56.15038403217707, -3.4520274094182026 56.147092583730654, -3.4501209901300083 56.14395745180347, -3.4488418102458853 56.141700912017086, -3.4453480796556595 56.14089539931952, -3.442541347980091 56.129812174112864, -3.4370073532860084 56.128839750392764, -3.4362739744589343 56.12319366757554, -3.4397792392747806 56.122978532470995, -3.4400812406871566 56.11905011296344, -3.432560305030339 56.11429885911339, -3.4271664171782317 56.10657442282705, -3.424703723722633 56.10776829220488, -3.409799758298368 56.11016058959252, -3.3903693283994256 56.10932061531082, -3.373050478146373 56.11211930732126, -3.3643814243410914 56.113136409261735, -3.363455156926614 56.111728732422876, -3.363973186614684 56.11159811324364, -3.363606955705598 56.110928247420425, -3.35458911809018 56.10366570915846, -3.351223494666453 56.10131245327327, -3.3479044499154917 56.098210108973674, -3.3404630096588415 56.09640867558131, -3.3256417487609573 56.08784833567386, -3.320829993614193 56.08850733256621, -3.3230790695251047 56.08468239984351, -3.3333734052384028 56.082024244835196, -3.3364621620253794 56.07980467187126, -3.341053199085725 56.07925977375178, -3.337232376663109 56.07546083277938, -3.3348992595536275 56.072107546826054, -3.3616273718619096 56.0672994151141, -3.3613851074873096 56.06452825330304, -3.3610514750126534 56.06458674542475, -3.360584920835247 56.06392696084107, -3.3663029235234188 56.062384760128616, -3.3675120824069182 56.06101724321481, -3.3732351893048804 56.06045139011562, -3.3750163797282915 56.055807582177, -3.3710639625189764 56.05421080209424, -3.369002880258884 56.04992600893479, -3.380568177235138 56.04898932752843, -3.382522658878372 56.04649101180731, -3.382432402377227 56.04217264784043, -3.3808865218209454 56.0383980399039, -3.3733756399558876 56.03187568437925, -3.388059010802341 56.02250643661013, -3.390439341255346 56.017131368971846, -3.387699600490438 56.011459042714094, -3.401368171678487 56.00900412323601, -3.408921181417525 56.01619195267983, -3.4394463335874845 56.02223992498096, -3.4559421522255307 56.020374930916844, -3.4627672417013815 56.02401824318293, -3.459604285148968 56.02903072955898, -3.4783660993270886 56.031023149970125, -3.493489682139276 56.03525206080759, -3.5066665885314157 56.03466947336737, -3.513330481207424 56.04032712141902, -3.5444039783076673 56.04011271443626, -3.555978220444351 56.04455274101654, -3.5583236449299958 56.049009773390665, -3.5755299270880587 56.058869786335684, -3.5867772792740147 56.0561044673485, -3.586443439988293 56.0510254403195, -3.593854534865933 56.04582951156538, -3.6058538009206775 56.04571163325854, -3.617516630606511 56.05596768261518, -3.638707833862207 56.054596240322965, -3.6527475358710153 56.052374527582685, -3.662341841548222 56.047855049819084, -3.673719417482793 56.04627062828467, -3.686352534138257 56.04805663987696, -3.708413378784072 56.056369866047184, -3.7243477248119916 56.06892644601409, -3.7392980613176023 56.07706653463265, -3.736350963758673 56.08202703440008, -3.735478748671203 56.08375115616362, -3.735624883059535 56.08442755241321, -3.7198025922325924 56.09026533347686, -3.717447655527795 56.10275369901225, -3.714629199067639 56.104493035067605, -3.7098171558685205 56.10461458653774, -3.6909299485734435 56.10211780684947, -3.684299365248119 56.10190112413166, -3.6743584086027017 56.10401865330536, -3.673749503171642 56.10643784577936, -3.657599313909686 56.10959772594027, -3.643810039145053 56.10900839014931, -3.64309942539836 56.10927664312881, -3.638309611666249 56.109144398582416, -3.634938916141194 56.10883952635021, -3.6292377799546314 56.11145609609704, -3.6510224268108122 56.117756101076736, -3.659651537715751 56.1198374858592, -3.6570816706372575 56.12430385299091, -3.654407869249629 56.12493996009321, -3.6509299291202852 56.12907598670035, -3.6499427979361383 56.12935695777514, -3.6464766907420594 56.129573945589996, -3.6436502465868124 56.129661913854996, -3.641570846626835 56.13017660426656, -3.636445015044532 56.13037671896194, -3.6311622093973064 56.131278692533115, -3.628382858983098 56.131228204597534, -3.6256127610886018 56.13246430654992, -3.6149030201794647 56.13539040696492, -3.607245144975409 56.134859486239954, -3.5947094288176364 56.137732084853326, -3.5915822576052636 56.13791978489247, -3.583806154209015 56.14406536138808, -3.582658447999383 56.1486053003564, -3.5829675050330723 56.1507579226578, -3.577462959661915 56.15455651417166, -3.5714218649236495 56.15738672410456, -3.563923247630854 56.15987565900184, -3.5565425645071715 56.157342334359235))
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>