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/S01001269 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01001269 geosparql: asWKT POLYGON ((-4.257869191502512 55.40998566275526, -4.259019835062331 55.40634223039183, -4.252248861846778 55.400058704599374, -4.248178048291599 55.398183242283054, -4.245639406659431 55.395749215266896, -4.244900492167711 55.39608635694176, -4.244794104675993 55.394488444167315, -4.244235858946237 55.39411221069925, -4.238891632657983 55.39399450691025, -4.233279128749617 55.39247934494329, -4.230159077179342 55.393156484647804, -4.229366491181494 55.39254178466026, -4.229873506997131 55.39201123263867, -4.228631621698711 55.39014640630818, -4.232321335292973 55.38949485199019, -4.234016913214979 55.388762807309085, -4.23576866389508 55.386789366163065, -4.234193084628339 55.38628788660607, -4.235495455488746 55.38446647004954, -4.234525430940391 55.383783145161296, -4.236036093739185 55.38285671377926, -4.234602238363577 55.38178640294376, -4.232925004964302 55.38171819175512, -4.235664475333547 55.37711118351759, -4.237423959639947 55.37696213686578, -4.239111455420054 55.374980826368265, -4.23768171620815 55.374818267592694, -4.237254273924909 55.3731183694057, -4.234785305286162 55.373837650179205, -4.233865767743023 55.37180519534043, -4.234889137097928 55.37148088952638, -4.233628886215186 55.36956252031184, -4.231744315629608 55.36359116561366, -4.252193702783952 55.356881118851014, -4.256991631286629 55.359129476145704, -4.261896494652945 55.360476860252035, -4.273473561967261 55.35379951176223, -4.276726731260332 55.35275921855245, -4.276805591928333 55.350286010332674, -4.282652832313807 55.34718382981976, -4.283767026878639 55.34461935357236, -4.283497321406388 55.34267396429811, -4.279962588057478 55.3396211046304, -4.2770890266824 55.337913033217134, -4.275724593470916 55.335844223778345, -4.274465144656834 55.331202831444024, -4.271012619196247 55.32928070092988, -4.264324055224171 55.32783197544785, -4.261947774727529 55.325970527689336, -4.259874544452516 55.32139799765886, -4.257392257843431 55.31793853852523, -4.258337118234469 55.31515271873914, -4.258415074534958 55.31098077637601, -4.259915939226253 55.30883180028532, -4.265889923029013 55.30969179542099, -4.268875231229019 55.31146994150396, -4.273004192906431 55.309820210392665, -4.277772805530957 55.30971336138404, -4.282603860642251 55.30795132849111, -4.288050671829176 55.30840663949558, -4.301494396881869 55.31089536260516, -4.304409918156888 55.307910197372, -4.305472372047883 55.305310508105975, -4.311342318367331 55.30004024311813, -4.311029997304291 55.296837339692985, -4.311795196462875 55.29293092155368, -4.314270847068707 55.28945946679549, -4.325668645132159 55.28263639907789, -4.331094284563197 55.27713987715494, -4.33337195969814 55.27624245374901, -4.33889813905853 55.27757480245188, -4.339807478662679 55.2769731265816, -4.337609823200621 55.27574788346725, -4.337486756492846 55.274725566695125, -4.338892930680426 55.272667250617395, -4.34513853678045 55.27125305235654, -4.347013065332063 55.26576725734297, -4.355210582066401 55.2653424308593, -4.358631171326069 55.26622014103003, -4.361452811701777 55.26761270220543, -4.364278713812072 55.265895117053276, -4.366392131765687 55.26702264492485, -4.372059845472933 55.265186808792826, -4.377262468484467 55.26584957334687, -4.382988195617575 55.268146794697465, -4.386925953499428 55.27002032909001, -4.387875129784461 55.272194949206025, -4.391425368708781 55.27363549006023, -4.390867673379738 55.27640588519761, -4.394056195022025 55.280199436718966, -4.395616044105553 55.28335075307027, -4.398997918192139 55.283023654755915, -4.39983658494604 55.28169335229537, -4.400175368627769 55.27589529506593, -4.396938574626876 55.26942067554468, -4.392326157450794 55.26523972380896, -4.392260745128405 55.263965464356396, -4.390026965774037 55.26110595480144, -4.389251323580269 55.25836865940295, -4.385712349664465 55.2582806798125, -4.382790448211167 55.25700130895995, -4.381089790181503 55.25695589962672, -4.378774283523393 55.25453956372078, -4.373381184964707 55.25250534597826, -4.371215048579212 55.248898147856444, -4.372193823499664 55.24393545214755, -4.372442523055949 55.23833977400365, -4.37181500552684 55.23608060376091, -4.372924753730714 55.23402490260634, -4.377817879751563 55.22693653742252, -4.387081837424414 55.22328597742869, -4.387602052329742 55.2212084268936, -4.391973932076399 55.21781495667623, -4.387072807084061 55.21591919204499, -4.384459959888055 55.21231731424777, -4.384666493322052 55.20736058005855, -4.385999865019521 55.204121494303664, -4.385616439914675 55.203840734430294, -4.382228147173663 55.20728423927424, -4.377685929004487 55.20571614048354, -4.380142248086319 55.20476833423024, -4.38014533104781 55.20408042678555, -4.377676310073591 55.201622928577976, -4.379529638191324 55.201010588334675, -4.383502668135523 55.2020430157779, -4.389912439876166 55.202871182620214, -4.394716266762099 55.198822188164584, -4.397245975193738 55.19332537487815, -4.397397577436346 55.186958457208306, -4.398976101730501 55.18577685560278, -4.400957988879283 55.1863400689643, -4.402051528817434 55.185976955145655, -4.405436423186329 55.18218890453141, -4.408174973008705 55.18097528938756, -4.41233355898144 55.18038968331192, -4.41029871287466 55.17088398767132, -4.412933995010135 55.16615772558507, -4.40832753271552 55.16007364079596, -4.408039098311522 55.15633106461201, -4.413026428863265 55.14845717261118, -4.427610138565416 55.14023936640864, -4.434136778808023 55.1397046251871, -4.435835209241487 55.138735842137294, -4.437671733436288 55.14029011396678, -4.441349153375684 55.13806814948019, -4.443524338693368 55.14900881347519, -4.44166763339252 55.15458311658067, -4.44763224815205 55.15841849000195, -4.448223995936702 55.16166952321428, -4.444009855995521 55.165412547191245, -4.444122250247386 55.1703271355991, -4.447891721472992 55.16803125099726, -4.455198393238365 55.17117413780902, -4.458189535090295 55.17129361093991, -4.462218082370973 55.16989991649941, -4.465539359166907 55.173077714947944, -4.467605571577214 55.18033478999417, -4.468707073764909 55.18138214798692, -4.46749034879333 55.18482252524407, -4.469107277878365 55.18734259316786, -4.468710854890646 55.1898225371637, -4.467461298506415 55.191465818408055, -4.467790129123423 55.19271758931375, -4.469471137805937 55.19449028788085, -4.476041054620826 55.19780870373901, -4.472214030172939 55.19970211106253, -4.468770044888668 55.20271123258489, -4.463304529275844 55.20557234924184, -4.459196564470355 55.20650022450841, -4.457499173171297 55.20548277022489, -4.455070601993814 55.206628325222916, -4.453919438851155 55.20988744038875, -4.452304946929386 55.21072890923438, -4.453153298348655 55.21685113209549, -4.452340330518648 55.220247237968934, -4.45082679568834 55.22120350396667, -4.449310953164092 55.22493730180786, -4.449086885933662 55.23689674701495, -4.44301165594068 55.24453310408562, -4.437599306119125 55.247504974369086, -4.431556483831014 55.25226330497265, -4.433149012304391 55.2525441610517, -4.429976019284483 55.254921282242464, -4.433063581456778 55.2555197275162, -4.434760912585164 55.254585716808464, -4.437087899342687 55.25473398505328, -4.437834674932478 55.255516033385575, -4.439863817930464 55.25622058174218, -4.439480969628094 55.25672182866881, -4.440900191193026 55.25709836438486, -4.439616514214017 55.258422487941026, -4.442215596277173 55.25995915988745, -4.440300774069481 55.26094440397837, -4.440435768158286 55.26162724266475, -4.439020706506379 55.262062576952694, -4.441501967187689 55.26767351806509, -4.438679037506098 55.27265703597372, -4.440348128490843 55.27391206140528, -4.452547384021736 55.279361702596546, -4.460734787779886 55.28410450527863, -4.453615783966009 55.28868822199275, -4.448174961216559 55.290622169821646, -4.452071769604702 55.29455286335596, -4.452602627989794 55.29728372339092, -4.447714945941276 55.29952112103539, -4.446889393395681 55.30120955070288, -4.437440627734579 55.30436481599578, -4.437161044558769 55.30725572636838, -4.457660190707192 55.31537494006979, -4.46140316956871 55.31549728566322, -4.465892548008852 55.31494827447957, -4.466062892411214 55.31819871307593, -4.471102381486131 55.32192597448032, -4.47336580330731 55.32460369891996, -4.476939592739996 55.323515539016945, -4.485236951042642 55.3223402075428, -4.486238940948118 55.32248160447971, -4.487228021077529 55.32342324695004, -4.490564643403807 55.322833890498075, -4.495925763770064 55.32385699104024, -4.494844393147312 55.32621614673924, -4.49270885697173 55.32760806154884, -4.496795560228219 55.329915554334576, -4.508096984237541 55.33335133962827, -4.512981938233022 55.33529134779757, -4.517996859761515 55.33877454455118, -4.51737352009332 55.33938963902177, -4.518809288671542 55.339647673801785, -4.519417921453096 55.34029129569282, -4.517112447470587 55.34074332699105, -4.507696702344663 55.345979679716905, -4.499564676800392 55.34609244329148, -4.495983928297252 55.34734318527928, -4.493820223147676 55.348304224593726, -4.491913798879161 55.350850964609485, -4.489789795411678 55.35219763011873, -4.489129295525421 55.35348747687173, -4.488215430616466 55.353506095974595, -4.485127762624856 55.352571216876136, -4.483956156562559 55.353511886621625, -4.483723106411245 55.35533232546444, -4.480734339214547 55.355222275819074, -4.47549606845168 55.35370160926136, -4.474766648972407 55.35414784191261, -4.470876036726198 55.35374121966496, -4.468730579441061 55.35450368552468, -4.467412124237956 55.355626930743796, -4.462237079195432 55.35740322472183, -4.457128406135565 55.355420746739114, -4.455451095741842 55.35432191756429, -4.449564767973627 55.35250756504993, -4.446319560445333 55.353093934990234, -4.437306967940423 55.35584480891681, -4.440457010768039 55.35960203240268, -4.44148798627158 55.361738673216394, -4.436357238596955 55.36478033997279, -4.428887403881552 55.36494705352689, -4.422323792854381 55.3636751690596, -4.419106326799953 55.363702967317224, -4.415803254125082 55.36439751816871, -4.412807163701388 55.366766752763596, -4.401102081326738 55.366583885473204, -4.401941193214882 55.36480566402102, -4.401509045263153 55.36366364332034, -4.396978298527336 55.35815284669699, -4.396991795697717 55.35941994667946, -4.394262540718613 55.360210515531605, -4.390057489188863 55.35955580824012, -4.38900745893224 55.35993588209194, -4.385077900186306 55.359662116824715, -4.383439728777033 55.36023339071595, -4.382491505512973 55.3612585838067, -4.379301253395556 55.36042192179431, -4.376685656886513 55.36127279823087, -4.374388894884968 55.36138936510375, -4.372708237315861 55.36336349968392, -4.37030519455208 55.36223267366423, -4.367989913652255 55.36230453705369, -4.365991359429746 55.361866876717116, -4.362939357438408 55.36228547776089, -4.362778584442886 55.362764968146934, -4.361991292972671 55.362528518843696, -4.359211420659037 55.36407429131696, -4.354121579061673 55.364460097109784, -4.355663029024301 55.36490676584015, -4.354663543080928 55.365348482855815, -4.35322267692036 55.36765927010431, -4.351298793507079 55.36874793904718, -4.351287938496373 55.36962899986832, -4.352950948059943 55.371322736754124, -4.354928157302934 55.372192462586106, -4.354453484098039 55.37323526320582, -4.359146065368764 55.37543676731587, -4.359335856789018 55.37862394715065, -4.359845566213413 55.3805555758319, -4.36051668724171 55.38095607520809, -4.358373743426241 55.381554724668646, -4.356716143133431 55.38129009005378, -4.352769038582625 55.3836761337658, -4.351081804234694 55.38583882445262, -4.351705302214234 55.38650094779951, -4.349861367596771 55.386824041142724, -4.343852102656785 55.38959995893712, -4.342715432073369 55.38960377616969, -4.340765688456406 55.39294880452024, -4.33771978563673 55.39191053756749, -4.32460460753576 55.402254425555746, -4.316060682579441 55.39930670163841, -4.309652102259023 55.39774717745373, -4.307394490674995 55.39803249596081, -4.306095704772942 55.3976974806305, -4.303201914460193 55.396277987253846, -4.299006497873322 55.39990728251833, -4.300316922119024 55.4012578129776, -4.301747333415801 55.40141064913369, -4.303573453497758 55.402391908835675, -4.305792638323745 55.40469597291294, -4.305431015603613 55.407165542648805, -4.304409557191922 55.40861391472392, -4.300622098706768 55.4113996621669, -4.294625237225038 55.41042480544294, -4.287305242283874 55.410058645891276, -4.284592558335469 55.411232879665455, -4.283373633113688 55.413107196085505, -4.280601381207569 55.4151722689513, -4.277273097957097 55.41636680347102, -4.272075908770972 55.41116054519588, -4.270033327973497 55.41025474358656, -4.262788241491871 55.410640722066056, -4.257869191502512 55.40998566275526), (-4.398838134924871 55.321866000998625, -4.397834613305262 55.321713440342755, -4.395912241864411 55.3224897201009, -4.394554537888551 55.32141077482925, -4.392937807943324 55.32100203630286, -4.3898272021351 55.32037057709458, -4.388408396777159 55.32065028594737, -4.389720368737486 55.32149649355388, -4.3898302214028 55.3227976660623, -4.39583937881837 55.3233720173142, -4.39468705434316 55.32465300330086, -4.393864023696481 55.32407590430225, -4.391682513483421 55.32371417307306, -4.391513036968001 55.32404107605845, -4.390797991304816 55.32394721724695, -4.390550250967665 55.324545304043504, -4.394443199267243 55.32505327666051, -4.390223804032012 55.327303460310176, -4.390879452757241 55.328309700347205, -4.394621463389649 55.327485647410356, -4.395304147844222 55.32651948364111, -4.396837044989513 55.32605795970272, -4.396697574205872 55.32644428888142, -4.398631434831596 55.32735309527098, -4.40241588302562 55.32833750554178, -4.404890288564663 55.32829579011807, -4.408953682447437 55.32620603001616, -4.404733453012065 55.32446455516381, -4.405844227757073 55.32354382825575, -4.403933011061475 55.32296126611563, -4.403254833052582 55.322174642768466, -4.399883265391922 55.321683671669454, -4.398838134924871 55.321866000998625), (-4.401140628728228 55.309632385545896, -4.398238287453396 55.3054109234891, -4.397599447200887 55.30631333052064, -4.396179686099643 55.3065479409381, -4.395113482013515 55.30506779056706, -4.392390769488583 55.30512117937786, -4.39421233701465 55.306586540606986, -4.394128956670191 55.30852117181294, -4.396633158332135 55.30857942075127, -4.396477325739155 55.30913077609738, -4.397140021750132 55.309144730976456, -4.397096383375156 55.31129383038812, -4.393334158364593 55.31241030213215, -4.39433352731627 55.3151321848708, -4.397656614036102 55.31665792792049, -4.400278589180337 55.31675022556541, -4.402598391208723 55.3160613734674, -4.403688410501591 55.31653938832078, -4.4047351573813 55.316163384699465, -4.403464692117218 55.31506939570263, -4.401245400201186 55.31501441815884, -4.400934359929239 55.31456212340062, -4.401140628728228 55.309632385545896))
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>