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/S01012422 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01012422 geosparql: asWKT POLYGON ((-4.723302082791414 55.36192052865402, -4.723186029903288 55.36023142753546, -4.724327366797229 55.36030657496179, -4.724321965766227 55.35885044260877, -4.727132925927563 55.35865287558474, -4.736933964006575 55.359053459312705, -4.736895748184853 55.358797079281096, -4.740448797122799 55.35816606597532, -4.740629514871866 55.35712824620032, -4.739124038599593 55.355436079907804, -4.736939893159979 55.35442431134291, -4.734264415383852 55.35337855924557, -4.731529746496012 55.35192955793838, -4.727866587309608 55.3482179735508, -4.727111169163247 55.347785382778895, -4.724650767716829 55.34917070542845, -4.722668108233469 55.34993405976413, -4.71780689215955 55.35013222743245, -4.7138455157761685 55.34987872752836, -4.710760984943691 55.349066280087506, -4.70856488446782 55.34994201878047, -4.705723273123742 55.35060728076472, -4.705282076875514 55.34876528401012, -4.704299063296554 55.349155611015206, -4.702188628214672 55.347817991828286, -4.700734184614131 55.34707709582281, -4.699384133331541 55.34763730441346, -4.69833818149569 55.347103090699335, -4.69680947171503 55.34666043206949, -4.697148547453908 55.34631135602237, -4.69675906861335 55.34522327976441, -4.69830540685201 55.344766632660665, -4.701338000348185 55.34482543800244, -4.703737962564314 55.34370261520079, -4.705829610678487 55.34361134241311, -4.707060254088423 55.342460411163884, -4.7077834254200175 55.34080835414214, -4.710973646952103 55.33994652282889, -4.711349771980032 55.33852686708989, -4.706469160823278 55.33610917438222, -4.690475604987491 55.33584236305648, -4.6920502671788915 55.343709123127546, -4.691025747878943 55.34325527866115, -4.6896070554645695 55.34326855415001, -4.686965053176144 55.34222104965307, -4.682129162249781 55.34068234698781, -4.680504586344924 55.34044834404454, -4.67909987745532 55.339733063943925, -4.676824833933514 55.33757164175833, -4.67516298853958 55.334677608820776, -4.67482925973075 55.33510740812591, -4.67333511502572 55.33540080394724, -4.658358544173216 55.33312094422898, -4.658050685905275 55.332291662380705, -4.6593371913485315 55.32913545075271, -4.662085454880872 55.32685525743111, -4.664101834944137 55.32682925529387, -4.66360764416814 55.32533885854118, -4.664319180742806 55.323255840665674, -4.666197109909241 55.321875477070684, -4.670104327706292 55.32019900412221, -4.671830047091893 55.318201619659696, -4.672996262642388 55.31609061254219, -4.67493595348148 55.31399860260463, -4.674600664712218 55.31346660084493, -4.673406994467874 55.313294987078265, -4.673985884197753 55.31089118824149, -4.674468974229068 55.31010754779796, -4.675531227010324 55.30973369290578, -4.6745828755876415 55.308756680777826, -4.675644570833024 55.307205267295146, -4.67747592397542 55.308189864344556, -4.679807787439513 55.30631389357173, -4.684011723081667 55.30369556794833, -4.684423172515185 55.303632458653205, -4.686308504476486 55.30243663391027, -4.689737221709736 55.29982103798609, -4.693570016807458 55.29848701145282, -4.696137258486858 55.298241552528495, -4.699474819749652 55.29704412442249, -4.701010325420385 55.29576064896897, -4.70223789164253 55.29365696225552, -4.705956954586088 55.2920553747079, -4.710938245235093 55.291180734317955, -4.71027731220594 55.290035800571275, -4.713529187876838 55.288291531408255, -4.713123133057551 55.28741961172217, -4.713704630493438 55.286939239586005, -4.713026121964559 55.286459916819396, -4.7131691047659885 55.28463191585064, -4.715325182958807 55.283685029324545, -4.720753519335476 55.28176625483342, -4.721420824916158 55.28139181071612, -4.720450064433055 55.28078419839543, -4.7209864045577525 55.27895640108518, -4.719378547672944 55.2780124132713, -4.722538094603793 55.277699242489874, -4.723634949448517 55.278079281606395, -4.723930535250271 55.278486191177336, -4.725462105298662 55.27798006872097, -4.729217142342286 55.276354445913064, -4.729552880583369 55.27642783930885, -4.732903918220391 55.274779691917544, -4.735436051849778 55.27450720280871, -4.737339584634743 55.2740240319828, -4.738900006540966 55.273818214441015, -4.740540409525503 55.27294536778729, -4.742481666117227 55.272326427157466, -4.745693626867238 55.273009290734635, -4.747502398408623 55.272752816850335, -4.75213454741564 55.2762082188357, -4.753965697795332 55.27718270079672, -4.754933314786098 55.27751144242652, -4.756563842309882 55.27695323008581, -4.767942310074567 55.274367362212196, -4.770213696094743 55.276005822989106, -4.766080192470582 55.27688447196967, -4.766499968081095 55.27898469044678, -4.7688628937736 55.280351401588476, -4.773081341853756 55.28393228632817, -4.777693134981081 55.28727034487066, -4.791095173529854 55.28790842606702, -4.792884536437 55.28961144330257, -4.7891195070279675 55.29028187208807, -4.7912186444867135 55.29347907441756, -4.783047667008212 55.29715359570308, -4.781280807118477 55.297571431391084, -4.781868890788038 55.30034475329496, -4.7819448723449804 55.3023296864853, -4.781458285175677 55.30393190605241, -4.774020024900121 55.30753514557151, -4.770172627009963 55.30931253155506, -4.7700665285567725 55.31027680452134, -4.769038260824348 55.31135189416656, -4.763949761182886 55.31161105121276, -4.759128649628276 55.31390453643164, -4.755557904430506 55.30923881570555, -4.74666935556801 55.31129095399491, -4.747229492184838 55.30936362099424, -4.746452709292396 55.30815855230815, -4.74311955893308 55.30507825205307, -4.736696661855379 55.30700231639563, -4.735532483832577 55.30840377649688, -4.735320340886676 55.30944230041458, -4.732189818004053 55.30883821531036, -4.731732268760808 55.309288931369636, -4.737680770793692 55.311447933686395, -4.7360912404989035 55.31468376363302, -4.741564206331062 55.3186151491628, -4.741098605779303 55.32123250291645, -4.747144333509159 55.32088985392476, -4.7509456707632545 55.31955473739139, -4.756808854154337 55.31953933529965, -4.757367514871452 55.318277198604726, -4.758280808646485 55.31801351220853, -4.762189891902554 55.31894411335909, -4.764730630843155 55.32088982502699, -4.768537267498219 55.321922473516366, -4.7699086473103 55.3220718366172, -4.775706058208967 55.32173602965065, -4.778482614921157 55.32581698169359, -4.776180084163864 55.32685256719502, -4.774822745918366 55.326916019753966, -4.7750474570916746 55.328164248575426, -4.776076739230605 55.32937644066627, -4.778362700861972 55.32948870544689, -4.779215988359842 55.32916259368112, -4.781999044413131 55.33008237244338, -4.780931168036945 55.33069156849656, -4.77932667980024 55.3299316247899, -4.777450602082994 55.33040440565815, -4.778313548751888 55.3307446532502, -4.778975705175601 55.33140544235998, -4.775430576905194 55.332341878328045, -4.773948982730657 55.33088221419693, -4.77284616709346 55.3313477441372, -4.771242866576717 55.33119536830951, -4.769426547413241 55.33142487152284, -4.769224924168205 55.33106378415934, -4.76782455980134 55.3312459384557, -4.766741159641365 55.331764908251905, -4.766155818024043 55.3324164151743, -4.769644098239555 55.33246318879248, -4.769324031714837 55.33284800203567, -4.771289012930118 55.334565334841024, -4.772483937414304 55.33609336940189, -4.7726518033058465 55.337141314008754, -4.7754197510885215 55.33815715585267, -4.781590520465363 55.33754030375774, -4.784560889196131 55.33761643231755, -4.785389129248174 55.33728291274205, -4.784178557831963 55.33553960959172, -4.782636134275772 55.33558375964159, -4.780980496732272 55.33356290798744, -4.78898623561545 55.33281396056477, -4.792557637236567 55.331914012497066, -4.795335620176918 55.33372811145781, -4.793150604456786 55.33417506203079, -4.793171313564247 55.334768025351686, -4.794865677468435 55.33560118312943, -4.795641869433623 55.33629995255326, -4.7998550035025005 55.33569407454833, -4.80183342452787 55.334327836215046, -4.802525506273791 55.33469181121643, -4.803992951868779 55.33384564427282, -4.804362950370478 55.33336755530713, -4.8053504938328775 55.33319810718567, -4.802942242345062 55.33587356354275, -4.8070018959112195 55.338385044714784, -4.809848097330271 55.33726210570008, -4.8092716293816 55.336787871184015, -4.810517363616817 55.336601042827596, -4.81427979760945 55.33361734981062, -4.81268040214363 55.33301848645282, -4.811332263778468 55.33390738342389, -4.809950975447358 55.33376619568115, -4.809119843016907 55.33339217494832, -4.810258949344439 55.332636987326836, -4.809636558906203 55.33203488494679, -4.812036750243293 55.33230324060388, -4.816097197362846 55.332196107420465, -4.816385307985981 55.331544339824056, -4.817654236982868 55.33093276755095, -4.821920763554531 55.33136378380075, -4.822705934488445 55.33187617665984, -4.823902918390184 55.331994433256106, -4.825653526376554 55.332411371560255, -4.827851123641639 55.33212700738022, -4.8289082566469785 55.33279259566025, -4.82720024443369 55.33389030014521, -4.826106071285962 55.33378348537103, -4.825300884709009 55.33425788839115, -4.824064240176808 55.33345676575998, -4.821889851778403 55.333106107601246, -4.821491675766583 55.33413741642317, -4.820405469966513 55.33392069459403, -4.818770006174686 55.33474054858587, -4.816339532973265 55.33533690263778, -4.812716859675166 55.33731449212909, -4.811148860498607 55.33848956798217, -4.810530543899124 55.33935690219706, -4.809868102765646 55.3409821612703, -4.809009677648989 55.34246899181277, -4.808974817417115 55.34598557132865, -4.809392595559393 55.34662769650589, -4.809519653674447 55.34757495909168, -4.80690370873009 55.34857271359942, -4.805869394803264 55.349896356163065, -4.804388088461422 55.35034389528218, -4.803715496474693 55.350314391160666, -4.800459684717007 55.351259270605205, -4.798606372684207 55.352137770759036, -4.797267119773446 55.35189517448966, -4.7961662261103415 55.35259369208145, -4.7946995970758595 55.35267041116806, -4.792861322831894 55.35401143185803, -4.791459085306655 55.35435362399003, -4.790999031457669 55.354968225740706, -4.789162151547201 55.35494186904557, -4.787079900499119 55.355554836172644, -4.785827733775753 55.355137530446584, -4.784916707940517 55.35546574406021, -4.782916205795254 55.35737933534614, -4.781421531947085 55.35710414810911, -4.779823763064917 55.35781113014263, -4.778190662349089 55.35889285154738, -4.776017226628538 55.359110369767265, -4.775024557289409 55.35967228812299, -4.7736991503041795 55.36111731674504, -4.772295096638147 55.36310886854124, -4.771641521423408 55.36464020151918, -4.770936230451765 55.366944886799395, -4.770511931254095 55.367527635427265, -4.769086033669033 55.36739046326033, -4.768296683867388 55.36783983271525, -4.767014086910753 55.367554275050104, -4.76650390026103 55.36815911652602, -4.764883471834641 55.36778230458195, -4.7645397065913295 55.3669271108441, -4.763260903928968 55.36669535538643, -4.763207707004655 55.36593246733183, -4.761824509528735 55.36533449710936, -4.760117561956382 55.36552589695938, -4.758368115578839 55.36510696010657, -4.757187882890888 55.365610038458314, -4.755966729556019 55.365071270355834, -4.754497535713781 55.3650504734199, -4.753230813558168 55.36567231494426, -4.751517308316354 55.36531540456428, -4.749287186953012 55.36547348948601, -4.7459127308385884 55.365971886829314, -4.743534733662974 55.36581856592017, -4.742434848243424 55.365205026813115, -4.742159355828425 55.36464488839703, -4.740144116072495 55.36425862724221, -4.7392309303116615 55.36474654963415, -4.7366064850608005 55.36467952436313, -4.735197087126207 55.363695307285475, -4.732478504180158 55.36386402206914, -4.730006433885332 55.36348781339596, -4.728711246371564 55.362554906414346, -4.726352406024775 55.36152887186304, -4.723302082791414 55.36192052865402))
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>