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/S01003959 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01003959 geosparql: asWKT POLYGON ((-4.413208760794831 57.93519233300811, -4.416016296756384 57.93725052752899, -4.420279040214782 57.94284813584374, -4.421679698395124 57.942821443753054, -4.4215863704259 57.94319162214641, -4.424181278435321 57.94408559762735, -4.440792990856604 57.94684054724839, -4.444975550594753 57.94820677863506, -4.451797448189479 57.9528286176027, -4.454674905810296 57.95387826985164, -4.460707965904737 57.95484882131015, -4.463257529979744 57.95480840649326, -4.467363167158358 57.956750482771746, -4.477842064314741 57.95550425103087, -4.485558201612066 57.95562311341713, -4.49069571689749 57.95682541739331, -4.496506479205004 57.95700790840558, -4.499703900069397 57.95631605914032, -4.505677725377047 57.95592882816224, -4.508278739848651 57.95686592808469, -4.516228308435109 57.956690779919576, -4.517468033387527 57.95653143837723, -4.517258444579736 57.95619413997844, -4.513071940337863 57.95579178421487, -4.518808691926307 57.95564224746746, -4.527970905277265 57.95732930960221, -4.532034526005694 57.956942886116565, -4.534549531919807 57.9573689875317, -4.536689261295092 57.958224856435, -4.542818635464883 57.96218185937761, -4.552358038852252 57.96361472447008, -4.544531365024197 57.96010781940743, -4.541904585614459 57.95786903509475, -4.534143932537535 57.95617299390474, -4.530643011734345 57.9556676847345, -4.527178360405086 57.95591635699713, -4.51986219691813 57.95451612592857, -4.514780977481006 57.95509303594267, -4.507908197227583 57.95446509611666, -4.493952712507006 57.955773119826006, -4.484229575162874 57.954498946900664, -4.479966499186521 57.95444744036941, -4.467897099502301 57.9554192437445, -4.453234091325205 57.95233364564636, -4.448639343598566 57.94889998373569, -4.444539671223281 57.946768512010564, -4.435246615570939 57.944296284314525, -4.424682287325615 57.942746201958414, -4.421898150740568 57.941550339754784, -4.419576673072744 57.93844071129427, -4.41515987405493 57.93531699075727, -4.415171676837612 57.9312194396993, -4.408767627379874 57.92690228417122, -4.403121053627469 57.924798855461574, -4.401384494223833 57.922567400355, -4.401760662775149 57.921625811492326, -4.397935995554213 57.91415939064373, -4.396521242568468 57.91319771185627, -4.393382845282838 57.912717788082524, -4.396307674672262 57.910245566751485, -4.396129273518231 57.90934141260296, -4.391496560012276 57.90720039432528, -4.390813897251277 57.90607211455798, -4.379477506219208 57.89953712598408, -4.376989988245603 57.899691548343796, -4.377286442147639 57.90060249830845, -4.376564661979825 57.901173101361245, -4.37455433275924 57.90139940731555, -4.371715353695096 57.90288113636768, -4.369296000538854 57.901255050466204, -4.367995093856703 57.90123439416285, -4.365913912700308 57.90218070958767, -4.363740650037805 57.9017270014913, -4.362855722096241 57.902129838793506, -4.362233097700247 57.901386666929284, -4.361791519657259 57.90160154427913, -4.361002343033735 57.901148987633505, -4.35903836786451 57.90131129155163, -4.359687704310594 57.90066127158843, -4.357398380571277 57.90023657234627, -4.35638215407745 57.89915025194338, -4.354299930415053 57.898793536704844, -4.353995856434676 57.89775688581195, -4.35274549232726 57.89747456850916, -4.353372999939793 57.89623196112166, -4.357802350559267 57.89403823316313, -4.363413733299479 57.893960595774715, -4.363379877464405 57.89343986065061, -4.357777670738042 57.892115851814054, -4.355209997603498 57.892585826288425, -4.352345279704133 57.8949661207039, -4.350745197201139 57.894753162522925, -4.349454542689202 57.89385157885644, -4.351071867836896 57.8938126488167, -4.354491623441161 57.89243741534412, -4.351243915841879 57.89283007018612, -4.348704944152752 57.89167305327933, -4.348785697069314 57.889290474745444, -4.349355126429844 57.88922602311246, -4.348895328015459 57.88890208294405, -4.350552600995492 57.887927953337524, -4.349150952771271 57.88764840147162, -4.348261484091462 57.88668547437211, -4.357697385581345 57.886510544855966, -4.359569815519772 57.885478383779805, -4.362124747423684 57.88220327828779, -4.357769056535797 57.878501823995094, -4.35965921815269 57.87822124362034, -4.359724252718298 57.87921384853833, -4.360608373545667 57.87891495201587, -4.361769688900129 57.880181083097085, -4.364135427105855 57.879523521135205, -4.363154764768776 57.87835499931871, -4.367058640350269 57.877518532462545, -4.367681880470945 57.876241095047234, -4.370664605531736 57.875604359209284, -4.372864578964332 57.874032845244685, -4.377280937513082 57.87369159118995, -4.384186872107202 57.87483794388815, -4.390291074244613 57.87457035713922, -4.402654418454139 57.86852331141671, -4.411823489598814 57.86191578183576, -4.416333465435692 57.86003287264642, -4.420236104065237 57.856894432742244, -4.440666331242904 57.84867659991905, -4.452388546770622 57.85203573380691, -4.503073295638674 57.847217726628635, -4.501938105260398 57.840815330381574, -4.528601563065374 57.84371548391282, -4.526764906991725 57.84115877009399, -4.539692750074339 57.8366681919534, -4.544944932559208 57.82466560376599, -4.550755002930003 57.81631775489801, -4.567024550562356 57.81494695418987, -4.577925534092711 57.80891178553004, -4.595444347188322 57.80694573746578, -4.61968048347832 57.803158167252114, -4.630944517074896 57.7978835797752, -4.65041280533547 57.79461126195946, -4.667019405909808 57.79000329788712, -4.67585170212807 57.78997882333455, -4.682317962938114 57.794802547057145, -4.686342124570378 57.801618804577146, -4.708410444871727 57.80881445349113, -4.743008948228793 57.80793552490841, -4.74948283020835 57.81623359072107, -4.758638814058643 57.81396773284218, -4.776555347906292 57.81769236420675, -4.81938332662764 57.80974623658345, -4.837891264330712 57.81065419039237, -4.872029994921605 57.81732628877985, -4.88096476065994 57.819549987292646, -4.892100826678955 57.82610851058905, -4.886689826708223 57.83343041457334, -4.934942088308547 57.84851729732075, -4.948561923688157 57.858617443988365, -4.924274061046149 57.861004157763276, -4.919624187715849 57.87270456901167, -4.899704331901344 57.88066557783171, -4.907792326595783 57.88492902513624, -4.927345507886291 57.89029403625824, -4.92182898073275 57.89322513028618, -4.871510845619722 57.906856405667305, -4.878615419659256 57.915044275326885, -4.889982722370054 57.913815428280074, -4.902912009602495 57.91897549589562, -4.860165046942734 57.94399429879819, -4.894020114657165 57.947397322212524, -4.912771310048979 57.945189291846404, -4.91584698533254 57.948515785995454, -4.914131740875815 57.951332114741355, -4.917784011482521 57.95176053022135, -4.919214198532627 57.95396542240599, -4.922528852369866 57.95564166821094, -4.92753235270106 57.959157196912614, -4.918336883816217 57.962720891513584, -4.918208266069565 57.96612989313485, -4.920427878544693 57.96701353692058, -4.905746208197028 57.97408131855899, -4.906475667708231 57.97703030750776, -4.939915439431344 57.97636895096703, -4.950454444619353 57.97925218489956, -4.93888952816947 57.993575802130785, -4.945205864284827 58.0065955607113, -4.947740550849659 58.020116162828664, -4.94493719483712 58.02206837347942, -4.947430894383209 58.033389566701864, -4.937783420090065 58.045159491483524, -4.931247498751857 58.057595320965696, -4.919705741113821 58.059514228892894, -4.918506443559655 58.06310052329775, -4.909739215396447 58.06205210326895, -4.910025182827152 58.06598178107838, -4.911548230822015 58.069065373371934, -4.902594910356411 58.07430245092213, -4.898663831170264 58.081671291858896, -4.890661688254736 58.08593320293546, -4.891498001171499 58.08934713917686, -4.901703556000681 58.098622945079555, -4.902002248642677 58.10354985417012, -4.899073163191559 58.105108358754286, -4.896764078499951 58.11598089592446, -4.898761459017341 58.1182001079313, -4.895538766266535 58.12766449081696, -4.882938076775178 58.127923260346456, -4.88173012822523 58.136308157269134, -4.890222449874615 58.146324668527924, -4.885892047685054 58.149307499795924, -4.881837029043451 58.149399245465816, -4.888750998828665 58.15396071875124, -4.894572304239872 58.163255657999656, -4.900060046552536 58.169044172593125, -4.898922957965401 58.17220636890582, -4.906072600357861 58.17540464099613, -4.901918289889459 58.17702697806762, -4.893050349996086 58.17445160444913, -4.883165262191913 58.176311044149166, -4.883742541222211 58.181546160073246, -4.88006366434391 58.18086543608477, -4.882032451532765 58.18332821555815, -4.879262809719843 58.184424225849575, -4.866155603303986 58.17695509134877, -4.847412012173217 58.175919172089266, -4.84423800648941 58.17919816325613, -4.84900213315391 58.18282120739833, -4.844450809981804 58.18441454580368, -4.843472338872878 58.18877681371886, -4.848528644411067 58.192420335771075, -4.838981288300246 58.19860015767147, -4.839675679529181 58.204524718415286, -4.834565790581025 58.21644645696574, -4.821025781855215 58.22028709854555, -4.826956882554103 58.21498870618618, -4.828360237085838 58.2063037449626, -4.819997041500078 58.20174407008992, -4.828798558998664 58.20068653191418, -4.831091765853332 58.19656483046147, -4.81452968600678 58.19245611095376, -4.815951526659593 58.18612530358738, -4.826672249151249 58.18400102654804, -4.826792071905524 58.181868595430295, -4.807813252764405 58.17592551363167, -4.809590386168099 58.17365776173745, -4.804779947984684 58.16851564248035, -4.785277375180875 58.16117001745593, -4.77858393247694 58.16407484212136, -4.759043501477601 58.15284409594382, -4.757487134437461 58.14942707573199, -4.756066455494269 58.15061701686014, -4.749235169712052 58.15063872281022, -4.741585815192462 58.1468765274291, -4.732139500000922 58.13960290673808, -4.724654111413093 58.1388196047921, -4.722810400840227 58.13603737759843, -4.716499865304455 58.133044894228426, -4.71619374375938 58.130364581599, -4.703713568931811 58.1228211209106, -4.704426031327145 58.12101777838839, -4.702655573353551 58.11939292841587, -4.693061817668392 58.118508767852106, -4.689432078275807 58.115638031878404, -4.682107338341997 58.11126351247106, -4.680057278605271 58.10906016032207, -4.676830058616324 58.107681275897505, -4.676607327487173 58.10357936806758, -4.67521400709159 58.10174854357105, -4.673230603942319 58.102688784006475, -4.665982098104659 58.104044821289875, -4.659451281092495 58.103192889765566, -4.653615102495925 58.10163424234909, -4.648094925111291 58.09837942500192, -4.64860318799758 58.091503633021894, -4.643387595831326 58.0902731006115, -4.640077505666898 58.088418792910694, -4.636608610705466 58.08532763857696, -4.633765927243012 58.0794468124784, -4.635442637623955 58.07711173590734, -4.628294535471422 58.07124796090707, -4.615896074400356 58.068214520332255, -4.617090856927052 58.06577277537946, -4.61429270201702 58.064608201121004, -4.612830386150484 58.0624636592074, -4.608526091218224 58.05993706836493, -4.601944348965122 58.05708850486654, -4.599056294290825 58.05653647696441, -4.591429415109213 58.05772535657954, -4.586874877509818 58.05686551814548, -4.583603519994051 58.04842247262888, -4.581762855032943 58.04663572589115, -4.577757079149007 58.04490181624951, -4.5752416349197 58.04032509876119, -4.575300438222208 58.036433091171304, -4.577870028160932 58.03353257521214, -4.583660202513411 58.03230987759183, -4.585846038292103 58.03044136086617, -4.586343301242949 58.02700767291011, -4.57503993720457 58.02103681666237, -4.574891188691677 58.01849685969191, -4.570692797752401 58.01638926455679, -4.563625608378929 58.01393503336188, -4.558831517679498 58.01058106509366, -4.553521264560442 58.010660836025764, -4.552285044859217 58.00922997083429, -4.543630086178001 58.00559345021668, -4.543542782769777 58.00484040337065, -4.539920023162701 58.003879459476366, -4.53653766071885 58.005833959059665, -4.53298921329645 58.00664150763853, -4.53063980200353 58.005493191313334, -4.520909399951535 58.007456614061724, -4.516944503977535 58.01053633008552, -4.509309061315963 58.014883458561506, -4.506379925632936 58.01343164167396, -4.501617030403556 58.01383990169937, -4.495899637802645 58.01440150304075, -4.495140421562347 58.01614161608516, -4.492616970534817 58.01442993325487, -4.490949056828636 58.015522898102, -4.483647056410711 58.01277236825439, -4.481399609967633 58.01283420497184, -4.475842545915715 58.01032773016715, -4.462068093133578 58.00647082223447, -4.460305538853991 58.00545363718439, -4.457054165884898 58.0019762477579, -4.450992535866484 57.99921789798703, -4.453052754653902 57.997668623875335, -4.451431285013196 57.996972077867625, -4.450550914135476 57.992262727754806, -4.449280895069971 57.98977128793703, -4.432879512354602 57.98668074304188, -4.449178026462003 57.97216186836999, -4.431544228801502 57.96598604407749, -4.428183104502017 57.96428917190723, -4.426262748996968 57.962142407527516, -4.417358996996895 57.96072168287778, -4.407104350825374 57.9615812483229, -4.406185115673846 57.95946913782787, -4.404590432643642 57.95862774836179, -4.407770467840985 57.958405799272704, -4.408621336360759 57.957482156437486, -4.407907524211234 57.956920624274915, -4.407999939713654 57.95552614966811, -4.405855442694913 57.955306195737, -4.406802959516964 57.953050908393585, -4.402424867533655 57.949000522901315, -4.40142491876706 57.94870493815336, -4.403276911944986 57.94556998675403, -4.402569894880759 57.941315335154925, -4.406413750819793 57.94118866807197, -4.407148340621973 57.94156111843654, -4.40760547617493 57.94359212892111, -4.40918632162058 57.94423605854878, -4.410869792872854 57.942631688272726, -4.409420044001396 57.93915491232382, -4.412367552414734 57.93700538235202, -4.413208760794831 57.93519233300811))
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>