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/S01005001 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01005001 geosparql: asWKT POLYGON ((-3.406511440942666 56.35229187022601, -3.407249180173787 56.35123222301542, -3.410632613871304 56.351964973815726, -3.411173358794788 56.35105336236197, -3.413133431580335 56.35106642068635, -3.413300398518132 56.350768002423465, -3.411384320886089 56.3501518935701, -3.410694504515777 56.35075277135186, -3.409873801315054 56.35043864450802, -3.41096187340729 56.348790951847526, -3.407530258222805 56.34919837390951, -3.407200192435131 56.347563823059104, -3.406149446627746 56.347647987774614, -3.404747825057355 56.34613903769468, -3.406397797188924 56.345248755089195, -3.406031389558045 56.34452510942145, -3.407169668806517 56.34336207882277, -3.409850881687333 56.34280147129451, -3.409149536339442 56.34158542188921, -3.406010801598008 56.34176688303705, -3.405941468286368 56.341201600099616, -3.403751393923481 56.341918294542936, -3.402354364682874 56.34176339626613, -3.402391695136804 56.34101720269542, -3.403386472842012 56.337708362150906, -3.405493223119019 56.33735135447825, -3.406300712195173 56.33821442889878, -3.418448233953393 56.33861505585466, -3.419197174328847 56.33308055065137, -3.398292495739322 56.330236347656516, -3.397023438753164 56.330915573694476, -3.391506175834638 56.32997132911141, -3.390352466335757 56.33249115850093, -3.385165537987548 56.33129133763148, -3.384989751287522 56.33179647449039, -3.37940906514459 56.32819278300213, -3.377569537550817 56.32623129842027, -3.376831228561852 56.326350153693944, -3.376269988181005 56.32585344802738, -3.377618776389406 56.326089926673696, -3.378695625380173 56.32581641408562, -3.379675518726293 56.3248203639105, -3.378164743776381 56.32534983338619, -3.378567483397201 56.32279305879587, -3.385181805487181 56.32325837726512, -3.385999422736091 56.32083220831135, -3.388045589217758 56.32062958957004, -3.39342531104601 56.32227637504232, -3.396059066856898 56.32389054063287, -3.396233388261997 56.3225682203439, -3.397423995689619 56.32196176868533, -3.399032694973945 56.31996685583603, -3.401956479158362 56.31944859082112, -3.402808173050513 56.31842361434726, -3.403072088765353 56.31724355732432, -3.400897948409882 56.31350335155491, -3.406018820565647 56.310758725044025, -3.409668433241373 56.311534894717674, -3.414050570795554 56.31069423085876, -3.420884747266823 56.31102932665019, -3.424692112098836 56.30952098095082, -3.426121724582891 56.30797702612179, -3.428927094484964 56.30735165535786, -3.433666420005694 56.309597114439214, -3.443066000167613 56.3072326226228, -3.445905852323501 56.307100656155654, -3.447446323563417 56.307657720271884, -3.450858304058371 56.31110409962826, -3.452248733907406 56.31068346274801, -3.453087164898121 56.309775094144634, -3.455363114551723 56.309263157009056, -3.457735725614876 56.3100169756696, -3.464375882812793 56.31076528469965, -3.46631930558141 56.31043678539125, -3.470509290236695 56.314304717429266, -3.471580229189148 56.31398649522788, -3.472357700287675 56.31612475547121, -3.474776089746064 56.31552990199086, -3.478486952290742 56.31616854421498, -3.47996447939966 56.31716624200267, -3.482416723609773 56.31702908926408, -3.487167249006414 56.319865428322274, -3.489657484849314 56.32236039118125, -3.486661181144549 56.32182132856211, -3.483898766395698 56.321890396860226, -3.483587225045361 56.32261295813814, -3.484371029820153 56.3240591951917, -3.489970613454954 56.32496406567801, -3.489701891243719 56.32679966895356, -3.48611774972474 56.32734934634752, -3.486675481850814 56.32672364651726, -3.483847064394766 56.3270533626854, -3.47349769899741 56.32486429670929, -3.473381809456419 56.32844047404457, -3.471585504376767 56.33093279307195, -3.468971989851191 56.33068527009168, -3.466786298464179 56.33399979730581, -3.460922597321512 56.33293693995506, -3.461114693815963 56.335423605209144, -3.460429053253015 56.33568327876843, -3.461404264408007 56.33665117626498, -3.46071200651519 56.33758482830163, -3.461139085863844 56.34029335368261, -3.463193374284131 56.34324324503489, -3.46094514876583 56.343260791544004, -3.460271257617575 56.345101736909854, -3.458526384711488 56.34474490639692, -3.458220914346069 56.34649164977487, -3.459694390136302 56.346519224959174, -3.460068306320657 56.34783565835369, -3.462824394719925 56.347551561955854, -3.466732580334691 56.347774917776505, -3.466560051606576 56.35086790352882, -3.45570441988303 56.351364310690926, -3.45096006986046 56.35086286780137, -3.451039379860661 56.350394704755224, -3.449137551265299 56.35059666705366, -3.447108710900693 56.350009379705625, -3.447353209555357 56.35603563862609, -3.446370235781208 56.35743084479031, -3.444394360828768 56.35696868781023, -3.440824235606083 56.35757632062361, -3.436270429042701 56.35742252186922, -3.42987531342084 56.358233265193874, -3.428185090529744 56.35761480650697, -3.427530038319279 56.356544126455304, -3.428277574658464 56.354028631513096, -3.425618319917437 56.35345724256449, -3.422553492705548 56.353726082651264, -3.42081182306929 56.35476139430189, -3.423598077391662 56.35830553520543, -3.422980941753957 56.35955258457532, -3.421909987621734 56.35991530160111, -3.420288874846133 56.35941274696693, -3.414323949892994 56.35524893197706, -3.413953414989306 56.35354597070124, -3.413153682361518 56.35292612767732, -3.409715088559762 56.353153985880304, -3.406511440942666 56.35229187022601))
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>