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/S14000004 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S14000004 geosparql: asWKT POLYGON ((-2.6829622931667987 56.50738830578107, -2.6920532103811308 56.514547790237245, -2.7087046166809805 56.51826678722464, -2.705603886368665 56.52212248291323, -2.709430216721673 56.52830696071211, -2.718293403433798 56.530378993444636, -2.71652548119027 56.535503892338845, -2.7278408360793143 56.54035426287882, -2.7557583969050667 56.54360248954978, -2.7777618713643846 56.55040342184771, -2.7885995974699225 56.55179775247856, -2.806579046726506 56.56548301211086, -2.8182205427336173 56.565264978944505, -2.8351294964783142 56.573769294704874, -2.8504271331837843 56.55374662915442, -2.8797811491793466 56.55021093375719, -2.890789918038728 56.54529177225148, -2.912129962251685 56.54827160222168, -2.9433136849389263 56.554545281882945, -2.9373596372129676 56.559349331093145, -2.9301928320231116 56.57267929374293, -2.9592220003425678 56.57877289295216, -2.9813729164885276 56.55618583223233, -2.9905421673672588 56.55575641596027, -3.0146860935769255 56.54784223846754, -3.0189409693657288 56.54686945074546, -3.0367923592851223 56.54552687076013, -3.0432495472787915 56.5438807841371, -3.0473128529760998 56.55762517550346, -3.050329460813971 56.55916032050481, -3.0653481534050617 56.55938020265613, -3.070232632603399 56.55857815569389, -3.0746487972950103 56.5584807551876, -3.077196507524618 56.556872041872786, -3.0768609424270683 56.55605556442103, -3.07971061121748 56.55420519362031, -3.086348488022186 56.55024807773931, -3.0909013379897488 56.54911298560657, -3.092531822290785 56.54828825947761, -3.0937526072638475 56.54696488330376, -3.1011577929773404 56.54146564409608, -3.100842259173561 56.541240232544126, -3.1032431907514324 56.53745801073373, -3.113372651179596 56.53714214916408, -3.1151133786320155 56.535399707193406, -3.116714640768778 56.53500078705061, -3.1336432444853153 56.54012682856553, -3.1476139810235604 56.54372360002238, -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.110771249980719 56.612200389740586, -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.1512410846783734 56.65306576591333, -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.28436954653008 56.66567413911345, -3.28815251578602 56.679562074646135, -3.2993948476892956 56.68361771553956, -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.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.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.1690549202375284 56.90774619919323, -3.148502957294485 56.903462705414576, -3.1392736570951225 56.897756955437025, -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.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.76504645731743 56.95978861152901, -2.7413745317340834 56.94938804325277, -2.72468387674568 56.925841950869334, -2.7103067592766066 56.924446678451474, -2.6867490706187276 56.91507818432277, -2.679949400228191 56.908750483187674, -2.6823949589906633 56.898334122117355, -2.680959071057347 56.887992813223896, -2.6693020945487262 56.89050385523372, -2.661142468913613 56.885732084921685, -2.6630164393613747 56.8687655295978, -2.665966034498867 56.859366399272886, -2.6717301085472482 56.85870396966967, -2.6677652188407706 56.850264410869485, -2.671261706189971 56.846379978304554, -2.6658220715254632 56.83428112093648, -2.6509647356533246 56.82260791573097, -2.6498216562932764 56.81544666596473, -2.631604473901148 56.809088110826536, -2.626202462516346 56.79903291360043, -2.6171769221911476 56.79301747525705, -2.6162344440109786 56.78346613586396, -2.6053795315651636 56.78250863150342, -2.591015261670045 56.78834598329304, -2.5706751675969897 56.78550663523382, -2.558559550154514 56.78700966104703, -2.545361935151147 56.78181081386426, -2.5299787328398966 56.785210090352045, -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, -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.48356926098779 56.671660928436694, -2.4842400133821063 56.668616397489224, -2.4999880360233724 56.66265624789593, -2.505276775322422 56.651913557155545, -2.506946689817287 56.641696595408206, -2.5006222981800215 56.6295355616508, -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.601489770743193 56.55166998363467, -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.6829622931667987 56.50738830578107))
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>