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/S01000662 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01000662 geosparql: asWKT POLYGON ((-2.871794699834304 56.67816345120662, -2.868103776244344 56.67380499734139, -2.869048277618469 56.67371755077912, -2.871206219842453 56.67176190329192, -2.872019099582108 56.671630435211796, -2.871916145669845 56.670705793857486, -2.873095005063891 56.66946670841863, -2.869929203021242 56.66733268203294, -2.866942315521013 56.668000372804684, -2.864411776088438 56.66793711213746, -2.863823867426476 56.6657760217165, -2.858914048216434 56.666546688598665, -2.857786632148934 56.666014451420615, -2.853177714032727 56.66540921565495, -2.853836186553891 56.66494651127616, -2.853124453009109 56.6637744742192, -2.860746928560982 56.66106270819871, -2.859070410406483 56.65774797011048, -2.858363761140696 56.658132374327444, -2.857835633888991 56.65790538100904, -2.857348446503359 56.65813628975802, -2.856643442563659 56.65776060686008, -2.857527876961367 56.65795845308458, -2.858721728284561 56.657375239686466, -2.858664833153987 56.65702525070619, -2.858996129809903 56.657247568793736, -2.860062940144611 56.65681794802913, -2.864555029898111 56.65424432335007, -2.866929029026215 56.6532126041905, -2.870878657489641 56.65402954155689, -2.875499936668816 56.653556901265894, -2.875156984558469 56.65424210741248, -2.876328042891688 56.65409910543919, -2.876695879155963 56.65480065152523, -2.871972332843585 56.65827889129407, -2.869953106512466 56.65885243824905, -2.871008475020323 56.661072189527886, -2.874576105346915 56.66014876381426, -2.874546478424237 56.65957398776336, -2.881406240659719 56.659229130986006, -2.880847800583241 56.65403646485449, -2.880505807146958 56.65349463775664, -2.881508293073485 56.653119193267464, -2.884549081771772 56.653421050676634, -2.884417467739375 56.65267630079837, -2.88714565712572 56.65286352244394, -2.887638732812625 56.652329941388984, -2.891748840522818 56.6537110762724, -2.891319504210253 56.65210598691985, -2.894755625493027 56.651883682354104, -2.894554428735311 56.65039840642013, -2.895173972151916 56.650897196362344, -2.898916168190277 56.650577953496686, -2.899442039571768 56.65085693828183, -2.904459606521418 56.650536570408875, -2.906704596148395 56.64988850202609, -2.906462304211929 56.649529982607724, -2.90372675038746 56.649869306337465, -2.902373120870052 56.648504548511305, -2.905435485860998 56.647673732781, -2.903990463473855 56.64589638668773, -2.900967652951662 56.64769717173952, -2.895914628769629 56.648488317207295, -2.895268279967744 56.64713759628463, -2.895689188307687 56.64595836255612, -2.898565478938205 56.645695520628344, -2.898443698895054 56.64424320008555, -2.900028586883931 56.643943511255316, -2.899593147345491 56.64291374883807, -2.901794339825002 56.642704966710454, -2.902333922827342 56.64324157262971, -2.907810913155701 56.64290598099068, -2.911130894466158 56.64211574923049, -2.91091712162535 56.64171247848378, -2.916295204753533 56.64089205098204, -2.918319151588888 56.63963731333454, -2.920814394862118 56.639663797422806, -2.92866390269207 56.63663169481015, -2.930534628041341 56.63381464436333, -2.931977492055244 56.63348939946439, -2.930174320043487 56.631759952586215, -2.935784415199264 56.62988510621846, -2.936542206951118 56.628891139358785, -2.935039933634664 56.62746496047289, -2.944696490355436 56.61927924031321, -2.947246338847172 56.62218871921508, -2.954961690679937 56.62116842336888, -2.954458374411061 56.6205972928842, -2.960161434399679 56.61928662690787, -2.961894028734609 56.62077361732431, -2.966460287663466 56.6215378291101, -2.967498991014858 56.62264380601162, -2.968664800552123 56.6223472380705, -2.969803827617728 56.622913358363924, -2.970890463373861 56.622707229429835, -2.972166634355899 56.62099923634149, -2.972932075710282 56.621613167294484, -2.975868632578444 56.62173391702661, -2.983503697857028 56.625177668433224, -2.982771034656574 56.62708812599103, -2.984184570041482 56.62691523600708, -2.984959835591945 56.62726847273535, -2.969247263960403 56.6304465131204, -2.967515151857658 56.63601226485224, -2.969444887635533 56.64005812904764, -2.960346892239023 56.64711840460154, -2.967303408882967 56.65130509309225, -2.970670172261472 56.65348002157602, -2.963675413946749 56.65421717234673, -2.966297309110032 56.655966737995264, -2.958908652956052 56.65727268183096, -2.953668902041575 56.65713325035318, -2.951460976595108 56.657637911236705, -2.94498384170476 56.65620223767273, -2.944267070898073 56.65558776791896, -2.940960044653448 56.65575655765723, -2.934343648549515 56.65798057082406, -2.937307351499157 56.661057835164534, -2.938797216954022 56.661271214259905, -2.938971046160345 56.66235698915985, -2.940590593985063 56.66318927693466, -2.941579015191392 56.66487981751202, -2.941989371215052 56.6689016278468, -2.945334658558736 56.67151762233098, -2.943464015108774 56.671756414821104, -2.939230501792431 56.671473977090116, -2.935393386544668 56.67269775713855, -2.93430570366535 56.673586372131986, -2.928886325217954 56.674192922890356, -2.925575834390731 56.67428944892511, -2.924606304261007 56.67468297002478, -2.923620091737031 56.67372898573414, -2.920094775923006 56.673045337347446, -2.918952563096436 56.67237996213372, -2.910981350332421 56.67476543154982, -2.908970572794516 56.67394460259994, -2.908853294657415 56.672463074340655, -2.904439614795435 56.67214483313589, -2.899250986197039 56.678336543653785, -2.894371285050271 56.67972829086727, -2.896630288309726 56.68206588462054, -2.893743258825471 56.6835330799755, -2.890738361422385 56.68485730635351, -2.876129620038611 56.68631753797728, -2.871794699834304 56.67816345120662))
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>