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/S02002094 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002094 geosparql: asWKT POLYGON ((-3.5541469312396874 57.628115899009764, -3.5616636477551746 57.6243774218214, -3.561122048367486 57.6230724852193, -3.561302403534335 57.621812532764984, -3.563174552929949 57.62134918883061, -3.565555485894567 57.62025963637876, -3.571140770923963 57.61934259406869, -3.5838480460031397 57.617004188107714, -3.5901286149133287 57.615910152318264, -3.5846829418777766 57.612340113161345, -3.5879106768443174 57.61073641371058, -3.5887083358578376 57.610986918701485, -3.5907466863381834 57.611353779293054, -3.592022476098461 57.6121310743062, -3.5923909350508905 57.61213541578074, -3.593018957766051 57.61087031369287, -3.594229738729676 57.611064565471786, -3.5944870471441597 57.61171181084996, -3.5959721923356858 57.611730885219025, -3.5967004376707714 57.61154200937734, -3.5971223740101093 57.611996532706236, -3.5949444024011785 57.61309518100962, -3.5938698022301945 57.613796746665436, -3.593542211013737 57.61472621116691, -3.600118112888165 57.61351115921348, -3.598429288311661 57.61244056483074, -3.5999081786049327 57.61254356332344, -3.6020168504627734 57.612543243957305, -3.6027564228986595 57.61221998895626, -3.60368523870759 57.611264902250696, -3.603365631814049 57.61046937795565, -3.6038046678534594 57.610176341689304, -3.6035674103082096 57.60973016050141, -3.6013688051579016 57.60959633209313, -3.6001983014737036 57.60924282244986, -3.5999969194707324 57.608850083594426, -3.5986598198968904 57.60942447138535, -3.598496635914246 57.60883929840491, -3.59760820220702 57.608298926444704, -3.5976468295410595 57.607300747317595, -3.599340818378377 57.60639642237437, -3.598597119890688 57.606129337604216, -3.598163443899589 57.60559418498965, -3.5967437688540276 57.60451610483225, -3.598180297499656 57.60445302425013, -3.599718206722214 57.604792916734056, -3.601262209311636 57.60489014966768, -3.602571786754214 57.60574497136079, -3.602855122525395 57.60495313479083, -3.6050306852543317 57.6048696571131, -3.605198360322622 57.60572062472221, -3.6061701236907235 57.60574421155912, -3.606749094536275 57.60445215952202, -3.6078726446247527 57.604495983941874, -3.6078580083566982 57.60351799867851, -3.60810448679685 57.60302569080566, -3.6104793599361313 57.60251805855718, -3.6086803791025277 57.60220509593771, -3.609894842261302 57.60178883056295, -3.6108863805035467 57.60226132174672, -3.612646145515703 57.602319728535704, -3.6127611678758274 57.60340530861434, -3.6138288407017938 57.60370611189087, -3.6116205095550167 57.604479958872815, -3.613879331006333 57.60673303062399, -3.6130058944260988 57.60704065070978, -3.6132452390044154 57.60753171675713, -3.6144213503571265 57.60834892541215, -3.615060229594475 57.608451829012, -3.6163024318076538 57.60809858402287, -3.6176299886478294 57.607897894124015, -3.618952665628076 57.607530583837, -3.6188776690569977 57.60659722164388, -3.6207130696833962 57.606648700195414, -3.621459634701226 57.606321551208396, -3.62190330673325 57.60741464872794, -3.6228648099692173 57.607710989947485, -3.6234461228702117 57.6074257411469, -3.624264088529619 57.60785617659443, -3.62591164318796 57.609318874647414, -3.626594431508802 57.60961552170785, -3.628482119243253 57.60990565421698, -3.630629293585416 57.60963540021726, -3.6311159090036527 57.61115638197045, -3.633840972257829 57.610747044167375, -3.634130544792157 57.61185565926393, -3.637623673544896 57.61219627204834, -3.6396628580941295 57.61249218166468, -3.6371304464982037 57.61350425949995, -3.6337881494538746 57.61475135548511, -3.632261713087581 57.61580422729163, -3.6325527637630115 57.617067196181516, -3.6322385800873525 57.619020757613285, -3.6327750331457325 57.61940911927195, -3.6324147378011613 57.619961790336454, -3.634379429075087 57.621580450426265, -3.6311779403401823 57.624119326883125, -3.63227369550181 57.62652184391742, -3.6316588243871775 57.627374257638564, -3.632424075429948 57.62763390236027, -3.6316133983988323 57.628228320845025, -3.6315917319375357 57.629612116588426, -3.6319240316908945 57.6312394683853, -3.6301257401888174 57.63311713172978, -3.6287815027442365 57.63432535622074, -3.6265818964206895 57.63559075369232, -3.625832719262895 57.63576838874404, -3.6254137729490274 57.636738641316754, -3.623885756820179 57.63778153769303, -3.6232757997198846 57.63796455585506, -3.6209205521072745 57.63811515824379, -3.621340449252161 57.63682508488142, -3.6203960490419616 57.63668267099627, -3.620931761602817 57.63573609373134, -3.620516938254745 57.63522304417785, -3.61946779063877 57.63562369183886, -3.6190659468027193 57.63513832131177, -3.618182647913294 57.63523857069703, -3.6166251828375247 57.63490633047292, -3.615151412027418 57.63476076407887, -3.6138270733157882 57.635113672350215, -3.613495598776403 57.63549522417292, -3.6140234479755255 57.6360661463258, -3.612674150264807 57.6361597281069, -3.61123781052488 57.636028012475755, -3.6102441679496207 57.63549086264114, -3.6101765189751083 57.63487094219187, -3.6087759335567915 57.634599494410644, -3.608578402984645 57.63505658924355, -3.606853271203922 57.635476511270134, -3.606253334390713 57.63501518015786, -3.605791082319491 57.63421969632051, -3.6060583796669285 57.63382460640098, -3.6043625145896914 57.63373024884271, -3.603465198572739 57.634158482350855, -3.6012217249051983 57.63361015790996, -3.6007850131098755 57.63232651231016, -3.600335481965372 57.633453382974295, -3.5998253037249923 57.63377337518414, -3.598716604758082 57.633928446734565, -3.5968521764597825 57.633542349904026, -3.5956765629740266 57.63266869679966, -3.5958542275250402 57.631526409992524, -3.5950103964828113 57.63149483598869, -3.594637438565061 57.63104855457831, -3.593333516897648 57.630989532467495, -3.5925805651323 57.631426649282645, -3.5919532289517373 57.63094583274329, -3.59123153356273 57.63121724601745, -3.589850958608349 57.63082226074099, -3.5886701244302737 57.63143362980791, -3.588962743725439 57.631767742412585, -3.5881361014064344 57.63243664168045, -3.5871857290068485 57.6327657050587, -3.586218474914577 57.63397538334474, -3.5850140456993476 57.63408751485462, -3.5842371641628654 57.63444043308635, -3.582364271948148 57.634934639185175, -3.582216543231995 57.63612054896581, -3.581794785597029 57.636475295238135, -3.5820003308651014 57.63738816917072, -3.5809976051189163 57.63809695433842, -3.582102319595486 57.63911626776192, -3.583093259563705 57.640906905109674, -3.5838235128761466 57.64109540215296, -3.584644081020002 57.64185682574028, -3.5843689652395807 57.64219446959803, -3.5832016773250293 57.642511844348476, -3.583774780907757 57.643207197005715, -3.5832655074750592 57.64363132009258, -3.5834422002297557 57.64434151961225, -3.584595203683499 57.64469451081642, -3.585225009023579 57.64545921986613, -3.585283760226352 57.64638381068382, -3.5882910151767136 57.64844737424168, -3.5900224485945085 57.64904535842242, -3.58831574687878 57.64978086485762, -3.5868837143510595 57.65058542605102, -3.5907824499066416 57.65158365931798, -3.5898039992060613 57.65218385267356, -3.5902796240587462 57.65239680651323, -3.588397725242693 57.65373941804122, -3.5880423843621547 57.65456876025407, -3.5874070859189593 57.654929456577705, -3.5854477253175676 57.6551566286949, -3.5804091595244407 57.65547559808397, -3.578827597730401 57.654861694025236, -3.576080985363826 57.654132337233925, -3.582389341634883 57.648905870714614, -3.5812967458546314 57.64805709870199, -3.5800726774480345 57.64803646541607, -3.580024316293282 57.64730938603893, -3.57771518130964 57.64545163225239, -3.577270046650832 57.6448462909217, -3.577799653863495 57.64430964210588, -3.576739642957411 57.64381977528583, -3.577789877137745 57.64292626654425, -3.5760908446779163 57.641324461958376, -3.5751240395323443 57.640721294101006, -3.5743749128799247 57.64060869724056, -3.572607772270549 57.64164326565463, -3.5717110237053324 57.641438806537, -3.5708095665715365 57.64151288659963, -3.5678109396501885 57.641127879169744, -3.567547007597501 57.64083468846837, -3.5699461486694273 57.64014012376548, -3.5702079077047166 57.639606832390555, -3.572078286280388 57.63792159620145, -3.5736138752358215 57.63712412213942, -3.574741741533556 57.63611899078513, -3.5767610623321024 57.63658758699651, -3.5791621926641253 57.63641786579679, -3.574698509349737 57.63301505740839, -3.5760772864102224 57.63274593461049, -3.5758722735829513 57.63236929483553, -3.574672408803599 57.632165797397974, -3.5747493150116263 57.630359965850154, -3.573112876026738 57.62969757528554, -3.5666861048371064 57.630963244996124, -3.5681579856183236 57.63247223194988, -3.5647705006914023 57.63315205322316, -3.5623983336992477 57.6344660674298, -3.5606514048924227 57.632602570814655, -3.5611843096064106 57.6324735963792, -3.56004967468322 57.631704967718086, -3.5592102807924557 57.631777688791196, -3.5555497409220145 57.62949330501358, -3.556589024498942 57.6292602665486, -3.555892423103054 57.628583716335676, -3.5549733133936368 57.62882887234238, -3.5541469312396874 57.628115899009764))
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>