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/S02002507 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002507 geosparql: asWKT POLYGON ((-3.5676510531941474 55.97854714259037, -3.5674199517375618 55.977588583703806, -3.566936137386524 55.977513853412745, -3.5676763568697725 55.97600379209091, -3.568353156711056 55.9749078784083, -3.566031817622413 55.9745689742278, -3.563559119135083 55.97405222777748, -3.5615909608748795 55.97372673089509, -3.5598441737530386 55.973326513275424, -3.5558457177809712 55.971921334828465, -3.551953850513367 55.9711706603763, -3.5510600500179117 55.970858419483335, -3.5553840075594456 55.97039955081152, -3.5545376706221314 55.968873631814056, -3.555574912181543 55.96717118174846, -3.5550320918157623 55.96402396030137, -3.550806165763631 55.96411316387168, -3.5488484418550734 55.96443432458268, -3.543142577108177 55.96477549333609, -3.542396531730973 55.96374246469483, -3.541148220599848 55.963371688813034, -3.540710360901756 55.96202927304971, -3.537430402904752 55.96213314464183, -3.5372947979384985 55.96113739726769, -3.5341913882411435 55.96124796615845, -3.534028842087183 55.95997398705683, -3.534171893824506 55.95954087192245, -3.5348457875785324 55.959164064437864, -3.5354193575778 55.95706319356269, -3.535796126935338 55.95646542526873, -3.534548944485653 55.95247323395158, -3.536480222334357 55.95230538758938, -3.5367082414911666 55.95199702122239, -3.5387458664042826 55.95208840523136, -3.5397877635201564 55.95170696313427, -3.5404685556373603 55.95110536998193, -3.540629621513326 55.950321586317784, -3.541313161802293 55.94938748211402, -3.542063645551913 55.948928791950145, -3.542937270734624 55.94915148823892, -3.543449405395655 55.948740704330106, -3.545236662193762 55.94817914071962, -3.5465726954466485 55.94835108031184, -3.547090512644062 55.948083985123546, -3.5488475062594063 55.94877179031855, -3.5499082356014435 55.948866290171814, -3.5503260495726434 55.94850159367224, -3.549483537107222 55.94785621729178, -3.549877802237789 55.94730311284545, -3.549774882051456 55.94592955272764, -3.550188718076834 55.94546606056553, -3.5491557874627873 55.945263391334244, -3.5486059815891866 55.94432677642578, -3.5486271049834195 55.943652562230774, -3.5501074634104524 55.94303188144741, -3.5544033534277757 55.9423487274849, -3.5560625257956406 55.94259735905314, -3.55985108773428 55.94124647159586, -3.5632325470241737 55.940925037923314, -3.5651860504452055 55.94053180405006, -3.567259546187003 55.940334704559746, -3.568271744002668 55.94002528281179, -3.567666797467768 55.937337177745256, -3.5655153875185897 55.93440814081411, -3.566829999878147 55.93405895027247, -3.5667853077225873 55.93374498998579, -3.570010804410706 55.93275140556819, -3.5717474802217573 55.931758763322286, -3.578541940309221 55.931150666730694, -3.5778286622736366 55.92974900644039, -3.5773981669829187 55.92979046517921, -3.5738193983799262 55.925628085687585, -3.573161346069966 55.924943426851634, -3.5694722086575363 55.92074784569902, -3.5671756170813214 55.91832662757154, -3.5687795535931337 55.91803982129993, -3.569085080730911 55.918441882230425, -3.5707841205069317 55.91823807314051, -3.57050896997502 55.917765322004264, -3.5763881565998017 55.91595588689867, -3.5790981429833266 55.9157054972962, -3.5842270869970956 55.91474100709338, -3.5923982378146158 55.91383584171798, -3.591715488485856 55.91279329685192, -3.5904514308887303 55.911228079140315, -3.5983636311153924 55.910271939295434, -3.5986104107039734 55.9108258740396, -3.6043364400924887 55.91078733967469, -3.6028035467065354 55.90853381703174, -3.602234655982128 55.90831656879407, -3.6023065121046702 55.907731536065945, -3.6051883174845925 55.90701105206506, -3.608681693135703 55.909086182070645, -3.6092680890560076 55.908566309612596, -3.6134052116572786 55.91110915684406, -3.617284706843585 55.91014167291486, -3.619276601373789 55.90956733729274, -3.6191671366169174 55.910395501961666, -3.6193800548571318 55.91243255553609, -3.619494614815213 55.916331031946505, -3.62304118837241 55.91619446429416, -3.625199203730393 55.916525454641054, -3.626856222794834 55.91520955878708, -3.627390732466553 55.91536425508401, -3.628225684084955 55.91467026947617, -3.6303081581763825 55.91434617075208, -3.631079070574293 55.91365301307178, -3.632256248143428 55.91387104441198, -3.6341465084020723 55.9136227821788, -3.635746531011101 55.913404424754084, -3.6392650772854527 55.91338343868784, -3.639916658647246 55.91324989020476, -3.640093171616277 55.913556798214145, -3.6412213664205093 55.91415024585543, -3.6429333982955434 55.914744485276515, -3.6437550022080316 55.915632137277704, -3.6455787796088486 55.91902252367533, -3.6503385774788613 55.9176827843968, -3.6498157088870786 55.91705177220941, -3.6509278441336632 55.91611128892796, -3.6527988781264784 55.9157087681361, -3.654178605048343 55.91692134835118, -3.653869993771677 55.91794093330332, -3.6568192100776082 55.91805405104316, -3.657946141132781 55.91746376606231, -3.658558848345051 55.917572335217734, -3.659539976251851 55.92007170157235, -3.6615925740191315 55.921318048623355, -3.6624813475193054 55.922138570068164, -3.6642612495062075 55.922186192401476, -3.66613690979018 55.92251803904245, -3.6704110308007345 55.92377435919449, -3.6741172067039485 55.925765149839535, -3.6759058818106114 55.92556423268546, -3.679890145755758 55.92627987685628, -3.6794464683862156 55.92805332010124, -3.6776895594469616 55.928634816756315, -3.671183338421447 55.9297059825902, -3.671130134872667 55.93015219550185, -3.6668506891555195 55.930769958291876, -3.666562782963482 55.93029208681027, -3.665513850934941 55.930068663810204, -3.6643710345897147 55.930079297037835, -3.6644017317167714 55.92926693490878, -3.663143162282187 55.92927868990582, -3.6630820537895588 55.92960008843979, -3.66104803620626 55.92920973541587, -3.657124491360926 55.92920670706632, -3.6571440351497118 55.93005625801155, -3.6585060181567104 55.93035351863688, -3.6594105867291615 55.930749351276475, -3.6576914768627766 55.93095090517235, -3.6565358680909124 55.93091709319259, -3.656430478306837 55.93124534689333, -3.6546730972210977 55.93129087890742, -3.6512461332650625 55.93074550328306, -3.6486239317027094 55.93043915884036, -3.648344810334884 55.92987141992931, -3.6471159083982916 55.929823844365394, -3.647254228584433 55.931733521402535, -3.6453641063345676 55.931704870497086, -3.6415568351383896 55.93088949175177, -3.641888786714694 55.93163548221075, -3.6432957714487184 55.93206367016734, -3.643535349679967 55.9318717640696, -3.644910591284509 55.932176195658904, -3.6433897307781233 55.93271072236246, -3.6422499975656484 55.93263606363183, -3.640670596458095 55.933142365563555, -3.639477102300615 55.933311023169395, -3.639780966986595 55.93406181395716, -3.6388874651838012 55.937164938857485, -3.638373465437547 55.93750426417473, -3.637181342847741 55.93770882467468, -3.637410051685971 55.93895485702079, -3.6360178897986923 55.93897335622291, -3.637074517255256 55.941628197846036, -3.636776478065382 55.94367200938103, -3.6391004315242093 55.94372198252949, -3.6431188119256968 55.944081808208196, -3.6475145071065667 55.94427470681144, -3.650527035078587 55.94432423937773, -3.6517951950657763 55.94214158693908, -3.656081358343425 55.94089785265426, -3.659385759227695 55.93954139992725, -3.6621696730127313 55.93949488597479, -3.6639954608066314 55.93962134541158, -3.664263758855915 55.93979010236373, -3.665642797969151 55.94170348074782, -3.666141011128669 55.94286495459864, -3.663945617978823 55.94469186493571, -3.6639100421236535 55.946849025893535, -3.6659904562764605 55.946443487963464, -3.667564655752111 55.946606654961286, -3.667492193255535 55.94721754630466, -3.667701607511417 55.9483263008734, -3.6682947559033736 55.94871276235425, -3.670245416032494 55.94871418674735, -3.671707543437102 55.94908525239907, -3.6727809713876467 55.94987494385405, -3.6726779730735233 55.95060602001223, -3.673029622860567 55.95148099152178, -3.673914623981363 55.95224447652614, -3.674352933013948 55.95304817466746, -3.67421953763045 55.953927936933816, -3.672589794899332 55.95468873742628, -3.67038379714574 55.95460454301695, -3.6686963182067545 55.955105475208875, -3.6680516047733303 55.95609190046495, -3.668001639008057 55.95709004080013, -3.6674460624511886 55.95765470320594, -3.6657798206588352 55.95790818727469, -3.6652961081227855 55.95835684516951, -3.664621360274978 55.96017667308502, -3.6632991758440094 55.96076065694548, -3.661472558059053 55.96056514197669, -3.66106029276258 55.96029033303201, -3.661192272463664 55.959743993279595, -3.6592831212062777 55.95915327014652, -3.6580622643733105 55.959073554369176, -3.6562030278115065 55.95867441563698, -3.6533919589747024 55.959093183804775, -3.6527817092004975 55.959470701687835, -3.6532275575783415 55.960083864400914, -3.6537573567365533 55.96157564092481, -3.654433082197078 55.96235645018731, -3.6548656591821604 55.96333282497616, -3.6566840026455703 55.963707373812625, -3.657489939653783 55.96423390561761, -3.6569990676124813 55.964781472981805, -3.6525612359166275 55.964558009875006, -3.651910122583129 55.96484351343764, -3.6524447019138826 55.96546986703123, -3.6510814487204324 55.966414610780355, -3.6494023805197098 55.967168567973594, -3.649402417323806 55.96863779711831, -3.648670232362891 55.969430285925064, -3.646978810313701 55.96985997528015, -3.644934581294346 55.96956647682538, -3.6442344103679045 55.96975644215392, -3.643364947565029 55.97048872594194, -3.642336778311045 55.970879849631395, -3.640137561454521 55.97063506474424, -3.6393897529791817 55.97087416114828, -3.6384620129482106 55.97153893080965, -3.6376782107794217 55.971931218239796, -3.635991928746822 55.97237416576039, -3.634653361013388 55.97300747593969, -3.632264413780921 55.97362413578785, -3.6316392532872626 55.97396220374716, -3.631911430525337 55.9745319967511, -3.627874448315179 55.97421686688433, -3.6280991793947694 55.97308166213953, -3.6273264459984613 55.97261560715828, -3.625976797944702 55.97214817493429, -3.624629239729789 55.97211203179891, -3.6238921109315148 55.972876568217, -3.6242628709061084 55.97407582353062, -3.6144001779140376 55.974286297603776, -3.610865768494336 55.97445835340145, -3.6068761545519283 55.97486089478516, -3.6039789518422785 55.97532996714511, -3.5977295499693014 55.97612104334739, -3.5927099265500595 55.97681503940612, -3.5903561579791843 55.97720568543775, -3.590592124800158 55.977902793601025, -3.588550000660734 55.97822098943621, -3.5865596572446066 55.97826088920599, -3.586703185449496 55.97923786792433, -3.5843576178043497 55.979492702588296, -3.584141742989014 55.97857658810756, -3.5826915924923206 55.978708595925234, -3.582605802810052 55.97833239390069, -3.5800512964466136 55.978489914561735, -3.579052917535866 55.978320708359625, -3.5720590471125373 55.978562872469816, -3.5676510531941474 55.97854714259037))
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 lang='en'>
<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>