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/S01002345 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01002345 geosparql: asWKT MULTIPOLYGON (((-7.263915571860227 57.46659770033662, -7.263933404870583 57.46564951058795, -7.264821867220705 57.46544134239722, -7.263948991276613 57.46216639181309, -7.270159682141071 57.46065544738782, -7.27360505543417 57.46104192358417, -7.278002675091598 57.45981357232094, -7.279650859624082 57.46047327571975, -7.283167363754356 57.459965631242845, -7.285312489138232 57.46071237071901, -7.290573807137975 57.455766757250416, -7.29332248527228 57.4552641242257, -7.296747469791768 57.45355409217818, -7.304512651311581 57.45414460433456, -7.315696327540027 57.4583327308972, -7.330152186659978 57.45915257978212, -7.330055787370789 57.45494512565164, -7.332055632647029 57.45400541990443, -7.334103777408746 57.454197512562104, -7.336198195132424 57.452191834288804, -7.335398089872794 57.44957106857633, -7.339044263120886 57.4513330782072, -7.341905399907357 57.45080657783961, -7.344812304258844 57.453103790689944, -7.347959017322867 57.45281699112725, -7.348921354611774 57.45175015004923, -7.35283110009712 57.45177272229885, -7.356148712853615 57.4511724646276, -7.359202253190174 57.454803998351494, -7.360676109371027 57.45403922432366, -7.365527460684622 57.45512816445162, -7.371522848441241 57.455025142442956, -7.373012583447888 57.45561843642275, -7.374930655947019 57.455491451432465, -7.3758894811057 57.45667436224322, -7.378309841451038 57.45666983760804, -7.378673274907332 57.45516940361407, -7.380557581889551 57.45492678373745, -7.380117502527671 57.449105047037804, -7.387176393364343 57.450107651070006, -7.390290014970628 57.45129709648587, -7.39140319537469 57.45080838969203, -7.392600648613766 57.45111700528826, -7.397557010021874 57.45065230990743, -7.399885541159951 57.45149725189598, -7.410055129034124 57.44948508994965, -7.410455526486905 57.45297767441534, -7.407544331318143 57.45496583105367, -7.401611200935578 57.45695779159676, -7.400735401751782 57.4601812269231, -7.401705046142415 57.461543491303914, -7.409108099905318 57.462368160940734, -7.412113633843777 57.4649566857658, -7.408851632945998 57.467400925110766, -7.408492686207349 57.47095317259205, -7.406544643366167 57.47133392562536, -7.405647464569769 57.47193046515923, -7.405670417472215 57.472649438105265, -7.402931114453511 57.4724251784258, -7.400010914769383 57.47402651678575, -7.398164535254202 57.473853804249295, -7.395171024070889 57.47439623950512, -7.391940766055953 57.47356284014366, -7.389328407077016 57.47397179790165, -7.386500124484071 57.47518189316987, -7.381774339660652 57.47406131098483, -7.37888727292698 57.47476078296856, -7.376255119219633 57.477645118386604, -7.374550604779668 57.478662899774136, -7.374667828496898 57.479458827101034, -7.367844981430008 57.490432316799556, -7.360896994711299 57.49273542423807, -7.352228758519777 57.493572463599776, -7.346120295754364 57.4935172245654, -7.346167700485746 57.48732555377039, -7.346919989948691 57.484133669387745, -7.342116966656103 57.48337712847506, -7.337994514157241 57.48534696065839, -7.335172138894389 57.484657775715434, -7.332642887805521 57.48468918120177, -7.333644931383229 57.48287715979323, -7.337064235510276 57.48091203025885, -7.336908526504561 57.48048888421721, -7.335849644374324 57.480306303520855, -7.33337319601411 57.4810180334393, -7.331081009974701 57.481064651547484, -7.330742079705947 57.48163514589891, -7.329669590802709 57.48168059381641, -7.328836515915214 57.48143781866998, -7.328574486613925 57.480286085266606, -7.327378366947348 57.479805899895126, -7.326814641968579 57.48076509505959, -7.325271157598555 57.480476550916876, -7.3237700608215 57.48112150694088, -7.322190969170273 57.48091026655528, -7.323135810313727 57.479024916434305, -7.320227843361367 57.48063940312603, -7.319643372708939 57.47952657697316, -7.318390972247973 57.48059069629338, -7.316044826069402 57.4805887592682, -7.314532571578866 57.48115823463551, -7.311985241715555 57.48041900216343, -7.312624335611493 57.479330299433656, -7.311824334476426 57.47931353437796, -7.311377229160664 57.479787436629444, -7.309004666795613 57.47928091102947, -7.309111554040965 57.48033809472473, -7.308363565061039 57.481001637535414, -7.307291047397554 57.4810468952342, -7.308260959084818 57.48287658359808, -7.306933117435175 57.48407015179076, -7.305105320802868 57.483439453533606, -7.304529141389535 57.483994595762105, -7.304829772339487 57.485094176447724, -7.304127415905254 57.485427136817215, -7.30319019129698 57.48511275084382, -7.302222581996269 57.48555800050747, -7.302027084896771 57.48485843729962, -7.301320230378874 57.48483767544518, -7.301152753392568 57.48529974250064, -7.300235413497108 57.4857681336069, -7.299374041609418 57.48565274813581, -7.298926699248114 57.486126609940136, -7.297066886639899 57.48559825394874, -7.295646634674594 57.48583500114876, -7.294173070641193 57.48473421408036, -7.29333558473237 57.48267335758075, -7.294558024846297 57.482401962805035, -7.294590882814847 57.48118131361943, -7.296233227111067 57.48092362980055, -7.297643420742561 57.48131676108592, -7.298232048117086 57.48055034917273, -7.298359903581198 57.48030613402566, -7.296357091645084 57.48049104743167, -7.297572029051168 57.47905720584578, -7.296200967942309 57.47780768348699, -7.293712104528133 57.478339828557914, -7.295395058280914 57.479488254291745, -7.294869068344832 57.480365140904404, -7.292319510936221 57.48015819952404, -7.285087837233802 57.478388260356056, -7.284615055951689 57.47834527112308, -7.28507140554464 57.479080279938884, -7.284429389880676 57.4791575298155, -7.284123347838904 57.47865503107906, -7.281744961383085 57.47916713355101, -7.278549722703112 57.478505681330844, -7.276946828012207 57.47790674496612, -7.276930302852051 57.476663053367986, -7.275119481903968 57.47543177061061, -7.274259379333722 57.474160604588135, -7.275298158974787 57.47408561852326, -7.273876524455253 57.47296368969665, -7.2752443625721 57.472906326392376, -7.275890821632116 57.47237643052331, -7.276894299659213 57.47302566225614, -7.279459822124283 57.47254091005711, -7.279918695503295 57.47281074881514, -7.280306756963404 57.47242993388268, -7.280727116725992 57.473078478796815, -7.282249134504531 57.4734419337681, -7.282572423443519 57.47277473414202, -7.2814526370801 57.471967025465545, -7.279077141296534 57.47199133970924, -7.279944824395105 57.47154011291804, -7.279243009532703 57.471041655862514, -7.279736114599351 57.46961315138394, -7.278359983210215 57.469934873879716, -7.277192208627254 57.46911658623073, -7.277542055232495 57.46831001157115, -7.276792124135982 57.46677545696302, -7.272757416378635 57.46623383167839, -7.270505225514697 57.4682949252288, -7.26956035802989 57.46814496079978, -7.268920412008756 57.46736614443521, -7.269814763457278 57.467470839411476, -7.269677826512423 57.46591271394606, -7.266910591777154 57.46318523908467, -7.266420085475213 57.465575273897535, -7.264942581316866 57.46660863248825, -7.263915571860227 57.46659770033662)), ((-7.338041359998497 57.49183708271271, -7.336704133932063 57.491754843857656, -7.336261079294961 57.491211447104774, -7.336756229082577 57.49074827323795, -7.337027554294492 57.49115998226161, -7.338054376765577 57.4912048570757, -7.338508443837727 57.49054128274693, -7.334482156407643 57.49043414399779, -7.334369441138261 57.48998412084989, -7.336613014848615 57.48952890994891, -7.33559351752591 57.48873203026722, -7.33257545015061 57.48870208966596, -7.332910860582345 57.48818884578666, -7.333768206494103 57.48851886793932, -7.334534419537826 57.48814528024125, -7.338166412347213 57.488123853561994, -7.339491693940189 57.487404358759775, -7.34024483849583 57.487662948768765, -7.339641461936457 57.488427618432894, -7.340034138310812 57.48942794675333, -7.340640082959588 57.48988229565868, -7.341979710388978 57.48990121908179, -7.341898689421662 57.490308933852134, -7.340185413091515 57.490861737114024, -7.340130026511402 57.49192530384797, -7.338041359998497 57.49183708271271)), ((-7.273099257787967 57.482338168404496, -7.273172051572849 57.48154289771409, -7.272011055240627 57.48108589082386, -7.271803437763909 57.48138113913592, -7.2712308795482 57.48088260937902, -7.270370213116437 57.481205214952325, -7.266658386256263 57.48019763960038, -7.267090957487163 57.479943550137364, -7.265892411001892 57.479875738094655, -7.264175391938612 57.478599158687764, -7.264059400483576 57.4777949450511, -7.262829838333301 57.4777284081634, -7.26245733294957 57.47818223328294, -7.263096445384797 57.47849260885669, -7.262351113002197 57.479181159321215, -7.261845731377817 57.47871349636659, -7.260724199979573 57.47896266628991, -7.259041787142014 57.478358798921754, -7.258183207305538 57.47847896434516, -7.258427257614928 57.47669893376735, -7.26049419261625 57.476932763306344, -7.259306658336151 57.476072228671555, -7.259339286804132 57.47543034252786, -7.257910898480066 57.47506867454061, -7.259245425892856 57.47499602720177, -7.257440709809051 57.47397586235143, -7.256364328154614 57.47388600923878, -7.257028187575291 57.473066054321606, -7.259199371262559 57.4727224570009, -7.260212320775826 57.47367457324404, -7.259751756052255 57.47264878421186, -7.26051632649333 57.47209428800944, -7.263534376013733 57.47198487356708, -7.26458027124532 57.47251419454569, -7.264433210910847 57.471711272874096, -7.265525498591931 57.471261018672536, -7.26795069388981 57.47137858469511, -7.268382115228461 57.47198417249983, -7.270312740523369 57.47235839702748, -7.27047049065474 57.47670054707038, -7.271268500908733 57.47616143586257, -7.272122498135507 57.47622677810498, -7.275275854912334 57.47943200349849, -7.276414629948062 57.479519082444305, -7.276354028553102 57.4803981271945, -7.279908154967184 57.48009729249046, -7.280836253155326 57.481322517341134, -7.278904693957898 57.48202725551529, -7.276303494377955 57.481563288413, -7.275775637208294 57.48202368223056, -7.273099257787967 57.482338168404496)))
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>