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/S01007927 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01007927 geosparql: asWKT POLYGON ((-4.362170916822354 55.707735727176896, -4.3595861005528525 55.70631592404183, -4.360683474313859 55.70550838258979, -4.353209253855756 55.69741900865187, -4.347444337093546 55.69867985900112, -4.333614524067252 55.700282788807186, -4.333415272395099 55.700277589332636, -4.332573910920095 55.69443800634604, -4.329677703082407 55.69203035274047, -4.328831519414308 55.69184868122729, -4.326800958507363 55.69034578510736, -4.3251814842764995 55.68839466434914, -4.323266518726049 55.687896158591336, -4.317040216639433 55.68699825749123, -4.29589546032895 55.68400280687443, -4.28196561842654 55.678645309054055, -4.284283210908005 55.67720009532806, -4.292561753005985 55.6678152451502, -4.280359859905152 55.65706860231466, -4.29765994474667 55.65333039597059, -4.302420694324629 55.65183902170363, -4.305929284266752 55.65122485091426, -4.319259017576134 55.647890679862996, -4.322635483471378 55.648276177097586, -4.329424175329853 55.64697001252733, -4.330739210743525 55.64690008885981, -4.330348337542085 55.646206460682095, -4.331525788369873 55.6462290304681, -4.332223074038714 55.644858590515696, -4.3343271037512565 55.64496236682049, -4.3352405777925185 55.64428885897209, -4.336423517684843 55.64387085996612, -4.33853839373061 55.64362382816493, -4.338804124753426 55.643016570167774, -4.339852003542482 55.64326621912593, -4.340817052131511 55.642393949828865, -4.342517037196038 55.64291874078316, -4.342795374414939 55.6422573050273, -4.343990729458663 55.6423153546703, -4.344582213904478 55.64157602520295, -4.345680162483228 55.641599968685185, -4.346083811779957 55.640909163885716, -4.347370019658444 55.64142382707328, -4.34796382338017 55.640990026080644, -4.349274815250312 55.64138735174246, -4.350224259435071 55.64105458139587, -4.354196884501069 55.64158057539578, -4.355367734590433 55.641495177136505, -4.357811762463661 55.641969513011425, -4.359456431741102 55.640778432663524, -4.374976998574161 55.63596684830486, -4.372717208563466 55.63512078283351, -4.379007474805173 55.632994575107844, -4.380439773058404 55.632247735035186, -4.381990943879048 55.63293665371257, -4.382257744642891 55.633929142101245, -4.384378752031722 55.634319342120996, -4.387338555110308 55.63622114499228, -4.388441399094802 55.63502223481298, -4.387622756707901 55.63462472790987, -4.39032839782458 55.632873272510295, -4.391512026478423 55.63352429680222, -4.392998804692261 55.63211112876545, -4.394696278049201 55.63259030807455, -4.394897687032556 55.63302679846614, -4.396552031094214 55.63383934450691, -4.399971464217467 55.633380718925245, -4.398591918258774 55.63184561293149, -4.401958028030953 55.631306783513416, -4.403759740808672 55.63244891438965, -4.40479063936007 55.6328831558258, -4.405582309082724 55.63173251186212, -4.406905358329387 55.6310715836239, -4.408661934157635 55.63181342705757, -4.40999433439461 55.634106175307615, -4.412568110470999 55.633327525221176, -4.4136646296214295 55.63410588692968, -4.4166851044235 55.63284198553001, -4.420219475386622 55.63347334720931, -4.41837750713403 55.63426468354507, -4.419669226293702 55.637690646792706, -4.426902261025231 55.63774831250034, -4.429104309452328 55.63762993816613, -4.428775104437551 55.63616240425218, -4.436497933417895 55.63463400308568, -4.436775289096038 55.6355342374784, -4.436606301118748 55.636285895898084, -4.438154923966333 55.63751021099329, -4.434331237087496 55.63975111988562, -4.433325346906696 55.64073908087971, -4.433586029973031 55.64103311420338, -4.435582678590298 55.641985226302275, -4.43706343516195 55.64162600515618, -4.437512086903021 55.6411826365463, -4.4394882902947455 55.64158531372325, -4.440168894745671 55.64116685562317, -4.441146502905587 55.64157175645755, -4.442872537204097 55.64080787754544, -4.4462318592396635 55.64345533877279, -4.452999962020771 55.6428558571606, -4.453548188765269 55.64227553248736, -4.45503569994017 55.642173846818764, -4.454845502488529 55.64472132265634, -4.453694457315781 55.64512186273179, -4.452553555063949 55.64466831106292, -4.451681877693905 55.645216043746025, -4.451676181055397 55.64614193166795, -4.449279586280465 55.646450486169364, -4.448291293408323 55.647422966876064, -4.446587306190289 55.64711542232381, -4.446195078659875 55.648471471876974, -4.447676668060646 55.64903513008057, -4.448934429837222 55.65110426523195, -4.448959776530099 55.65165263975985, -4.449835273789379 55.65260860079642, -4.448112288114496 55.65249173413631, -4.447139007721613 55.65331100551642, -4.445520210279423 55.65391095029987, -4.44392626720161 55.6525176496938, -4.4420651982983745 55.652861989335506, -4.442715386140944 55.6536405563011, -4.444327467665133 55.65358288546424, -4.445050654036761 55.654215204483705, -4.444764048437132 55.6557298854108, -4.44348709160587 55.65599278079448, -4.442514319979224 55.656446472938576, -4.441898991872952 55.658354172387554, -4.441064563266946 55.65797530341391, -4.440703949252267 55.65729039268477, -4.441393782042244 55.65636886098996, -4.440714845634551 55.65465664920543, -4.43929072921279 55.655529866030115, -4.437828032869496 55.65578365079869, -4.438221980789211 55.656946282509224, -4.437922951199082 55.6580900617221, -4.439095494916781 55.658849797162816, -4.438172885607435 55.65921825503806, -4.438441224381588 55.660274518424494, -4.439687589406259 55.65986324475108, -4.4401615124040505 55.66006953284799, -4.439603893740182 55.660817652014245, -4.44203890801211 55.661521756848515, -4.441932125140077 55.661948770142914, -4.4408186854866205 55.66270795849153, -4.4419377322151385 55.663314855874134, -4.441652575385744 55.663988566692026, -4.441233459320664 55.66629919245126, -4.444191655453581 55.666078850046404, -4.444134030353513 55.66513165932763, -4.443071177942008 55.662366507908246, -4.444340157232493 55.662062594598176, -4.445651294285261 55.66054378880341, -4.444969152702496 55.659910879082325, -4.444858294582013 55.65813343468867, -4.443014656674175 55.65814320501065, -4.444220327691886 55.65719581412551, -4.444721076291542 55.656133456249755, -4.445357423709392 55.65679339053703, -4.446647372405701 55.65680085914774, -4.44706779554623 55.65644065740378, -4.449331967943941 55.65670494408515, -4.450960697768501 55.65656455015178, -4.451651730064176 55.65871688838004, -4.452968570763331 55.65867258398208, -4.453018526570939 55.66048719550301, -4.4541390951568465 55.6616242547394, -4.454478061802426 55.662974686300814, -4.467242216576328 55.66240402485057, -4.467090995864982 55.66151723436037, -4.475564675869906 55.66049262683587, -4.478011255103889 55.6604612003821, -4.478556778176455 55.65953336936173, -4.483050922161007 55.661707518598035, -4.487403737083007 55.66164629010411, -4.4890885745253115 55.66029411847861, -4.4904667419905095 55.66130550111077, -4.493371357968112 55.66183021265897, -4.492474182383374 55.66326910859513, -4.492857901233572 55.66415204787017, -4.492652372902936 55.665045449629226, -4.488928929420062 55.667385810170295, -4.494209709604051 55.672150187475, -4.491470292562546 55.672305820847775, -4.48781282534611 55.67179248503383, -4.486060661416389 55.671340941835716, -4.48494785222972 55.671695927074424, -4.48559314248366 55.67281159699191, -4.485422465806536 55.67313975495188, -4.487072136502637 55.6732686767316, -4.488099327522857 55.673719443179145, -4.487780410216473 55.67429801225858, -4.48938491504064 55.67583838019269, -4.486159482292978 55.67737397273781, -4.481258872754438 55.67559464470812, -4.479339031207128 55.67617275131026, -4.479116670382709 55.67668058486912, -4.479529232345166 55.67766993936384, -4.477757958640251 55.67908990867632, -4.47438024024479 55.67950861625506, -4.47047117912038 55.68108841828521, -4.472467341940983 55.68171333477726, -4.4778936660760404 55.68098367977396, -4.477158311261912 55.68168163230852, -4.479397012556418 55.68362280700321, -4.478661068486617 55.68431179175989, -4.479998140298824 55.68532740589468, -4.477717506579714 55.68700034488569, -4.475696611470667 55.689261219450486, -4.476936693002112 55.68974852065761, -4.476745468101572 55.69049840052678, -4.473516605852126 55.691021926593926, -4.471288344362078 55.69076122438749, -4.4707689810684625 55.69110424548659, -4.4673755456029935 55.690786034173506, -4.464909334535237 55.69129399481907, -4.463269869354077 55.69229763708829, -4.462010490432755 55.69200832539737, -4.461449943222406 55.692963326542866, -4.459763707068796 55.692718505715064, -4.459288407529564 55.693258332711444, -4.458334153068313 55.69352013286936, -4.456055926106465 55.69322420198128, -4.454755112377111 55.69405017406139, -4.453482094125706 55.69379700030439, -4.452426595461404 55.69142725605704, -4.451062662570555 55.69149944771321, -4.447946004585166 55.69000671570881, -4.44272058390511 55.691944420943024, -4.4419099703339056 55.69272454320078, -4.439940789878413 55.69355466507768, -4.437855086154486 55.69328153380309, -4.435760620428042 55.69440169490453, -4.433214556312335 55.69465893890299, -4.42467934371916 55.697137797031466, -4.422097647281955 55.695022656896136, -4.419686061220145 55.696634680734995, -4.418018004900989 55.69601801928005, -4.417381034423711 55.696571854818174, -4.426464194909491 55.70205494011775, -4.422608665083398 55.702508603502054, -4.423211756319312 55.70325169347478, -4.42214192760979 55.70422554375242, -4.420995752938725 55.70370887548818, -4.420056339695696 55.703961091300314, -4.414198545595965 55.70369894819734, -4.413035149638535 55.70444986542401, -4.411993326041143 55.70407487090706, -4.4107165123659176 55.7040190730749, -4.4103952297476035 55.70500506763217, -4.409155876976301 55.704265419811904, -4.408336605469767 55.704919646996686, -4.406367969116405 55.70500319970733, -4.404784894616759 55.70695765863737, -4.402778137485204 55.70979222446526, -4.400639873086351 55.71360182468616, -4.399382188049222 55.71448566501639, -4.398394112161075 55.7155089307505, -4.397253147990168 55.71707804680471, -4.395770461749963 55.71782692794873, -4.394441659798199 55.71812518878163, -4.392643705364715 55.71895388113765, -4.391282149277883 55.719212299667305, -4.390213886431769 55.720023144221834, -4.389691834605095 55.720808967097476, -4.388528469142028 55.72076779282826, -4.38734497173722 55.72177948655089, -4.384503348666637 55.72296898914808, -4.384249142553792 55.72162034415941, -4.383236495301978 55.721073767177906, -4.383364332838254 55.72074142843419, -4.382015849724278 55.71993531857929, -4.378046697011013 55.71842590131654, -4.377624848221904 55.71790378517733, -4.377911000811478 55.71737244934534, -4.377288966627699 55.71696206469178, -4.374795837724237 55.71654744253447, -4.374483246027332 55.71585692604721, -4.375904670551194 55.71422507992962, -4.376103493378155 55.71330446372321, -4.375499373263455 55.71135229550182, -4.374128017209068 55.71000367527942, -4.372834654809901 55.710464598101574, -4.371736336800765 55.71059817360573, -4.3707641121427265 55.710185535264564, -4.370193221446213 55.7093022630561, -4.367801530149973 55.70792383936519, -4.3662254474520195 55.70805309968506, -4.365588930605241 55.70845184836965, -4.363622088671727 55.70870095387051, -4.362579980868192 55.70765595102117, -4.362170916822354 55.707735727176896))
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>