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/S12000041 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S12000041 geosparql: asWKT POLYGON ((-2.44493817707604 56.75170913797984, -2.4396234576361877 56.7507146386844, -2.4312224487368606 56.75105662232651, -2.4431296506285447 56.72533582458907, -2.4477995064152784 56.7116979829645, -2.4546827306539285 56.703409786470424, -2.4656714598055838 56.70387856295056, -2.47591384430141 56.707818757098764, -2.472328314386892 56.71404557512125, -2.4743676662842833 56.71993116774636, -2.5159216667664923 56.72349427081411, -2.531394273128808 56.70542752188142, -2.5160849514057153 56.69856179564659, -2.4838487086186536 56.69942967839787, -2.4796871180067 56.70437427807981, -2.4627126423778534 56.70165712462508, -2.4372508498687626 56.702314202912426, -2.4425597228770073 56.697139668747454, -2.4394451028057555 56.691895194370254, -2.4513345836608114 56.67870848670515, -2.469394946321658 56.672786053993676, -2.483910115924698 56.67139188593426, -2.4842400133821063 56.668616397489224, -2.500077461015063 56.662510350349365, -2.5062543845208443 56.64867988408996, -2.5049403058848463 56.63382319214907, -2.5007137497237033 56.62954866944949, -2.482860244292029 56.62874152038744, -2.4800898259189057 56.625826191084506, -2.4883842836040726 56.61412561338562, -2.500956807127662 56.604965912723664, -2.50437608365906 56.599117026128255, -2.5133567798257714 56.595898161500294, -2.5196235150197506 56.59022032447967, -2.524427538061156 56.579334939926476, -2.534195506001472 56.578031099908394, -2.5403569918191256 56.57281531562427, -2.5381856302983374 56.56672729441904, -2.554647602140342 56.56031849595781, -2.567742911625085 56.56051271438046, -2.586955719070158 56.55281487266871, -2.6019817014794255 56.551611002019065, -2.6120412772265236 56.54761426240539, -2.6274398105655834 56.53646366822363, -2.636001199143112 56.527190754467505, -2.653490468123145 56.521506459160705, -2.666009389568088 56.515610148939125, -2.673510881033829 56.50966080983218, -2.707222002751529 56.49948747627238, -2.715072884258087 56.49431701631141, -2.7219455965829145 56.47743268615924, -2.734243695591288 56.46425249709908, -2.7444225356024265 56.46458357900832, -2.7704399521368543 56.47333544312985, -2.794805701327506 56.480482481577965, -2.8237287603318997 56.47779090803081, -2.8386528744016664 56.47408017241125, -2.8457119187781297 56.47790993287369, -2.8356419396708366 56.488720703680436, -2.8394013604220305 56.49202673740944, -2.8606034019186226 56.492284099142765, -2.862668107214886 56.49609530922315, -2.8811881242703388 56.49399140399363, -2.8890611141354583 56.49595676960881, -2.8965035754975714 56.49320345484251, -2.908158107093747 56.497804126424086, -2.9183775326948758 56.49655021694609, -2.9224778212395 56.502592382665995, -2.9589587859035467 56.49884176743368, -2.9680852787172447 56.49535596728013, -2.980879790772443 56.49429153002483, -3.0097767999195453 56.5016994241258, -3.0317225824400276 56.502466913287414, -3.0317731641185772 56.500321889760606, -3.0520398727292086 56.496842851539675, -3.0511612289863272 56.48258012931737, -3.073584822244641 56.48452478972421, -3.0845002792802125 56.48094210433226, -3.0980245869191205 56.479279672421654, -3.089405734957216 56.46695772681637, -3.119625640888942 56.46766455532045, -3.123473566735436 56.4741867936785, -3.1301141623629753 56.47918757817168, -3.143626667751656 56.47975386322198, -3.142041106273067 56.48756085961313, -3.157367683865327 56.49037130801409, -3.177592384599091 56.496706734508166, -3.1915656307472555 56.50329669711667, -3.191420701304223 56.51152087995018, -3.2072172944870823 56.518416853766865, -3.2120647434428893 56.52429506116395, -3.200855596635297 56.52590974919726, -3.1839100108500054 56.5234578522106, -3.173022587845705 56.52620171873881, -3.1736211765582922 56.53099655995775, -3.164043369796819 56.53815037256515, -3.17548644214054 56.5432896706153, -3.1839390977434467 56.55372234003141, -3.190322018646762 56.55634238623822, -3.190200442723407 56.56127880298973, -3.1835803199838195 56.5650005469333, -3.163987463138673 56.569549360903835, -3.1530684762655925 56.574356328365404, -3.132663256934058 56.57907569668755, -3.1230068299773643 56.584023907152066, -3.11214085745171 56.593889496970746, -3.097979480883124 56.59833326100609, -3.105282494014412 56.60736263920189, -3.088152272658593 56.613188236764984, -3.0912177107714056 56.61693753704499, -3.110649162770252 56.61220417515986, -3.1444576824023684 56.595570195140525, -3.1659460634896384 56.60108655653946, -3.1594195160258165 56.606250774724856, -3.149885912985634 56.60811640816086, -3.15304988467245 56.61389470134453, -3.1609121588081743 56.61921767146405, -3.180959393555141 56.61575951890446, -3.191257614297361 56.621963986109364, -3.175626291765784 56.62425727491536, -3.178134696066548 56.63560883224925, -3.1565942060596965 56.637083101493744, -3.1486305969339914 56.64381613942244, -3.151453912114615 56.653198563937124, -3.157315542254088 56.65621598477803, -3.169756409509249 56.655779850800336, -3.1757247959892334 56.652001355282316, -3.187254452797548 56.65448573652476, -3.212748829593969 56.65538189214666, -3.2237055623129014 56.65835096445731, -3.2393390477118076 56.659855746835156, -3.2669145293289463 56.67064625863851, -3.2718468478984697 56.66616657516124, -3.2838999517777014 56.665539721532205, -3.2867654020546415 56.66822797951861, -3.283864740209211 56.67779236989219, -3.3085279690311293 56.68995955072812, -3.302022815530656 56.69496026661372, -3.3068068973445017 56.70272225986758, -3.3152832788781548 56.70996896175509, -3.3279055166344653 56.70912078397864, -3.338538076851586 56.71941566499879, -3.3527837518446595 56.726161997380494, -3.349109962961176 56.72944976139534, -3.3639463584823917 56.738129123382784, -3.3578744730548364 56.74315492439644, -3.357841006895114 56.74766555552726, -3.368711494010924 56.75295539925758, -3.377839683090309 56.75324118050039, -3.383109027388979 56.757773968896956, -3.3897975280549217 56.769244819966396, -3.3795083303317957 56.777112777833445, -3.3800093101155113 56.78139737266959, -3.3741186169200965 56.789270002251904, -3.357641791654952 56.8044456047136, -3.366731198298261 56.81344768167406, -3.3516367326514422 56.820287578766376, -3.3585403597002963 56.82565721319942, -3.3702434768712806 56.83113110286634, -3.376890076245378 56.829818093512316, -3.381549167958999 56.836401667917784, -3.3928014329890486 56.84257476477796, -3.4030891701726946 56.84565804830653, -3.3978307793907994 56.85105383967048, -3.390899647694965 56.85140976928066, -3.378518594468836 56.860971801436364, -3.3796891094283668 56.86692455870318, -3.3721381539958473 56.87462656373083, -3.367318919906688 56.881718715515234, -3.3536387043797897 56.88393862230019, -3.348678902508734 56.88976474060641, -3.3370616159110185 56.89585074476102, -3.3216175631741915 56.89550745692152, -3.3021684639928486 56.89988938459667, -3.297872611552407 56.90479009197646, -3.284854317639725 56.90727851065448, -3.2833644802621946 56.912675392920306, -3.2910981679790114 56.92545670034478, -3.2842470565245283 56.92856844443441, -3.271940176781803 56.92350139824781, -3.256332290968011 56.919822997741235, -3.235121016168842 56.918219608254574, -3.224257358843461 56.91378050577591, -3.203522713134012 56.90897324384512, -3.199449485036519 56.90614197120268, -3.186420710762272 56.904114652839006, -3.160446243342054 56.906833697106784, -3.147611517781634 56.90303965968587, -3.1391308673249654 56.897650448356764, -3.1390489959487677 56.89361284730072, -3.1306337664289456 56.88837520200355, -3.1203370155883015 56.888458985428485, -3.1067530178880847 56.89504858870236, -3.1123557571839586 56.903219236605345, -3.1088412635692064 56.90790420498577, -3.1107725488510733 56.91874425080149, -3.094613338513473 56.93235831486719, -3.098651159306714 56.94533618507822, -3.0938869128629545 56.950202343100514, -3.083634695670064 56.950709363033035, -3.083168579057247 56.95844407939003, -3.0669328287528557 56.96054663127973, -3.058376594813479 56.964141020332654, -3.0480372877709883 56.963455472604906, -3.035595708404702 56.96700442254456, -3.026338509813849 56.97278547117289, -3.0072683267643527 56.966826992901815, -2.9986799127456045 56.97071398620235, -2.9941194300334115 56.968091105648014, -2.9674716802968146 56.96959372807423, -2.954077274771381 56.96681282275415, -2.947135624371984 56.96839731328851, -2.944673217220894 56.97378373079275, -2.9240750833110156 56.97236094721817, -2.9102425787941084 56.97579982200757, -2.912957031845146 56.98164820314079, -2.8924263475231324 56.98681642734828, -2.8839154421952173 56.98564159033656, -2.8745682397945505 56.98035750383016, -2.861380005738345 56.97888584944293, -2.8510849322152936 56.97257791701175, -2.8336969986215608 56.97291511660608, -2.8245059884194905 56.96194861273022, -2.8173241424697912 56.962157460005294, -2.802620545932933 56.9582504779835, -2.78322871698158 56.956823758783756, -2.765666893957094 56.95997347011496, -2.752673308940865 56.953714420477695, -2.746216779443083 56.9486100059024, -2.7400171069876444 56.94849862296172, -2.72468387674568 56.925841950869334, -2.7188594117785265 56.923256861093876, -2.7103067592766066 56.924446678451474, -2.6867490706187276 56.91507818432277, -2.679949400228191 56.908750483187674, -2.6787321132257778 56.90320522928331, -2.6823949589906633 56.898334122117355, -2.6811462899018066 56.88836012166555, -2.6693020945487262 56.89050385523372, -2.6611455170068754 56.885440100888175, -2.6630164393613747 56.8687655295978, -2.665966034498867 56.859366399272886, -2.6717301085472482 56.85870396966967, -2.6677652188407706 56.850264410869485, -2.671261706189971 56.846379978304554, -2.6661665549121367 56.834668277145674, -2.6509647356533246 56.82260791573097, -2.6498216562932764 56.81544666596473, -2.631604473901148 56.809088110826536, -2.626202462516346 56.79903291360043, -2.6171769221911476 56.79301747525705, -2.6158128177779014 56.78330471445741, -2.6053795315651636 56.78250863150342, -2.5915059273463807 56.78832119783195, -2.5706751675969897 56.78550663523382, -2.558559550154514 56.78700966104703, -2.545361935151147 56.78181081386426, -2.52972414611203 56.78514648951187, -2.521416324049751 56.78173356532593, -2.515796420241431 56.77577470093809, -2.5026925334914667 56.77011549329796, -2.483811599685834 56.76508566350797, -2.477953532391227 56.761598333983834, -2.472069971327137 56.75338719868056, -2.462623558026753 56.747736836369775, -2.44493817707604 56.75170913797984))
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>