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/S02000007 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000007 geosparql: asWKT POLYGON ((-2.278146961201506 57.137468013746066, -2.273239250386109 57.144099319547756, -2.26843889802726 57.14525952234644, -2.267070276740338 57.145675667062335, -2.261658016503286 57.14541699992994, -2.26208500000819 57.14411415927023, -2.260232419531939 57.14366537216561, -2.259000664853533 57.14395154531665, -2.257098204026387 57.143323745091166, -2.24652817680039 57.143458970413775, -2.246158594454945 57.14466746036459, -2.243866925628525 57.14548041784108, -2.241306079974737 57.14557519839655, -2.239909169234637 57.14434801319303, -2.238747955111515 57.1444875022663, -2.238758559702811 57.14417175677548, -2.235372836265215 57.14410265836239, -2.235201017633655 57.14445494827171, -2.228421187201552 57.143721893043825, -2.228311440258983 57.14468327677716, -2.225033632433894 57.144118282578724, -2.218478776620694 57.14410756557464, -2.221623589458496 57.141501497641045, -2.220537544188276 57.141695765735754, -2.219647479725054 57.140831747019746, -2.211490751361638 57.14138548976144, -2.210981195160253 57.139710099326045, -2.212368130485677 57.1395370759009, -2.212356130395012 57.137444047229394, -2.214618440713795 57.13712262765355, -2.213253432821181 57.135271192561824, -2.212000714095713 57.13549140888539, -2.211393595892248 57.13457917539206, -2.211834428424908 57.13289052499363, -2.209526086909234 57.13075643740679, -2.211506717052888 57.13046565159843, -2.211586749059462 57.13001636533809, -2.214853295895201 57.1300479134769, -2.220959777856493 57.128410179447734, -2.220013696357332 57.12729492521713, -2.214265647100277 57.12765824428883, -2.212501525964695 57.12822718722028, -2.209930593293957 57.126345079344894, -2.206990988684979 57.12642183079815, -2.202748841926899 57.12390452817423, -2.198389655230255 57.12375977573757, -2.192057282043138 57.12279849652658, -2.191630201045913 57.123248302959205, -2.189627454183993 57.122397963412794, -2.18647076143059 57.1219445637092, -2.18330042203681 57.12205704301302, -2.180703785880646 57.121288266493444, -2.181740914240907 57.120613037730436, -2.178661700004678 57.11905441372147, -2.181067953658611 57.11808078383531, -2.183495229073901 57.11804132144922, -2.187419849114877 57.12020941175868, -2.187406025003341 57.12073944059214, -2.189663349939236 57.11972993912988, -2.195733420640785 57.118382121092324, -2.198086271885755 57.1203598325553, -2.201569798049531 57.120098764394626, -2.200346330948674 57.1194258488708, -2.201829385621105 57.11883956382443, -2.202796109002582 57.1196450200704, -2.20496069274805 57.11885347485796, -2.206388757715876 57.11972099243381, -2.208067947738195 57.11941677491154, -2.205818494672788 57.11787091989124, -2.20658165552246 57.11741218610218, -2.207728767302753 57.11788507869144, -2.206729179360273 57.118234910983396, -2.208250425226339 57.11936824245585, -2.212989358484506 57.11826413361807, -2.218988083673515 57.118999388610796, -2.212574949990245 57.115111781576196, -2.217486435486309 57.113558257271364, -2.213383455100413 57.109271398076714, -2.202231125326538 57.111817855431646, -2.201125812141745 57.1109086347687, -2.204686912546851 57.10998659433686, -2.208391637621103 57.10821081474985, -2.203451742407675 57.10452688836927, -2.209384398944349 57.10275642561187, -2.210382775300855 57.1012545739734, -2.214158182370092 57.100484609149404, -2.216206635262437 57.100786513933976, -2.217994497032785 57.10168172205674, -2.222319594032041 57.101763953531886, -2.230699080087496 57.095550454490024, -2.233449514827674 57.094656026518976, -2.235328949407979 57.09435607148013, -2.237110378264971 57.094217959141474, -2.240018956598755 57.094850220669834, -2.244729667535339 57.095963990911166, -2.253410853904249 57.09600960855916, -2.253713868663036 57.096880362054286, -2.249088559789912 57.09871321674598, -2.251453331622042 57.09939120995133, -2.252671147779013 57.098867734957565, -2.25538610440484 57.09994334375241, -2.253987550845918 57.10069763028007, -2.255990036899946 57.10146609778103, -2.253575979181525 57.1032676345755, -2.254882646015514 57.10365125442289, -2.254902430791129 57.10412732067268, -2.258840356670597 57.103182467871626, -2.260925845110639 57.10153676130812, -2.262904113588095 57.101191269204456, -2.260919376001813 57.102965094065404, -2.263699854848187 57.10400131065331, -2.263192100799226 57.104559332199635, -2.269052366161104 57.10685553629351, -2.269303520080984 57.10734009072632, -2.268651179471644 57.10844641160768, -2.264202614340163 57.109695544229446, -2.264110918960212 57.110728787944474, -2.270866015806511 57.11106470613491, -2.272458632660575 57.112103299757514, -2.276660537524412 57.113199011245285, -2.274771954635826 57.11457758060261, -2.278545524396568 57.11795587580275, -2.277459628786668 57.11848828581137, -2.277727206018596 57.118936845885884, -2.278634775874079 57.1188450063654, -2.279002662009767 57.119455035390196, -2.278359705050481 57.11960019483392, -2.279755603225293 57.12075590045855, -2.281061606321214 57.12093264603099, -2.280555563939426 57.121715309814356, -2.278147310143025 57.12210694958527, -2.278287461904895 57.123175626759206, -2.281349150215747 57.12400421359699, -2.278077133037683 57.12596084642205, -2.265869323498664 57.125708869639055, -2.265534249059171 57.12735348434261, -2.272115312174453 57.12831851955669, -2.273440969236091 57.1288905510059, -2.2768394977717 57.129001724605345, -2.2766591340178 57.12907840720362, -2.27527342265603 57.12992741475348, -2.275069560038022 57.13018461575834, -2.274878596682202 57.130537251268606, -2.274763345823688 57.131348541803796, -2.274765264496582 57.13186066443202, -2.277710810218455 57.1333633607031, -2.278704098806863 57.135377530085684, -2.27844115921638 57.13671411957704, -2.278146961201506 57.137468013746066))
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>