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/S01010551 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01010551 geosparql: asWKT POLYGON ((-3.7124039852046633 57.56687958854251, -3.71308175734644 57.5666148927223, -3.7149306614250976 57.56650354861991, -3.717270279621609 57.5656649783161, -3.719198583868348 57.56511137082028, -3.7204272972490062 57.56425911168317, -3.7214968708343314 57.56439277218474, -3.7228109318040707 57.564074779879405, -3.7245918162397564 57.562846594239325, -3.7254645050016117 57.562600185539, -3.7273512793937265 57.56251688927201, -3.728653496294733 57.561880958295674, -3.7297325701419894 57.5609821397383, -3.7271727011867615 57.55955815750515, -3.725326366845518 57.55840827503084, -3.720634348298787 57.5566359478509, -3.7196602003771977 57.554231588563596, -3.7165999242623227 57.545918885133496, -3.716520276451557 57.54593074920887, -3.713557020071058 57.53833718265704, -3.7126898491179046 57.53622759324304, -3.7250242669131697 57.5344875818369, -3.7249172950727423 57.53540542985734, -3.7308828457392247 57.53438940061995, -3.735259876622948 57.534751552027, -3.734682094977503 57.535355389561865, -3.7349774016302946 57.53816406576953, -3.728778491135929 57.540539911525116, -3.7320341424864614 57.55196462902943, -3.741136427037797 57.55183941805436, -3.7437280189954323 57.55046500328405, -3.745864723313338 57.54971673517211, -3.7527118307579572 57.55205657487229, -3.755344595721903 57.55189421569835, -3.755945969584113 57.551194067654286, -3.7555665878423086 57.5502560013146, -3.7554645012015277 57.54882893021335, -3.756044819589748 57.5480392315398, -3.7607990943069156 57.54890736477335, -3.7577313134336534 57.55217545430802, -3.7658231850982 57.55430858620571, -3.767778026761111 57.552915624130826, -3.7713589016186293 57.554725169275294, -3.776283067057087 57.55116110660557, -3.7809797728598316 57.55219999785469, -3.774583807671532 57.5574560126691, -3.7817123901976286 57.560356355920085, -3.783869551296499 57.559688013628275, -3.7865853977989112 57.56121287513702, -3.7835365941634964 57.562792287603656, -3.7873737194054784 57.563313020784506, -3.7902635779819596 57.56428730201813, -3.795339287624708 57.560765547322895, -3.798045043778112 57.56206542451309, -3.799032055976487 57.5634349457961, -3.797824014308606 57.56368574390713, -3.797681849895247 57.564532291008604, -3.7986114661875225 57.56507607826658, -3.799353707536925 57.566901163970755, -3.7997865504247117 57.5675418369769, -3.800855990006427 57.56740897617053, -3.8010222770520827 57.56597571251032, -3.80369280010705 57.56650603004713, -3.803145311171544 57.56539344482106, -3.804840819499933 57.56561609161223, -3.8079978024483845 57.56522053914965, -3.810003073223475 57.565200794417606, -3.810196855854057 57.56438942835125, -3.810703611518076 57.5641575544131, -3.808597993802439 57.561815874320764, -3.8145343369552847 57.55815493783255, -3.811661569755414 57.55483605543018, -3.8113973282409557 57.55220743862014, -3.8148719243659706 57.55318174886829, -3.820588841609994 57.55110657739134, -3.82006036020982 57.55069646163013, -3.8235170596999537 57.54930500458738, -3.827152068965917 57.547092896464775, -3.832106413495607 57.54891678966348, -3.833930673564781 57.5509656917673, -3.837687549880094 57.553552457659144, -3.839243594215338 57.55424855692838, -3.8427493064517404 57.55317322122565, -3.8464683072568593 57.55466418820736, -3.8482938420662687 57.55605701040167, -3.8496521197184803 57.556475421750115, -3.8523402190305687 57.55679810854096, -3.8578979417478996 57.55362500867602, -3.859032017411205 57.55324893610339, -3.8610587827043314 57.55333587109164, -3.8621485748302367 57.55471238821246, -3.8657865197353183 57.55329303874641, -3.870440533863453 57.55311635472735, -3.8722616110598795 57.55375420946402, -3.874108603559919 57.55326860480999, -3.8787892747692174 57.55425918940612, -3.8768046997904304 57.55596880454921, -3.8768823087031103 57.55747704096108, -3.878393294761301 57.55825418632698, -3.8789916192286773 57.55948513931535, -3.8788364841055536 57.563287876225324, -3.8777703907570484 57.56433694856276, -3.873756511506386 57.566912224012974, -3.8731269368237253 57.5693024423171, -3.8703526044318877 57.572699036078255, -3.870280136514349 57.57321999344522, -3.872135556798579 57.57336760690759, -3.8724002755286393 57.574101995983774, -3.87330940873272 57.57428908154243, -3.87245768436584 57.574980909434906, -3.8715282694124835 57.5747136985844, -3.8706048194697487 57.57556803563636, -3.8688121165188627 57.57531588927908, -3.869233080549921 57.57635019014847, -3.86494794254863 57.578403231244444, -3.864063643573032 57.57910402505385, -3.864259474877653 57.580125950049855, -3.8655624273231406 57.580252608273334, -3.8652213367023345 57.58135122845848, -3.865237970199591 57.582285603817674, -3.8645369476593476 57.58243822280217, -3.8620572285795154 57.58304160448455, -3.858840247998026 57.583592116911866, -3.859680752239752 57.582034428329976, -3.8588851000378823 57.57988090195143, -3.8619622882524345 57.57584974895632, -3.863337792184801 57.5737106851083, -3.861826450741899 57.573325093022866, -3.861574341416301 57.573945957860055, -3.861890899402237 57.574586820892826, -3.86080942898579 57.574982648715014, -3.8601651264565437 57.57573831364689, -3.8587099674153786 57.57525933948224, -3.8570928117119885 57.5756681203434, -3.8564809456572333 57.57686351144283, -3.85541003349044 57.57737423562963, -3.854442261329479 57.577325595215086, -3.8538635829976506 57.57802550095316, -3.854742098695075 57.57880921371012, -3.856158614535921 57.57922021973496, -3.852473544817699 57.57963273060513, -3.8514307643765884 57.58014511834061, -3.851573718266978 57.5805045630947, -3.8527168931340694 57.58086394763, -3.852750209430459 57.581506390240236, -3.852232501020074 57.582527610389306, -3.8499631334059368 57.58253682840784, -3.8418336814248675 57.58292725502465, -3.8417713214889555 57.583050989512685, -3.8432641226307793 57.58559822652861, -3.8178016244401953 57.58882531721237, -3.809378821103064 57.59001537697478, -3.811638561381313 57.591384536422446, -3.8150828147807623 57.5933058330515, -3.8139693679499844 57.59370284992334, -3.811302767314648 57.59353637878398, -3.8089726099206076 57.594172576501855, -3.8077302515663365 57.59432116255679, -3.8118243056846794 57.597473212539725, -3.8165446818765196 57.59748293439901, -3.819277722753153 57.597127036306496, -3.8222644844216 57.59634532785438, -3.825498768914923 57.59979333024964, -3.821849330396651 57.6016864221633, -3.8194096661745736 57.603100665752926, -3.817633036504491 57.60371919949247, -3.815449114826959 57.60472988423691, -3.813302457022883 57.60598706257503, -3.812153342955859 57.60643928524876, -3.8118671515013425 57.60691057108724, -3.8100932414086306 57.60809496493191, -3.8093522570875678 57.608172959471304, -3.809384163716695 57.608814876755794, -3.8076797808546976 57.609885936204684, -3.806744400537617 57.60958935752804, -3.8056682624026648 57.60982934611946, -3.804772585972971 57.60965796502653, -3.8029694563957492 57.609921788178504, -3.80277558781469 57.60955620044759, -3.800439688167066 57.60954009180292, -3.7992775673111243 57.61007323822207, -3.798882871507555 57.611062630862605, -3.8000236199338806 57.6119582856327, -3.800480912202052 57.612742383123276, -3.799726694827122 57.613570691741266, -3.7975205807550614 57.61466222991207, -3.7962059716850183 57.614653969645, -3.79589092420863 57.615224452965016, -3.795100286383796 57.615487244383814, -3.7927911775134184 57.61687214725476, -3.791835076960734 57.61785600131283, -3.789822647356155 57.61898059076732, -3.7876655761508875 57.61903807383997, -3.785936299216918 57.62013613362409, -3.7852883615603354 57.62006892248255, -3.7831637506584603 57.62147349705532, -3.783289676202036 57.62182210343617, -3.7826693835323844 57.62317398906348, -3.78469504408452 57.623504768995545, -3.7851997129985655 57.624063645349516, -3.7874358769973675 57.623564863211485, -3.789112926762734 57.62293470182048, -3.79219207996015 57.62272486910104, -3.7946941966413252 57.62202004075222, -3.7973527975317225 57.62143422352541, -3.798104094719289 57.62223660819793, -3.799168524784384 57.621412880659356, -3.800631822242645 57.62103716455119, -3.8021045421777178 57.620176148800546, -3.8027066963151657 57.61931855183035, -3.8038068960932057 57.61905129523429, -3.80496500314774 57.61843281684467, -3.8050574360303377 57.617595963017045, -3.8074995980842514 57.61738137284589, -3.8086877035487086 57.61669504832372, -3.808783284082425 57.61609173606731, -3.8108795512765434 57.61565742362091, -3.8126408705499255 57.614549569712544, -3.8147609182512454 57.61375548017338, -3.816018217284427 57.61345441373786, -3.81676100652097 57.6125722633882, -3.8193844654176248 57.61247612088319, -3.8209135906298304 57.61091778464806, -3.8218390078329914 57.61101225706153, -3.8221351363868528 57.610239831450954, -3.8230068506425594 57.60993077694878, -3.8229568058444756 57.609432871323776, -3.8236056899171507 57.608853006326896, -3.8247313702081134 57.60876937324043, -3.8250431735784876 57.60831116358762, -3.826341594216794 57.60783420421451, -3.8264447303854427 57.60872215821167, -3.8247908537338913 57.610623769128054, -3.823081988406951 57.612099396507965, -3.8218029722098223 57.612800647537895, -3.821049744644269 57.61347184853387, -3.819251972527871 57.6143467563095, -3.8173820514164865 57.615622474935975, -3.8149976394678164 57.61692442164115, -3.813033414037096 57.61772436370529, -3.805761010670618 57.62097745959944, -3.801608064810934 57.62249217203008, -3.7992650214551977 57.623194873617074, -3.79688221113546 57.62377231738121, -3.793089270554988 57.62445058644976, -3.7882879201981186 57.62505766226593, -3.783837840897618 57.62548889777696, -3.7819150997643782 57.62555195609285, -3.778156283393692 57.62608105307253, -3.7752052103429503 57.62686372804546, -3.7732521381094757 57.62716515990344, -3.766555416803917 57.62910517218261, -3.7658214478251804 57.62951072976412, -3.7634607316614943 57.63021749617999, -3.7623514437874572 57.630690259002776, -3.759909030016341 57.6281539297193, -3.758977870174584 57.62704567235725, -3.7568059881339684 57.62299259389652, -3.7511953843994754 57.61288246291473, -3.7484044948523305 57.60771929918305, -3.747041818457967 57.60583171528921, -3.746791554533869 57.60515058475412, -3.7448632020801393 57.6051799242116, -3.743390616637886 57.60459112774071, -3.7426227896837916 57.60330531105031, -3.743085822697798 57.60259456865973, -3.742388468905896 57.6013814481461, -3.7399570914881255 57.59960016276959, -3.738753719440953 57.59819454344759, -3.739355946448654 57.59756006038348, -3.745013285868411 57.59458735681124, -3.746345659743366 57.59421407201925, -3.749355520363236 57.59278168245262, -3.74868327705481 57.59090701357087, -3.7464725058930783 57.588414849030556, -3.741135614734652 57.58581843759189, -3.7277523847940164 57.58015534248515, -3.726909197685279 57.58043280412624, -3.724955770794808 57.58068230407155, -3.724515654726492 57.58007649583064, -3.724818754668385 57.57835279674652, -3.724355551492467 57.57697556742035, -3.723230102094753 57.57598650421123, -3.72249051257775 57.57452679176035, -3.720529464878599 57.573510467771115, -3.719899394947959 57.57273833051901, -3.718478100195203 57.572668736520534, -3.716543597354471 57.573028356568926, -3.7157879888972536 57.57271340014695, -3.7159773590842775 57.57215381235372, -3.7172166717385986 57.57066806383231, -3.716277952700062 57.5703340385848, -3.714458449891794 57.57022129734091, -3.7135696682236436 57.56966826042471, -3.7131837528891367 57.567860503486884, -3.7124039852046633 57.56687958854251), (-3.803536468093884 57.57744007633735, -3.8030883860200837 57.57787811519835, -3.8043456489002536 57.57844925848863, -3.807202356014281 57.57703353829045, -3.809339548417477 57.57807302086921, -3.8109689420903465 57.578060780557884, -3.8126851373717328 57.5777852732141, -3.8135446278523886 57.5779166828719, -3.8143177841174443 57.57732159837837, -3.8124904677626072 57.576179535812756, -3.8117298305910383 57.57458131667871, -3.8117192981930694 57.57349565180986, -3.812302134409643 57.57310995703789, -3.810662933270136 57.57276510526436, -3.809911565619404 57.57290511542358, -3.8091015006038407 57.57358666401688, -3.8084631449315305 57.57460247069872, -3.807822388629278 57.5751143880238, -3.8062333229033114 57.57485483023968, -3.802126191947708 57.574962587025304, -3.8013008747812673 57.57546183965442, -3.8030739045968973 57.575763628666245, -3.80565684745208 57.576017213724924, -3.8058259536661816 57.57639214487112, -3.803536468093884 57.57744007633735))
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>