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/S22000068 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S22000068 geosparql: asWKT MULTIPOLYGON (((-4.872026525922877 57.81730724417799, -4.819380068688917 57.809727461645124, -4.749480299582666 57.81621506366441, -4.7430063273554754 57.80791709145164, -4.708408153779641 57.808796182908395, -4.68633990812222 57.80160070310106, -4.675849427635452 57.78996085757621, -4.683156378508697 57.78117944268524, -4.662235630407395 57.76505026962785, -4.646964561602454 57.76431905104659, -4.672907956405816 57.74791319084436, -4.657583478929075 57.7356011539639, -4.664041538848675 57.72860881915085, -4.642406378521494 57.71917758459707, -4.647623447035725 57.698876342321135, -4.625507529209246 57.693388486817526, -4.632541117503701 57.6605860518788, -4.60312871451619 57.65116651073212, -4.572170636853366 57.66230519641747, -4.497043063101664 57.65968670044344, -4.447992094230827 57.641838618922904, -4.405750735713922 57.64389885696797, -4.391549766053635 57.6320737827289, -4.376971269098701 57.64123562970474, -4.346586669787208 57.63544518524403, -4.409298038059892 57.595429571207504, -4.38074897399948 57.60751380165007, -4.345740493951385 57.62232962055539, -4.234313275721117 57.66934292175529, -4.232910768632263 57.66947601940112, -4.1655238481207375 57.67585086746189, -4.165162559491771 57.657576954053056, -4.165149636535798 57.65692185523987, -4.086704916009213 57.66452267606985, -4.086013937063735 57.66479226341217, -4.037039875281006 57.68381129241441, -4.036022271712934 57.68370590916695, -3.998419052741314 57.67980550263993, -4.0024317413190404 57.66817419487607, -4.101996640851275 57.607483343817364, -4.102036456626941 57.60745901778746, -4.112945319503814 57.590094281108, -4.091332622526812 57.57413172013259, -4.140005313802467 57.57742070656897, -4.140539041767565 57.57745755106079, -4.174447119720523 57.566206019808284, -4.174540454042817 57.56606633015131, -4.188238266110184 57.54562835268349, -4.193448370829736 57.537532018625974, -4.234407654281494 57.50077634470511, -4.382971590119946 57.511602874903616, -4.329923322325966 57.48463292400092, -4.28866376961846 57.4811541618987, -4.237584922602462 57.49553001116082, -4.222387417586727 57.49636671974849, -4.217658830332558 57.49438929567949, -4.192132692665565 57.483707774252984, -4.179381686750681 57.48496304979344, -4.178921919840055 57.48520622840877, -4.164834997970212 57.49265471280941, -4.149191676799453 57.500920753077104, -4.149160018256448 57.50122762298821, -4.147384794063439 57.518427309423515, -4.1135936631086665 57.516115853411904, -4.112773221945707 57.51605959220693, -4.102376205717515 57.53506688393378, -4.040713332154452 57.56014847928375, -4.047364287559521 57.57677331057767, -4.076374402748503 57.58288564254314, -4.059750795322871 57.59018216944152, -4.058072867024152 57.59091837437261, -4.016831694765212 57.59271260944453, -4.016389750268559 57.592731763392884, -4.009522203202508 57.5990284584879, -4.008497582523354 57.59996769315711, -4.005451122510832 57.60275994468945, -4.005173314498213 57.60301454702158, -3.93052259491452 57.58591134205334, -3.9301177622272663 57.585818424155214, -3.869456959896956 57.589641752611925, -3.8658415694127775 57.59135028143282, -3.819054193576179 57.61343700092536, -3.811589371178955 57.61695683085704, -3.7973952703804947 57.62364658429178, -3.7623508345282017 57.63067518008398, -3.738752921414896 57.59817922479246, -3.7486823634130575 57.59089168614251, -3.7123380740215284 57.56713987784093, -3.729730836958104 57.56096695420273, -3.711870839620443 57.534107173098505, -3.722307602410371 57.52865192167044, -3.69088575165498 57.520226715122696, -3.7053446369668017 57.512326388490195, -3.6827083110193897 57.4898314251017, -3.701302466017744 57.4372713748894, -3.6593788835271814 57.4302921462316, -3.685464659055341 57.423271220973575, -3.7149615772496554 57.431044526115684, -3.729833743556324 57.417701323586044, -3.7209551565734174 57.399955151975, -3.757888001999955 57.39435779967598, -3.780157346330543 57.35977812325374, -3.8378866573819845 57.344276277985195, -3.84614048506124 57.33390390931442, -3.8616711547252547 57.33482504627624, -3.9134047130215275 57.30473115894251, -3.953717666939238 57.29265533538536, -3.961474723593419 57.272839415019845, -3.9752364959736775 57.27491502969979, -3.997276128189557 57.2564526978922, -4.034571791207509 57.2521073606747, -4.064447700109614 57.19200261751692, -4.090641976091573 57.19571112577211, -4.112092281326567 57.18119085809728, -4.120149625101898 57.188396030428606, -4.120379301550083 57.177681511078134, -4.157715005774984 57.14694320507062, -4.138164416255003 57.13272160602314, -4.151004340471621 57.122761551186464, -4.168155248057037 57.12845940696601, -4.208368720926626 57.12396530342114, -4.279711143965457 57.094472009637556, -4.301927175212972 57.0914777401221, -4.311119745950122 57.09860509804632, -4.293778124620985 57.12013969120068, -4.300144952414903 57.1375545864747, -4.32104822795119 57.1427675577743, -4.371172534450768 57.14259329215906, -4.379722349719242 57.14805660113165, -4.505402932336052 57.125540823545045, -4.5416892339302555 57.13447955801493, -4.5689340498720075 57.152948790373664, -4.644825997921742 57.15250314429456, -4.454149335652911 57.28165831524449, -4.368641935951118 57.35044349184299, -4.333530356835292 57.38170293487407, -4.347658118923353 57.38734128821232, -4.308742707086125 57.42729977990317, -4.326194738147553 57.41941472600101, -4.328063692641081 57.40840801057863, -4.358154389051617 57.395960126018714, -4.407372259293325 57.35183263998171, -4.4552241908477335 57.33426196585447, -4.441236781080628 57.32418086044435, -4.518879038426924 57.26220384822935, -4.533224911840826 57.27480459704477, -4.525692083550851 57.29318967818038, -4.562511312010915 57.29229137668671, -4.569866456493071 57.30231387825106, -4.608492241019088 57.28882889561978, -4.611020380597675 57.31770442122067, -4.670827319913322 57.299085169454905, -4.688961145735247 57.299830746691214, -4.687846066969285 57.31457514111805, -4.7396035646126 57.32957130599543, -4.761950075905418 57.31228535218657, -4.786473679065994 57.24856420028465, -4.829529902386299 57.22887151195981, -4.898102026620585 57.23140507005663, -4.945843000539903 57.207455655297565, -4.983320872782936 57.21296037517879, -5.00651536006972 57.20811942493928, -5.04335398691096 57.21840331669977, -5.080364074062031 57.20586037424365, -5.089761094294371 57.194843346677025, -5.111784801080019 57.20686454997359, -5.154166763920687 57.20430502956677, -5.1622390561483 57.197027678253015, -5.204744575884722 57.20101836073327, -5.22165701667071 57.17427504845995, -5.251545450725224 57.17466306083862, -5.2332052455701845 57.20622941672745, -5.249719718881162 57.215465635828174, -5.270450005869229 57.213641466073746, -5.278647684207325 57.22312647675997, -5.259690806066393 57.24077117286194, -5.230941638584874 57.24511020318062, -5.231807383053714 57.25215287488371, -5.173149256755634 57.25734373017036, -5.136587217533851 57.268769849910655, -5.111000727158027 57.29015126130017, -5.0798368962814076 57.29029007610862, -5.03483613713908 57.3040134742967, -5.029126428842654 57.32603074987908, -4.954116517203587 57.33454976280666, -4.979296715187992 57.34102884709114, -5.011151460787803 57.334010729090316, -5.046804635593139 57.36572286660093, -5.095835822136621 57.36623318084281, -5.107761335596095 57.380167347962825, -5.096840344712262 57.41234913677786, -5.024577112917069 57.422950287069284, -4.998898056903167 57.40421405632864, -4.999006770913499 57.41837049576066, -5.016505403731032 57.42691974873141, -5.061420825262938 57.43461400739307, -5.086081141933315 57.42192917706123, -5.116692947950174 57.4218355520657, -5.178167755325642 57.43058775093902, -5.199542191143968 57.426836822893094, -5.135452319960625 57.42008893597912, -5.24009501199443 57.412765026520134, -5.259241966290267 57.43364573847585, -5.233871446264587 57.450389474208805, -5.167261343873719 57.45117426541962, -5.153768588716043 57.46273922115047, -5.119314215554093 57.46044252542957, -5.110084535141555 57.47879980769049, -5.074901968599183 57.477110972599064, -5.045654038250777 57.460295212788125, -5.027639659007725 57.464288787429176, -4.9955049722842375 57.481262255094926, -5.017607550067345 57.50321304723156, -5.006805285955722 57.50909767974629, -5.021494291164885 57.517096065826834, -5.020010654606149 57.52884249321725, -5.093401759088886 57.543106462782866, -5.11718045685474 57.53167213725164, -5.137121636718336 57.53853875691834, -5.190502699223644 57.52111940580494, -5.220234947641856 57.53167113481289, -5.23605383053611 57.56870340411291, -5.2564241535304435 57.56416148888672, -5.270918094855658 57.54842504176796, -5.313415163933146 57.54250658309481, -5.361484537533627 57.56472216392139, -5.398712892940471 57.55891531098151, -5.405954462641321 57.58145349502593, -5.474363497128077 57.585781945337885, -5.476747137432638 57.59934444647518, -5.42395019006231 57.61904500701628, -5.410790860030851 57.658843514183026, -5.325926287029551 57.622932535958924, -5.327333864719202 57.634257584663466, -5.387103339162895 57.66218453693406, -5.352411427255508 57.67563530673068, -5.305757558749874 57.668522771911974, -5.285699129076488 57.672005498537075, -5.271929611967617 57.675385022846875, -5.281578328141508 57.68143487355253, -5.266124861604699 57.71107112800226, -5.238450325761148 57.69192754068572, -5.170244646242106 57.69317102994194, -5.1558081298884675 57.68293537953445, -5.1263026783025865 57.69552378418452, -5.083883725436383 57.69906434039384, -5.082627999768279 57.70956570758653, -5.048882907430036 57.70446104715409, -5.0136967381460575 57.7129979540872, -4.990133905373383 57.7068706618775, -4.9747329561477756 57.71684135535552, -4.901080515669353 57.72722455861258, -4.9166695191879946 57.74744661372455, -4.984667484216342 57.779302945772415, -4.957714957312735 57.79306570712235, -4.928994493186651 57.78798144365133, -4.920241980598746 57.79877932895296, -4.903444263480913 57.7983746198482, -4.872026525922877 57.81730724417799)), ((-4.454564592534499 57.33417347994309, -4.454747311276572 57.3341327098097, -4.454822028532567 57.33418912223843, -4.454564592534499 57.33417347994309)))
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>