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/S22000077 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S22000077 geosparql: asWKT MULTIPOLYGON (((-6.08304555416858 57.26770395550825, -6.1204807779023005 57.27592932728246, -6.124100732854065 57.2565215115888, -6.1409307634160974 57.26601183196754, -6.160457095090704 57.26578841441871, -6.151481125642799 57.24343348150334, -6.190063772718544 57.212788817313026, -6.168405224078388 57.19843477778208, -6.167436545091974 57.19268724667503, -6.16522246860265 57.17954344689243, -6.173543610455841 57.17450988096888, -6.209674010280277 57.17780587509321, -6.3019968492366445 57.16335654360528, -6.322263633195238 57.16017397936877, -6.286646192656114 57.18641950790677, -6.286607469365721 57.18644795920702, -6.2852958846599805 57.19747365346788, -6.284833830429777 57.20135663543247, -6.293751219847255 57.20515688779783, -6.306128659222672 57.198824853133274, -6.310455754567727 57.19661040743171, -6.329043014598894 57.18709349341789, -6.34780591401227 57.185796272905264, -6.359300458995286 57.1949210657719, -6.387551506392086 57.21732356842814, -6.387253934376074 57.21776079422794, -6.380687665515479 57.22740461576901, -6.350294889638006 57.22985495198757, -6.349211911569414 57.22994209328495, -6.4050068607035895 57.23223928329513, -6.451487702567389 57.26242537788111, -6.458567377594905 57.28626520884702, -6.481846335283313 57.29150426144808, -6.482783198190198 57.3109793019369, -6.454029746954792 57.32117305470882, -6.438754603924162 57.32658349577789, -6.426855341706882 57.323065346625604, -6.426907187778076 57.323294045881404, -6.430653887499032 57.33998472820094, -6.386361602334914 57.32644246006431, -6.40263582135493 57.33988077209188, -6.395337522360903 57.34461984040233, -6.407174657484243 57.345197934775655, -6.405687534804943 57.3537627793321, -6.4147519322313995 57.34902722400699, -6.446550882038462 57.348210833255905, -6.45780119568901 57.33919756060838, -6.454523230750282 57.35988595622956, -6.4806608500133 57.36587921608735, -6.4794686775496375 57.367001210898934, -6.467806809018568 57.377971873479495, -6.485898236347169 57.380514855492066, -6.4873045401383544 57.39210579754367, -6.488710834650194 57.40368804356486, -6.524182000993821 57.369708193610435, -6.524634878492347 57.369429346642995, -6.5237048749698525 57.372167902558644, -6.516554762468406 57.39320823512946, -6.538405279632342 57.41346543091659, -6.534068541990972 57.39319781935801, -6.534121009071193 57.39318077246114, -6.541819148735471 57.39067925588035, -6.542189595929468 57.39725525774117, -6.543762089668743 57.39539454622422, -6.5521566032378535 57.385458040976, -6.572979894446037 57.38849143251089, -6.568979974630713 57.36734370162608, -6.563555279106866 57.33861883051681, -6.582125516772236 57.333004415072274, -6.624645195870293 57.350825093930965, -6.675030512630269 57.35720358224379, -6.700496756711121 57.371756505264344, -6.719622028924726 57.371808272016004, -6.738925459564079 57.38932476942408, -6.729135845286621 57.39530682028671, -6.743122444562532 57.417619400302314, -6.767228686237534 57.42893267070415, -6.789652166339329 57.421041109551304, -6.780175907437416 57.45777760600764, -6.738456487715011 57.45228738310995, -6.719976212261553 57.44973642828909, -6.732500448061789 57.45784116560906, -6.742951867787025 57.464600757474756, -6.749534931294416 57.483647923721755, -6.748124444277528 57.50009108461034, -6.7166729462733885 57.51375572289499, -6.716478273506891 57.51358843617919, -6.666763496777782 57.470805364452794, -6.666777716270832 57.45952042126913, -6.654396876471261 57.45939083783463, -6.639678137687407 57.44273557750426, -6.6199585766034605 57.43199154830964, -6.615588366153689 57.43884614337466, -6.610210673869908 57.44727730864387, -6.598859201965196 57.44498628706773, -6.584937385590715 57.4214535311923, -6.581960590355401 57.431619780628765, -6.581868701668793 57.43193346259152, -6.579027588826092 57.43130692276899, -6.573863234799933 57.43016781222822, -6.601990894755703 57.458710776520796, -6.603912184066011 57.460658502934606, -6.614773643016467 57.46069226926791, -6.613120549447177 57.45500073364103, -6.623518055754412 57.459198923537166, -6.613384469410694 57.467403870522, -6.63180577967021 57.494258598860284, -6.63747059725318 57.50250796893041, -6.6185471320031075 57.501565990178655, -6.598478600936873 57.50991468342232, -6.596912745991149 57.50906508562889, -6.567920739654659 57.493323016441465, -6.560580649023959 57.508026631150635, -6.641651613104187 57.55205847042213, -6.649312977089224 57.54343277522044, -6.649803135815609 57.54288080145969, -6.655674844464472 57.54623134486551, -6.652282832920239 57.54963298439141, -6.640712540893737 57.561230887487724, -6.636217452147611 57.59748885682362, -6.634849117850693 57.608513897958865, -6.582685545468624 57.5883566120053, -6.581472481740318 57.585524908936996, -6.565513485402384 57.54822645162583, -6.562909434804719 57.547456044954835, -6.550425592761214 57.54376149696286, -6.540884415917348 57.54793735454893, -6.5043194225838725 57.53404974118286, -6.4608344421677115 57.503942848763906, -6.465671157671108 57.49806917614531, -6.4654350478007165 57.49819284453936, -6.465555888780846 57.498041789816305, -6.4270992501486095 57.518255482658184, -6.4360092680557 57.491561028688665, -6.418975409506774 57.50537374662322, -6.399813530622079 57.508841817599524, -6.399610472833793 57.52870256191891, -6.383657875784872 57.52040893802549, -6.365160442009885 57.51516638545856, -6.370821906607854 57.52285790433329, -6.376183773913733 57.53013908772919, -6.367059799079385 57.531436023632054, -6.393483337950417 57.55213991693031, -6.395054617498842 57.55537077448389, -6.394617206913436 57.56858625560259, -6.394175098357154 57.569293221055126, -6.382584665245706 57.56310830210578, -6.359427793014858 57.58894657834573, -6.359558452180083 57.58893227256339, -6.394608393416486 57.58517355902901, -6.394237520494343 57.612723973586164, -6.427591807913414 57.64219435406558, -6.406393402132901 57.65047120935723, -6.40626856639812 57.650520179369984, -6.409145190209898 57.66030089513166, -6.382795542531188 57.65966428281028, -6.361703923001006 57.667995647562535, -6.355311131735021 57.67051942162942, -6.3535721664034135 57.6712057624541, -6.342350492912457 57.68502359801044, -6.354196417782762 57.707903888143726, -6.343761559117461 57.70421436503897, -6.330731436851132 57.69960483254483, -6.299102954459449 57.708032449257686, -6.2919700822479685 57.6992565195442, -6.291703371456829 57.69880708362448, -6.303207142898843 57.69159198373234, -6.289024348312631 57.6915561176063, -6.251069540971766 57.67347468681834, -6.235353899030748 57.63736160390624, -6.1913090627864085 57.63325179603225, -6.150594073540169 57.586519331850546, -6.142637177926928 57.58724432078484, -6.1372371161890955 57.587736026854934, -6.148801959771677 57.56562284817868, -6.1468119604241895 57.56183369856923, -6.138767842817256 57.546509798797004, -6.148480545205465 57.49996944499963, -6.136449322100918 57.472386387814744, -6.14095291750296 57.451874833012454, -6.142633755900081 57.444376213403416, -6.145852151504368 57.43001038665383, -6.173406525108944 57.412840507190964, -6.173471911563399 57.4128376521184, -6.197736174616509 57.41213498988468, -6.202132688014562 57.39054850487725, -6.20213612152341 57.39048709946394, -6.182693331765931 57.4073408806718, -6.182421109433725 57.40757590690959, -6.175695214590527 57.40227806102169, -6.156915936229671 57.40405896705544, -6.13947440783712 57.40571026402569, -6.128565498189868 57.392879194153984, -6.124207581689844 57.38775047311573, -6.131120367777245 57.382501027720714, -6.14621317667692 57.37103329667903, -6.1168585068281045 57.34169173561948, -6.103831662622196 57.33855428997182, -6.104139653967089 57.34570211392243, -6.095204767409565 57.33918650832512, -6.107115460730038 57.33523312356363, -6.105267097013367 57.3185424118459, -6.131914269976128 57.31625371058435, -6.164009944436463 57.29791007837776, -6.168421961688947 57.295386436187634, -6.117433240814293 57.313680915691634, -6.057399403145229 57.31368833795387, -6.041644516225105 57.29326704674979, -6.08304555416858 57.26770395550825)), ((-5.978390465433086 57.494583976076854, -5.983460055895381 57.47902541885783, -5.9988306963774685 57.455614909515965, -6.032363334719929 57.43429088356125, -6.031239385811114 57.430569034388434, -6.018653639440703 57.38883602157088, -6.014989257079262 57.386263310825974, -5.993437249148177 57.37112615527625, -5.993232264051969 57.3692421466883, -5.994506403348298 57.356668086030375, -6.021135203654985 57.33333961167704, -6.050269077014239 57.32838260658092, -6.06766180197253 57.33442162826762, -6.066002427043457 57.346034914089316, -6.087899632920393 57.35086402369064, -6.074330417354728 57.37887376302238, -6.074546399384561 57.37964702436469, -6.084681020386957 57.41872948758462, -6.07768691192647 57.44010940658304, -6.05504175369255 57.461927378587006, -6.0178298320096735 57.454609751742986, -6.015146017102396 57.45892981804415, -6.011314712435771 57.465095110937575, -6.029444222033205 57.46943543817402, -6.029447740564672 57.469517251569414, -6.03041864543006 57.492089278369384, -6.011284236930575 57.489598738379776, -6.006493244975642 57.49059992222657, -5.992069646680422 57.49361269772826, -5.990960210832526 57.50541192745788, -5.978390465433086 57.494583976076854)), ((-6.182645000495934 57.15529535272472, -6.189064934666508 57.148627056482184, -6.210140538259098 57.15056239593744, -6.210549846760125 57.15059994495647, -6.204075036374586 57.13617458144935, -6.204879783530645 57.135807940385845, -6.217247054464067 57.130171884392006, -6.248989237691392 57.13779935255902, -6.255955836798262 57.14954611888332, -6.24497364983542 57.15277326181177, -6.227180545960652 57.15799846304091, -6.219384688729095 57.154784328268306, -6.214659947232407 57.152835851149696, -6.21516679736988 57.16154906266383, -6.215358600113735 57.16484490128026, -6.193408183309731 57.164222702598636, -6.187161320336982 57.16404492375377, -6.1798768986566825 57.158169723483255, -6.182645000495934 57.15529535272472)), ((-5.966535119522561 57.583557628844986, -5.953899679656147 57.56853118070937, -5.954134047417265 57.56802265004691, -5.978751903882321 57.51453063477621, -5.9978891373867 57.52753836686822, -5.980442980021386 57.540440668739016, -6.0009374476230475 57.53917136810696, -6.000208022036888 57.541864098561454, -5.999229769307265 57.54547484114129, -5.976352137369956 57.54968759713873, -5.987048815176106 57.56698668537443, -5.9892887341853305 57.570606892883816, -5.984215856524015 57.57376151553541, -5.9818192022587295 57.57525164151787, -5.9788043260490795 57.57712591471301, -5.978768064432871 57.57714845598658, -5.966231917534753 57.571869152833834, -5.966535119522561 57.583557628844986)), ((-6.001496454807195 57.51600876260484, -5.992914255659429 57.507086744849786, -6.008982083229785 57.5132665265134, -6.013561745852775 57.51502712610408, -6.001496454807195 57.51600876260484)))
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>