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/S12000019 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S12000019 geosparql: asWKT POLYGON ((-3.3690392467802615 55.82411169647115, -3.3614978625009977 55.82574090492534, -3.356004759196049 55.82477273880383, -3.3506751840565583 55.82713631877632, -3.3495608415764724 55.83022164091877, -3.346482677369135 55.83192680539679, -3.296537328703943 55.86532606426418, -3.291686489362869 55.86697652873357, -3.2885982604208417 55.86569611017893, -3.2840297887935987 55.865893076750936, -3.274076482390557 55.87122345383151, -3.2691589388670343 55.87577974454791, -3.2650895249523937 55.87607595477952, -3.260780316498207 55.8725743684396, -3.253186611595336 55.86978372030857, -3.249606881805243 55.87316641186752, -3.2492433541915675 55.875235851981365, -3.244167913715579 55.87721206989027, -3.2397523061468965 55.878040131634144, -3.2398369687260855 55.880245249148935, -3.23720195612422 55.88248492056035, -3.231596326635128 55.883922193577355, -3.2270940384690947 55.88370665362849, -3.224575088662332 55.88235419206348, -3.211740841574675 55.881981668708455, -3.2090885062943872 55.88325765003984, -3.213449541968267 55.88448529128255, -3.213948734791652 55.88644459699586, -3.2069356131355646 55.889660310942105, -3.2007432067651966 55.89039477585286, -3.2012845940343637 55.89482116544956, -3.188425975855767 55.89231144456189, -3.161737935377612 55.88946349223269, -3.147754359204421 55.889805861146804, -3.138286102076553 55.89118857950259, -3.1209644881712597 55.89695537812226, -3.112099157624599 55.8987097435779, -3.0953354037158447 55.899500692923674, -3.0952369205952954 55.901564662083096, -3.098001292908041 55.90396693874444, -3.104917306678125 55.90688468265943, -3.106412011267728 55.908200185271234, -3.1145986681520847 55.912977545704834, -3.1176411718122248 55.91307402172403, -3.1136218932047472 55.92226657375678, -3.1116694148220643 55.92969452053823, -3.105335266729656 55.93005973027424, -3.09862418855122 55.93136515385335, -3.0921842770650563 55.93202355849856, -3.088572977281586 55.93138963270952, -3.0847276383745528 55.93016380943512, -3.082784822568222 55.92678630225298, -3.0804114793551958 55.925525747463844, -3.0790481619221715 55.92288166257942, -3.0792113474840863 55.920407444015346, -3.0759213869316695 55.91902018109962, -3.071307913792079 55.9197603429229, -3.0610606640267637 55.91595205377676, -3.0533753748765293 55.91050107152501, -3.0415103438237447 55.90997662244263, -3.034447412364552 55.908409829725095, -3.0301694278735534 55.906100472130476, -3.017740824803447 55.90163843082793, -3.0095910070188787 55.90166331272542, -3.0074907187577895 55.90652537115347, -3.00327571631491 55.908933751489336, -2.9888018514945816 55.91283274770982, -2.984238061087082 55.91241370755991, -2.973996070305222 55.91285903816486, -2.9688788479377766 55.91249869077127, -2.96596799760955 55.9107289707863, -2.966994053421132 55.90265937665833, -2.957433471332138 55.90047692018447, -2.955596733330836 55.89953875279738, -2.962200036872847 55.89539811409468, -2.9498776804848523 55.89175053226635, -2.947269613242817 55.888987063101666, -2.9490993387518407 55.884563886361306, -2.9483350041697567 55.8808848806516, -2.9406606511557185 55.87831282089624, -2.9414862760584337 55.87643306876375, -2.9376356682212617 55.87529444054684, -2.9362500562896474 55.871978592931995, -2.93582593150186 55.86730225938551, -2.9371874361786072 55.86352165946817, -2.9314353962458153 55.86138998306478, -2.9273063910195143 55.8607598596029, -2.923735290191492 55.86263411196724, -2.9190264336588423 55.862017046039526, -2.9177792914745684 55.85946914856567, -2.9140459935383505 55.856762683702506, -2.9103992685053375 55.85869280367856, -2.900853290553703 55.86040376021264, -2.8957441640687613 55.859857992521874, -2.89560790897201 55.858796867449996, -2.901037040316335 55.85792255427807, -2.901619243552507 55.85546618418205, -2.8983094130395686 55.853118236248086, -2.8990429303989584 55.85091956485015, -2.8940050467204275 55.848133094376, -2.8893673869669096 55.84724301577233, -2.8804450386211298 55.847417772670454, -2.8800905798865943 55.844879186833374, -2.8775203057032934 55.84294057182521, -2.873602926138619 55.8439280792687, -2.868920511309399 55.83856289694245, -2.8651737044381553 55.83995863389274, -2.8618686528548922 55.83673278642204, -2.855364358353102 55.83363423935857, -2.860157891483428 55.830550276104105, -2.8564535846734804 55.82842152999531, -2.852565156808519 55.82726695863511, -2.84988119398501 55.82423500171113, -2.8466393419394813 55.82268942815584, -2.8462391937735942 55.818964169098635, -2.8478801184518967 55.81853597732925, -2.852334954714128 55.823674384835954, -2.8557058711717827 55.82526382605717, -2.8641863610466207 55.82576253381765, -2.8704049386556743 55.82409953953364, -2.8740960704253933 55.820578979045585, -2.8758305708390064 55.81701117164106, -2.875068190633546 55.81425628674684, -2.878504478916918 55.810245859700885, -2.88688750972421 55.80595361907693, -2.896955584844198 55.80981163140146, -2.902891447553536 55.808788791941055, -2.9050275541022716 55.80629760636771, -2.9157545953221873 55.80287019245268, -2.9194823611606093 55.800820695402436, -2.9231226071864596 55.79656762270039, -2.9237545950187926 55.79422661924439, -2.9270599211989574 55.79317832174368, -2.934848210176952 55.79325947256161, -2.9410680162389293 55.79456348447022, -2.941111102144864 55.80309299705123, -2.9392484748087133 55.80524129349512, -2.9407307939684246 55.806177919340485, -2.9435890221411807 55.81161461620283, -2.9484656401116713 55.815695096346644, -2.9497952727739634 55.81878570586393, -2.95279208742345 55.81804910716469, -2.9603270807997846 55.81418697909819, -2.9657012215309257 55.81255807055694, -2.968015267641272 55.810381563891596, -2.967022198609835 55.808559929646165, -2.9698796921889388 55.80592089473004, -2.973531938440298 55.80593245284847, -2.9818359423625336 55.798949421551804, -2.990149975287455 55.795431422554614, -3.001217788533729 55.78983722821175, -3.013750345083941 55.78728504467142, -3.0214450768852474 55.783339631656276, -3.025104427100803 55.78272958672834, -3.0377894067838898 55.77652019562453, -3.0399451235385895 55.77629258052891, -3.04372608166042 55.773407623240246, -3.0503691808502325 55.77057447056293, -3.054181998250715 55.77039360265249, -3.060280550781563 55.76780711978077, -3.061891625822317 55.764922326273435, -3.075257134773024 55.75976967015846, -3.0790204354965387 55.756111032722046, -3.084048804448712 55.752989175675204, -3.0853740263329517 55.75079845431879, -3.089646832283733 55.7489050892084, -3.0956441408139344 55.74518108360396, -3.103373834693259 55.73712817514539, -3.1028348542885205 55.73411379474608, -3.1037638304526753 55.73204328722692, -3.102640035653754 55.72889492035967, -3.1005048160404014 55.72827609058448, -3.095768982002205 55.724912848539184, -3.092324619840437 55.72386519923259, -3.0918783136897856 55.722134979384855, -3.1025963265302767 55.71859320104343, -3.1074965268877675 55.718207657240086, -3.110553825478207 55.71655814135172, -3.1203362463959223 55.71491025649645, -3.126868126198462 55.71041154852833, -3.13438698251372 55.71146098889365, -3.1386471193149363 55.71619746258722, -3.137259930638698 55.719952884514676, -3.1451938839112312 55.72037520429078, -3.1454631094548784 55.72255443610318, -3.150582490521858 55.7247485095264, -3.151129294414144 55.728184928176155, -3.1458526738981685 55.73395366765669, -3.144313502640528 55.73659181519757, -3.1439927655032434 55.74005430196348, -3.1513436806480115 55.74465820765178, -3.154618276372055 55.74496443088687, -3.1599464528539576 55.7473673561123, -3.1615646534802586 55.74955356765537, -3.1625083595741184 55.75450028424418, -3.15399438950257 55.7610628632832, -3.1569565211505877 55.76190744934923, -3.167325603178287 55.76853972343885, -3.1699625491995747 55.770630802539834, -3.174364349087285 55.77176587676859, -3.1741582461859346 55.77360992699654, -3.1675718907154597 55.77795079841882, -3.16778479105638 55.77952216862967, -3.181038320567198 55.78134450477449, -3.1767596482566325 55.78438860237035, -3.185647864948603 55.78879600597862, -3.183401782798258 55.79250899290631, -3.184165992085957 55.79435628816433, -3.1887191136073407 55.79580210426068, -3.185093831244647 55.80174341029638, -3.1913214752369954 55.80151972918751, -3.195596393923284 55.802126990957895, -3.2018909912227764 55.795845698085, -3.2115545846590834 55.78970077887181, -3.214328867497581 55.78398814094084, -3.217584575966056 55.78515096307626, -3.2201251799533654 55.78449587009414, -3.2257108363906224 55.78702825842155, -3.231171267745367 55.78207551624058, -3.2360353334599554 55.77918090985652, -3.2455727823844223 55.78127176463646, -3.25066810507748 55.78170071270071, -3.257421587506034 55.77830801362655, -3.263348155725767 55.776641482802894, -3.2654017548118084 55.778367145510884, -3.2743705463180555 55.77535051811023, -3.292778247637416 55.7805650229187, -3.28981723294372 55.792865373287604, -3.2949249715845075 55.79495031686831, -3.2987771123886884 55.792654251909646, -3.3021257167021854 55.79262870831951, -3.305002310307312 55.7938957468661, -3.30315845077697 55.79666499362046, -3.3071607178859335 55.79766572796442, -3.310627463328995 55.79535003150145, -3.3165541141359767 55.79466937087189, -3.325263366993997 55.792750741549995, -3.3273928582558794 55.794165541526056, -3.332713389999336 55.79447638022296, -3.3364854836046876 55.79177106865829, -3.3422415654976856 55.79195548199953, -3.347273395926132 55.79444073932645, -3.346148710922377 55.796102936333966, -3.345456361287753 55.80058636153125, -3.3481094229878443 55.8028477731433, -3.348166410546183 55.80463716260407, -3.3503373608649123 55.80784822679004, -3.355196118443378 55.81276307743122, -3.3601545541458893 55.813548538024975, -3.3621111488323656 55.81725144974284, -3.3666115590916403 55.82225166549775, -3.3690392467802615 55.82411169647115))
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>