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/S37000018 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S37000018 geosparql: asWKT POLYGON ((-3.3690392467802623 55.82411169647117, -3.361497862501013 55.82574090492534, -3.356004759196035 55.824772738803844, -3.3506751840565445 55.82713631877636, -3.3495608415764737 55.83022164091881, -3.346482677369135 55.83192680539679, -3.2965373287039443 55.86532606426422, -3.2916864893628697 55.866976528733574, -3.288598260420812 55.865696110178945, -3.2840297887935987 55.865893076750936, -3.274076482390526 55.87122345383151, -3.2691589388670343 55.87577974454791, -3.2650895249523937 55.87607595477952, -3.260780316498207 55.87257436843961, -3.253186611595352 55.86978372030859, -3.2496068818052435 55.87316641186754, -3.2492433541915675 55.875235851981365, -3.2441679137155646 55.87721206989027, -3.2397523061468974 55.878040131634165, -3.2398369687260717 55.880245249148956, -3.237201956124221 55.88248492056037, -3.2315963266351138 55.88392219357737, -3.2270940384691102 55.88370665362849, -3.2245750886623172 55.88235419206349, -3.2117408415746755 55.881981668708434, -3.2090885062943717 55.88325765003985, -3.2134495419682376 55.884485291282566, -3.213948734791623 55.88644459699586, -3.206935613135565 55.889660310942126, -3.2007432067651815 55.89039477585286, -3.201284594034364 55.89482116544958, -3.188425975855767 55.89231144456189, -3.161737935377598 55.88946349223271, -3.1477543592044057 55.889805861146804, -3.138286102076539 55.89118857950262, -3.1209644881712313 55.89695537812231, -3.112099157624599 55.8987097435779, -3.0953354037158154 55.899500692923674, -3.0952369205952954 55.901564662083096, -3.098001292908041 55.90396693874444, -3.104917306678125 55.90688468265943, -3.1145986681520563 55.91297754570487, -3.11764117181221 55.91307402172403, -3.1136218932047335 55.9222665737568, -3.1116694148220785 55.92969452053823, -3.1053352667296417 55.930059730274266, -3.098624188551205 55.931365153853356, -3.0921842770650563 55.93202355849856, -3.088572977281586 55.93138963270952, -3.084727638374523 55.93016380943512, -3.0827848225682075 55.926786302253, -3.080411479355196 55.92552574746386, -3.0790481619221572 55.92288166257942, -3.0792113474840863 55.920407444015346, -3.0759213869316544 55.91902018109963, -3.0713079137920793 55.91976034292291, -3.061060664026764 55.91595205377677, -3.053375374876515 55.910501071525026, -3.0415103438237447 55.90997662244265, -3.034447412364522 55.908409829725095, -3.0301694278735543 55.9061004721305, -3.017740824803432 55.90163843082794, -3.0095910070188787 55.90166331272542, -3.007490718757774 55.90652537115346, -3.00327571631491 55.90893375148936, -2.9888018514945816 55.91283274770982, -2.984238061087097 55.91241370755991, -2.973996070305207 55.91285903816486, -2.968878847937762 55.912498690771294, -2.96596799760955 55.9107289707863, -2.966994053421102 55.902659376658335, -2.957433471332123 55.90047692018447, -2.9555967333308226 55.89953875279742, -2.962200036872847 55.8953981140947, -2.9498776804848372 55.89175053226635, -2.947269613242817 55.888987063101666, -2.9490993387518407 55.884563886361306, -2.948335004169757 55.88088488065162, -2.940660651155704 55.87831282089624, -2.9414862760584346 55.876433068763774, -2.9376356682212474 55.87529444054686, -2.9362500562896328 55.871978592931995, -2.9358259315018445 55.867302259385504, -2.937187436178578 55.86352165946819, -2.9314353962458 55.861389983064804, -2.927306391019485 55.860759859602915, -2.923735290191492 55.86263411196724, -2.919026433658813 55.86201704603953, -2.9177792914745533 55.859469148565694, -2.914045993538351 55.85676268370251, -2.9103992685053224 55.85869280367856, -2.900853290553704 55.86040376021266, -2.8957441640687613 55.85985799252189, -2.895607908971996 55.85879686745001, -2.9010370403163215 55.85792255427811, -2.901619243552507 55.85546618418207, -2.8983094130395832 55.853118236248086, -2.899042930398944 55.850919564850166, -2.894005046720428 55.848133094376024, -2.889367386966895 55.84724301577233, -2.8804450386211142 55.847417772670454, -2.8800905798865797 55.84487918683342, -2.8775203057032948 55.84294057182523, -2.8736029261386045 55.843928079268736, -2.8689205113093843 55.83856289694246, -2.865173704438141 55.83995863389277, -2.8618686528548922 55.83673278642204, -2.855364358353103 55.83363423935859, -2.860157891483428 55.830550276104105, -2.8564535846734658 55.82842152999531, -2.8525651568085046 55.82726695863513, -2.8498811939849955 55.82423500171114, -2.846639341939467 55.82268942815585, -2.8462391937735942 55.818964169098635, -2.8478801184518825 55.81853597732928, -2.852334954714128 55.823674384835954, -2.855705871171768 55.82526382605717, -2.8641863610466207 55.82576253381765, -2.8704049386556743 55.82409953953364, -2.874096070425409 55.82057897904561, -2.875830570838991 55.81701117164106, -2.8750681906335465 55.81425628674686, -2.878504478916902 55.81024585970086, -2.8868875097241813 55.80595361907697, -2.8969555848441684 55.80981163140145, -2.9028914475535363 55.808788791941076, -2.9050275541022725 55.80629760636772, -2.915754595322188 55.802870192452716, -2.9194823611605947 55.80082069540244, -2.9231226071864596 55.79656762270039, -2.923754595018778 55.79422661924439, -2.927059921198958 55.793178321743696, -2.934848210176952 55.79325947256161, -2.9410680162389014 55.79456348447024, -2.9411111021448644 55.80309299705122, -2.9392484748087133 55.80524129349512, -2.9407307939684246 55.806177919340485, -2.943589022141166 55.81161461620283, -2.948465640111657 55.81569509634666, -2.949795272773948 55.81878570586394, -2.952792087423436 55.818049107164704, -2.9603270807997855 55.81418697909821, -2.965701221530911 55.81255807055697, -2.968015267641243 55.81038156389161, -2.9670221986098206 55.808559929646194, -2.969879692188924 55.80592089473004, -2.9735319384402827 55.805932452848495, -2.9818359423625034 55.798949421551804, -2.990149975287456 55.79543142255465, -3.001217788533714 55.78983722821177, -3.0137503450839263 55.78728504467142, -3.0214450768852337 55.7833396316563, -3.0251044271007745 55.78272958672839, -3.0377894067838906 55.776520195624556, -3.039945123538575 55.77629258052894, -3.0437260816604055 55.77340762324026, -3.0503691808502325 55.77057447056293, -3.054181998250715 55.77039360265251, -3.060280550781563 55.76780711978077, -3.0618916258223177 55.764922326273464, -3.0752571347729942 55.759769670158484, -3.079020435496525 55.75611103272207, -3.0840488044487278 55.75298917567523, -3.0853740263329517 55.75079845431879, -3.089646832283733 55.7489050892084, -3.0956441408139344 55.74518108360396, -3.1033738346932593 55.7371281751454, -3.1028348542885205 55.73411379474608, -3.103763830452662 55.73204328722696, -3.1026400356537387 55.728894920359664, -3.1005048160403863 55.72827609058448, -3.095768982002207 55.72491284853923, -3.0923246198404226 55.72386519923259, -3.091878313689771 55.72213497938486, -3.1025963265302767 55.71859320104343, -3.107496526887753 55.718207657240086, -3.110553825478207 55.71655814135172, -3.1203362463959086 55.71491025649648, -3.126868126198462 55.71041154852833, -3.13438698251372 55.71146098889365, -3.1386471193149372 55.71619746258725, -3.1372599306386832 55.71995288451469, -3.1451938839112326 55.720375204290804, -3.145463109454864 55.722554436103195, -3.1505824905218596 55.724748509526435, -3.1511292944141442 55.72818492817617, -3.1458526738981543 55.7339536676567, -3.1443135026404985 55.73659181519757, -3.143992765503244 55.7400543019635, -3.1513436806479813 55.74465820765178, -3.154618276372041 55.744964430886874, -3.159946452853928 55.7473673561123, -3.1615646534802586 55.74955356765537, -3.162508359574103 55.75450028424418, -3.153994389502555 55.7610628632832, -3.156956521150573 55.76190744934925, -3.167325603178258 55.768539723438884, -3.169962549199561 55.77063080253986, -3.1743643490872704 55.771765876768605, -3.1741582461859057 55.77360992699656, -3.1675718907154455 55.77795079841885, -3.167784791056366 55.7795221686297, -3.1810383205671675 55.78134450477446, -3.1767596482566325 55.78438860237035, -3.1856478649486033 55.78879600597864, -3.183401782798259 55.792508992906335, -3.184165992085957 55.79435628816433, -3.1887191136073416 55.7958021042607, -3.1850938312446635 55.801743410296396, -3.1913214752369807 55.801519729187525, -3.1955963939232546 55.802126990957895, -3.2018909912227618 55.795845698085, -3.2115545846590687 55.78970077887181, -3.2143288674975676 55.78398814094088, -3.2175845759660713 55.785150963076276, -3.2201251799533503 55.78449587009414, -3.225710836390608 55.78702825842156, -3.231171267745367 55.78207551624058, -3.2360353334599563 55.779180909856535, -3.2455727823844223 55.78127176463646, -3.2506681050774655 55.78170071270072, -3.257421587506035 55.778308013626585, -3.263348155725767 55.7766414828029, -3.2654017548118084 55.778367145510884, -3.274370546318071 55.77535051811024, -3.292778247637401 55.78056502291872, -3.2898172329437045 55.792865373287604, -3.294924971584492 55.7949503168683, -3.2987771123886733 55.792654251909646, -3.3021257167021707 55.79262870831953, -3.3050023103073123 55.79389574686611, -3.3031584507769405 55.79666499362046, -3.3071607178859335 55.79766572796442, -3.310627463328996 55.79535003150149, -3.3165541141359767 55.79466937087189, -3.325263366993997 55.79275074155, -3.3273928582558656 55.794165541526084, -3.3327133899993213 55.794476380222974, -3.336485483604673 55.79177106865829, -3.3422415654976856 55.79195548199953, -3.347273395926132 55.79444073932645, -3.346148710922362 55.796102936333966, -3.345456361287739 55.800586361531266, -3.34810942298783 55.80284777314332, -3.3503373608648976 55.80784822679004, -3.355196118443378 55.81276307743122, -3.36015455414589 55.813548538024996, -3.362111148832366 55.817251449742855, -3.366611559091628 55.8222516654978, -3.3690392467802623 55.82411169647117))
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>