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/S01005493 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01005493 geosparql: asWKT POLYGON ((-1.232439375038771 60.03371705035691, -1.233559907551008 60.03182890656245, -1.234263772957018 60.027002090970704, -1.233523005398446 60.02722228148371, -1.23290791865401 60.02588976818632, -1.231598884636969 60.025083003384026, -1.230371723407342 60.020756785540904, -1.227407307255979 60.02017379372269, -1.228934053637855 60.020092913695066, -1.227417151333854 60.01975181958248, -1.227702327162991 60.01906207478802, -1.228731395607884 60.018780743164825, -1.227623325094425 60.01860366392736, -1.228088678558824 60.017879053439174, -1.230636196828249 60.017866967016836, -1.231686549404935 60.01959506210834, -1.231219075222938 60.01825911605478, -1.234879591048252 60.01598310909338, -1.235048233000569 60.01492937484655, -1.237274071298625 60.013724212874166, -1.239596077691628 60.014362984182824, -1.249932113340237 60.00940257950169, -1.248195616802886 60.00768663280333, -1.254403281373696 60.00596184533878, -1.251838389465884 60.00281354590248, -1.253279592255257 59.999328721222966, -1.252910686458761 59.99660588245347, -1.254319851863816 59.996919142833534, -1.257100968883708 59.99600993336082, -1.258232108952333 59.99432816527308, -1.257572710489486 59.992555523110795, -1.256563021671282 59.992020061480915, -1.256985286949549 59.99157346864091, -1.258918827228337 59.99167412194624, -1.260076441015698 59.991204703211835, -1.259875611819548 59.99056604200782, -1.260724910605832 59.99025651804522, -1.259817869340331 59.98913799534632, -1.26043657866869 59.98871943117041, -1.26230187758838 59.98865802549429, -1.262200091212876 59.98920519858934, -1.264948299260502 59.98974132170823, -1.267037152218008 59.99092024763018, -1.268416810428483 59.990128731368785, -1.268640079828199 59.990569956101346, -1.269665741143545 59.99038706421572, -1.270112521970477 59.9912605317218, -1.274290459169841 59.99200189725996, -1.27625669769069 59.99142902536393, -1.277392893957845 59.99028588592577, -1.276933495263012 59.98916094949241, -1.272101480822223 59.98802097128901, -1.271604288442585 59.9869945827511, -1.272112335181732 59.98671004305736, -1.269616065671404 59.98450529566709, -1.27039133174708 59.983476951070834, -1.269959208053961 59.982756212781474, -1.268701054906858 59.982928840830475, -1.268579496788781 59.98194043616946, -1.269690073110452 59.98033029156126, -1.270894373860705 59.980157357143106, -1.270776312480857 59.979007343703906, -1.270081972357832 59.97880596087307, -1.269034208593444 59.979195259541065, -1.269602495809321 59.97779761843228, -1.267766717062371 59.97817356745618, -1.265986512694497 59.976035569978926, -1.264213694611113 59.97519061922823, -1.260205211808039 59.974988637978846, -1.259374614009341 59.97528030482896, -1.258559813759834 59.97726915996167, -1.257529845778054 59.97685930064059, -1.256747082624026 59.97741162010221, -1.255105439630746 59.977115030415085, -1.254281445803435 59.97631121402455, -1.254722260108066 59.97582881510244, -1.255982220740574 59.97636570241006, -1.255646350588872 59.972969593811804, -1.256980077379954 59.9734081162121, -1.257198587314887 59.97164938082382, -1.257689616379178 59.97131990450046, -1.258463646589315 59.971952811029084, -1.261125690589063 59.97146488518097, -1.260241865591429 59.968523668677435, -1.258313281767042 59.968261426828164, -1.258209990519124 59.96727311169771, -1.258739565104753 59.96681813532295, -1.261441932397253 59.96691409170093, -1.259772159729156 59.96629414145308, -1.260015573954887 59.965819597579056, -1.262554493124695 59.96601338985993, -1.263382272675437 59.96503027560961, -1.262020910463171 59.96504063189045, -1.259210374816409 59.96418082926887, -1.258935624959076 59.962850333126276, -1.256179273277825 59.961981787574196, -1.253638001399701 59.95473900311944, -1.258095562418179 59.95476412956318, -1.263254312333166 59.953850178577504, -1.258862936621542 59.9524697045522, -1.261429112584998 59.95219674515563, -1.26330229396141 59.95249455404409, -1.264304928624268 59.95087486664337, -1.274453683274394 59.95013187057536, -1.273363560981414 59.949227931667856, -1.282041213175213 59.94860195800541, -1.27977804433978 59.946479466461234, -1.281884702704592 59.946733387240016, -1.28585827777805 59.94507579621853, -1.287407190583398 59.94629638720376, -1.294318080734213 59.94621678463646, -1.292589062964042 59.93999377357668, -1.294727830129656 59.937877097853196, -1.294694159629183 59.936934079540684, -1.296884824350929 59.936577611291604, -1.297809148075178 59.93686987419587, -1.298951721905994 59.934451506275735, -1.297167932341002 59.9341905940237, -1.301291746590425 59.925277964593434, -1.308724885125088 59.926538320480496, -1.312245514428071 59.924976340776006, -1.32678415565261 59.925186015759564, -1.331482642159181 59.92637722717179, -1.340471116691734 59.92595553395895, -1.343521934253275 59.92636584157595, -1.345734647798205 59.927553132894964, -1.348730959007235 59.928016926151344, -1.354265613382343 59.92766699266417, -1.356514077635252 59.927031461755995, -1.361370850880743 59.92657017249604, -1.368073384988288 59.926871822756986, -1.367706158515105 59.92924959510176, -1.367033749812143 59.92886026040342, -1.362578669388381 59.9307514196351, -1.362642307367557 59.931164777061255, -1.363973716124265 59.93170996294289, -1.365640039082386 59.931583295062424, -1.369065324981145 59.933045392697515, -1.371995740433377 59.93230510456105, -1.37247276406786 59.93357346316133, -1.368388531881838 59.93382336038781, -1.367228808480039 59.934590028776185, -1.364870577570091 59.934399113149325, -1.36599044160834 59.93572446388232, -1.365521476848711 59.935919755095114, -1.362955858306631 59.935341697574145, -1.365541835880717 59.93672799613248, -1.365489934609955 59.93757180530862, -1.363977816055803 59.93804043455426, -1.364918439677331 59.93844903433814, -1.36377878646422 59.94002391210274, -1.364987877984347 59.940433805849345, -1.36260670260702 59.940485183411994, -1.362480556139076 59.94146332382869, -1.363937297805103 59.94202707283079, -1.36248460367256 59.94498324927491, -1.358053968709134 59.94455770495351, -1.35725712419611 59.9441138396779, -1.355697347872001 59.944249904841186, -1.353868935710691 59.94255284763497, -1.352945824806067 59.9440029809041, -1.349134245345587 59.94396628295866, -1.347652458701661 59.94286348446148, -1.34559373575122 59.94288921079649, -1.344614258730285 59.94354882498687, -1.346443427722078 59.944276244017026, -1.346309852234191 59.94469761088362, -1.344356624735176 59.945720551116345, -1.342765004819865 59.9456497792987, -1.342903883919012 59.94677289314981, -1.344316927964488 59.9477317312508, -1.341612159371785 59.94963087174351, -1.340271049176354 59.94595161663359, -1.333947970598463 59.94529130671785, -1.33119906221988 59.945807188361144, -1.330094986554339 59.94730114627965, -1.329324347712299 59.9526668984532, -1.331052239437808 59.95398665196574, -1.332962675626775 59.9542477406986, -1.335624427826071 59.95275262987532, -1.338109820401064 59.95380672014914, -1.338086376675586 59.954982898539754, -1.336263981809169 59.9574789906272, -1.335684120251218 59.9569283316879, -1.334934157353407 59.956825784821284, -1.334640328804607 59.95719245713445, -1.335769087211094 59.95805118050598, -1.33480137791964 59.9580912043416, -1.33448746928276 59.95856552828643, -1.335596814595086 59.95860703282181, -1.333347722199905 59.960930328144066, -1.334198541812694 59.961365630122536, -1.333822050603536 59.96227962593208, -1.332371371494956 59.962799977711484, -1.332994797527915 59.96380869179166, -1.332526060187559 59.96492513221341, -1.330434523432131 59.96536206424723, -1.332590841353329 59.969102319261786, -1.33793170569705 59.96987506137433, -1.342253530621118 59.9695237287206, -1.343764851290948 59.96833785900245, -1.343495348031005 59.966919373808494, -1.353875242581354 59.96868606069403, -1.358949231032013 59.967890339129085, -1.361010420456241 59.96901912494016, -1.360099116010978 59.96998433325921, -1.357718710736622 59.969972777731925, -1.359432029062006 59.973710435352324, -1.353662561812148 59.97196683492833, -1.35434553330306 59.975102810000735, -1.352840099063042 59.975990481510586, -1.355774143165141 59.9781678255731, -1.356919824371083 59.9804855883606, -1.351427981807787 59.9797128797121, -1.344965398456442 59.9753549689356, -1.340668014104335 59.974438544490454, -1.340429729150658 59.97145389115163, -1.336734059234094 59.97024199235281, -1.331351091352067 59.97155740066266, -1.328840863324644 59.97795913014041, -1.331508926895494 59.97849476395901, -1.337683129728402 59.982329788771075, -1.340515891573751 59.98957885592785, -1.342579253918876 59.99063335037251, -1.344619185381026 59.9928810754067, -1.344300784349331 59.99392370560383, -1.345637774500957 59.99407950879193, -1.346643221955266 59.99594914121054, -1.34614210628061 59.99870635827702, -1.344175386871078 60.00026292440833, -1.343243498973245 60.00219753552347, -1.340415511350775 60.00210886940321, -1.336509283461782 60.00380479876263, -1.336623885563694 60.00552085878942, -1.33177679991181 60.00952409979481, -1.331394655535914 60.006314953492165, -1.329765387976674 60.00585918068693, -1.326490336376001 60.005767949362976, -1.323481195250408 60.00724430988218, -1.321328802186624 60.01051507466984, -1.318762821034162 60.01214276870201, -1.318891839272276 60.01311306213356, -1.322880441608991 60.01477444931625, -1.32061216756805 60.01640371276127, -1.321154598319032 60.01901702292977, -1.320234622287168 60.020280259468514, -1.317214636316039 60.02220393430565, -1.317778398188776 60.02377316515375, -1.316984098882587 60.026155818163474, -1.310017002535879 60.03148985453421, -1.305550627417243 60.038328402591404, -1.30731149674141 60.039680191775766, -1.308303655173101 60.042146729154936, -1.30645664921395 60.04489674410915, -1.306522921768544 60.04885014915515, -1.304775331652147 60.05654768601103, -1.280292372702078 60.05403693541267, -1.278255293781256 60.05275077942991, -1.271345452603795 60.0444339861076, -1.267836114028431 60.044010536160116, -1.262124702222915 60.04251516617729, -1.25667899744 60.042798945991706, -1.254084094117996 60.04403246177754, -1.253706257700172 60.04486540587455, -1.25106819746612 60.04561373546488, -1.2475352741201 60.047811574560114, -1.246702806515363 60.04652279286235, -1.238565379304176 60.046241073224486, -1.240214935906587 60.044517581352814, -1.243100835831253 60.04445285198755, -1.239018285292993 60.04355568171525, -1.235709698673465 60.04212191816906, -1.236464409854539 60.03820714712538, -1.236129052966199 60.03784996768277, -1.234178277665433 60.03801057683916, -1.233284090805428 60.0360687480448, -1.236545733679748 60.03567507927239, -1.235949592880638 60.034709042568494, -1.236555336739579 60.034265623936456, -1.236267933448407 60.03381704578172, -1.232439375038771 60.03371705035691))
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>