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/S01006081 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01006081 geosparql: asWKT MULTIPOLYGON (((-4.44570663744412 56.11516818461119, -4.442681518981328 56.1141405816712, -4.437560515110484 56.114053068980716, -4.430150813227216 56.10981322588251, -4.421951849018241 56.108599273327634, -4.416232576891562 56.107165502657715, -4.414447978205395 56.106148881829725, -4.415949284993731 56.105211763918234, -4.41882031241503 56.10483198278237, -4.420595925502369 56.10314346342925, -4.430303132461631 56.09947451145584, -4.441711246316219 56.09984252302985, -4.44491074391957 56.09374848643893, -4.452284346172717 56.08884771578743, -4.451241469321724 56.088374115474046, -4.453648768698947 56.08780498802664, -4.453344637651739 56.08656175059244, -4.452280618763396 56.086007690890604, -4.452723440124187 56.08538772888586, -4.451674865855033 56.085076024042465, -4.452098857946588 56.0841598455901, -4.453527699523923 56.08387978789407, -4.453342826167482 56.08274203068643, -4.454680999939152 56.0825536383117, -4.454906042487391 56.08129088754438, -4.456717153981585 56.08095824668051, -4.460632086780416 56.08185989160287, -4.461975243063033 56.08104149523628, -4.463781367102609 56.08065783251176, -4.464436794969937 56.07848183515278, -4.468498495507128 56.07404482834789, -4.473011418634892 56.07578518816906, -4.474433542732508 56.07437023082229, -4.473334081184837 56.0738327808195, -4.461969624272868 56.071838896639484, -4.460460728117744 56.070705749799686, -4.460133335929982 56.06799486510646, -4.45745826981026 56.06679554001762, -4.456367483043753 56.06688151154479, -4.455821439182854 56.06632972249019, -4.456290302559635 56.065179401906846, -4.457850393090884 56.0641540077621, -4.456670125416811 56.06381068172842, -4.456737939031295 56.06285663547901, -4.453209876614272 56.06270215892212, -4.454257347670515 56.06098625492963, -4.453788971164442 56.06097376817748, -4.453815917194128 56.05940612746047, -4.454545901620305 56.05907640750915, -4.453686545365165 56.05880308787834, -4.454876088989746 56.05784995261777, -4.452417992743785 56.0567972051671, -4.452542627658762 56.05624429102546, -4.451040191934179 56.05686734403877, -4.449895115502227 56.05628791850873, -4.449998588853373 56.05538709449844, -4.448384387093142 56.05475405946381, -4.446840367703556 56.05117165151976, -4.4458979874591 56.05125325789552, -4.445022649817187 56.052394074108385, -4.442315579786592 56.05233550696937, -4.441409442913289 56.05163878454668, -4.440935432259939 56.049630484150136, -4.436066692699635 56.049113995764095, -4.434707116662719 56.0484696018812, -4.435993490188735 56.046488093880896, -4.432800669352825 56.04548657439302, -4.434007707404194 56.04428038744168, -4.432877182335696 56.044176868352054, -4.431397541814756 56.04494116435551, -4.429766100382446 56.044895007447955, -4.42885110054073 56.04432569628238, -4.429341605928684 56.04338748555463, -4.428855823065933 56.04304280338915, -4.424234592510256 56.04181255193645, -4.423287301152123 56.04130090000799, -4.425304006970397 56.03889750297254, -4.425596704801477 56.03716611759201, -4.430726808962221 56.03807171520555, -4.43022436822056 56.033911343615806, -4.429647645186248 56.03341939998401, -4.438034588578783 56.02590192216095, -4.440762857262345 56.02615352220855, -4.441724172295599 56.02688945414711, -4.444989039241016 56.02699551768411, -4.44905017186371 56.02598917856724, -4.444118715472303 56.01906763704893, -4.444871247335485 56.018279770989245, -4.44371622338766 56.01726908531993, -4.445427294918587 56.016668927381374, -4.445745631649713 56.01535939191802, -4.444788214767261 56.014174028991384, -4.445729830269107 56.01384077976126, -4.445498076973968 56.01271292415361, -4.446491443358269 56.01294486886288, -4.446887619365863 56.010330562823796, -4.448778228691344 56.010544673957604, -4.45044483839327 56.010008239237834, -4.452524238642264 56.00712675357763, -4.451264131324683 56.00597442552952, -4.44906706021971 56.00573049356348, -4.448620181271371 56.00449906362484, -4.470175453186791 56.00192983992333, -4.473210517299925 56.004996728647704, -4.473526092681556 56.00667111554975, -4.472943103441607 56.00708726516949, -4.475764264856785 56.00805523988109, -4.476038614085095 56.00908332698367, -4.478133387071414 56.01047927867954, -4.481540770087216 56.01107582475262, -4.483029039570817 56.012016538117365, -4.481101162275469 56.01372709287916, -4.481691478114857 56.014425247055605, -4.480566507539272 56.01489728140624, -4.48087846348283 56.01650880979248, -4.478792073296832 56.0192561114115, -4.479362467493255 56.02014342117161, -4.480955546834696 56.02046189499108, -4.480357937143649 56.02190298158075, -4.482279461298828 56.02308664788773, -4.484560615437427 56.02312158584421, -4.485576463583561 56.02269665689506, -4.48731610062699 56.023794035533726, -4.488549972025508 56.023265816895915, -4.486340340167447 56.02534164726322, -4.486156474223196 56.026226161092744, -4.48707543289211 56.028535470443956, -4.48898855230587 56.03007871921627, -4.491810233843966 56.03127103150476, -4.48994147465429 56.033160275466784, -4.491917225996281 56.03367760536389, -4.487967247885818 56.036157117128646, -4.486854104632315 56.03931631562603, -4.488443136493611 56.04005720963066, -4.487852166671832 56.04110273132451, -4.49892704390273 56.04774549452248, -4.486346626381098 56.05064218001883, -4.485267685317403 56.05009769659106, -4.483604812101489 56.050212084903585, -4.483034561220796 56.05083473619282, -4.48079589323677 56.05147355738527, -4.477996305883687 56.05535069257095, -4.474761146714261 56.05625770830425, -4.47610416441338 56.058218202922994, -4.478138112555444 56.05812342834604, -4.475581220073193 56.060835146673035, -4.476588748490515 56.061515965949816, -4.47788427400354 56.06142703350194, -4.478587357279442 56.05986701398754, -4.480932809379898 56.05911881545113, -4.484533794453131 56.06115845921303, -4.48701492344571 56.06027259508678, -4.488161669003621 56.0606179670345, -4.485883285991857 56.06340517576718, -4.486992681323445 56.064416407147014, -4.488355773252214 56.06412827220907, -4.48888777772803 56.062661519243456, -4.491052827415606 56.06210551156103, -4.491840319154409 56.06235923916692, -4.491696048001697 56.0633597976969, -4.492412220071786 56.06400143735058, -4.495924481830835 56.06415511487609, -4.500418180176754 56.06532239998992, -4.504126293111449 56.06799745075055, -4.505690985300539 56.06759717794257, -4.505264620134428 56.06597904308518, -4.503487619891358 56.064343386750934, -4.504942040668692 56.06274099195315, -4.502485721411279 56.06177522654379, -4.500225295812029 56.06184802866916, -4.499925601804516 56.05994868757377, -4.501502090217463 56.05899098077564, -4.504039213498445 56.05848112084342, -4.506426877400276 56.058639344488434, -4.50711771285126 56.06111493262259, -4.510744113024053 56.061544488690295, -4.512603382687238 56.06468832399589, -4.516085284211887 56.06733168004796, -4.52434336677408 56.071746774660426, -4.527377944208705 56.071954278332036, -4.530841499257191 56.07429202198901, -4.531882152391895 56.07447739029701, -4.531636413829398 56.07489587553799, -4.533452183608517 56.076116904204156, -4.53358904594015 56.0779566084164, -4.531223529634031 56.07644127370256, -4.530744432300841 56.075507375947694, -4.529636293806951 56.075763787125474, -4.529137611788423 56.07747271784381, -4.531201705083125 56.07806852601978, -4.530626930980475 56.07984193881204, -4.533188170953433 56.08041852942341, -4.533475597035221 56.079900318983974, -4.534172857581379 56.07999385383373, -4.533930647692624 56.08046619799244, -4.536305140731393 56.08137911155353, -4.542557684939123 56.08235593213915, -4.541851237654862 56.082855834154564, -4.543633902077674 56.08380776463033, -4.542641612042314 56.083873156615596, -4.542841261656957 56.08421957002047, -4.542347262205991 56.084041004098, -4.542237931339317 56.08464987653442, -4.544928834864597 56.084034848080876, -4.544528880651176 56.08519142288909, -4.545745644497692 56.08633475120203, -4.544170495722166 56.08682562193172, -4.543064118073095 56.08833143102466, -4.541472968136568 56.08833724990466, -4.540983712868064 56.089695508252895, -4.53747954101212 56.09116972500164, -4.536770604435684 56.09016867185895, -4.535848260130532 56.09032244787343, -4.53431207165117 56.089697893497245, -4.532991161602219 56.09014744480666, -4.531034801869541 56.08924386312776, -4.51616274258109 56.09291844584967, -4.500512864676091 56.09406342581339, -4.500694048826805 56.094625983979704, -4.499226882890927 56.09630046684772, -4.502056859521996 56.09828334703493, -4.501919655898138 56.09988595137041, -4.507763563196805 56.10023470795146, -4.515912993152769 56.104122343420066, -4.519356805277054 56.10710794863146, -4.520746386712521 56.10720540645914, -4.521319857472093 56.10859579129881, -4.520021999048204 56.109655893389906, -4.521789829488376 56.11282843477608, -4.520517840776802 56.11477780360082, -4.516950104373337 56.11729527902203, -4.517407717924708 56.11789711731932, -4.516874696077722 56.12205135361348, -4.515101370105921 56.125448921108806, -4.515728658054537 56.126676453127054, -4.514805050379596 56.128304080037395, -4.49653154145896 56.12246441289347, -4.49487596569828 56.121248587386496, -4.478801641462358 56.117734643232374, -4.476491200014191 56.11557903232736, -4.46188313910242 56.11908880246813, -4.44570663744412 56.11516818461119)), ((-4.597537065999147 56.089550607599726, -4.595278788248844 56.089367430577276, -4.591041167166654 56.0863292595244, -4.588793714806173 56.086645581106445, -4.588853465463709 56.08617011903086, -4.590091772251175 56.08618258174879, -4.590571133588747 56.08581365361209, -4.589676644306891 56.085460758424524, -4.590053316675272 56.084927376600994, -4.589087939861735 56.08488355418273, -4.588477221751875 56.084319624231135, -4.590789741394444 56.083950673753485, -4.592029863793554 56.08330943301651, -4.593678206769453 56.08328761085768, -4.594830904039805 56.083391542212766, -4.596510170608733 56.084509702577776, -4.598462932314493 56.084583936932766, -4.598616133590437 56.08515745406444, -4.597882552659471 56.08549331655784, -4.598949315520116 56.08670123280582, -4.597862884153278 56.08758281996335, -4.600628769156932 56.08917788716492, -4.598534081871693 56.089721860301744, -4.597537065999147 56.089550607599726)), ((-4.563014705967472 56.08727134065478, -4.56370206503275 56.086105417756926, -4.566331646845036 56.085836875884546, -4.566362936788837 56.084980715713996, -4.568275417528898 56.0845460720722, -4.569929684563083 56.08332706723283, -4.57070325244679 56.0834590190402, -4.572146521450775 56.0826063345082, -4.572855224740324 56.081324747883976, -4.5760104260463 56.08012371056075, -4.579514333026496 56.08018211280285, -4.582039594624121 56.082317436071584, -4.581149758088928 56.08396482356417, -4.573238996093037 56.08531461328806, -4.568440923827152 56.08791531233465, -4.565728673712388 56.08871207762658, -4.564601391809028 56.08857099702737, -4.563014705967472 56.08727134065478)))
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>