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/S12000038 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S12000038 geosparql: asWKT POLYGON ((-4.393200496429333 55.88915139111506, -4.383331268407431 55.88685692501737, -4.368146737120579 55.8801333424006, -4.3640099492151085 55.87804864971318, -4.353448310316143 55.87374384400452, -4.356271448091823 55.873190145038805, -4.354884426954104 55.86522750442167, -4.361437545372265 55.859910631634285, -4.3646861036984275 55.855329143767335, -4.376559148266514 55.856466408237104, -4.3808869600345695 55.85634676959743, -4.379230615659061 55.852673240973736, -4.373934982224259 55.85030572441056, -4.374893720094416 55.848658651294386, -4.368600076695693 55.84688810773755, -4.370254289968633 55.8394502945966, -4.37402882983707 55.83756822381792, -4.378607635004477 55.828882959854276, -4.377573001716871 55.82679171599513, -4.381428541393854 55.82315256694581, -4.393487599891243 55.81634557485193, -4.400571630722787 55.81094951933075, -4.405372527660124 55.813767710155325, -4.407806774232576 55.81294174530586, -4.407702906933892 55.81110484685829, -4.4172540182932805 55.808043969673435, -4.434786906880783 55.80826506298603, -4.437814995854491 55.80579803611924, -4.445110839974719 55.80314456342557, -4.451412776926511 55.80274038807081, -4.45720411551842 55.80173311095967, -4.465820798224671 55.80473059817234, -4.468743232190201 55.80292380853822, -4.47272992201652 55.79779777632412, -4.480537877293794 55.79826060062681, -4.485603058314053 55.797822109395725, -4.495159623215221 55.8014492740139, -4.496202510137675 55.799119890923464, -4.498409315522102 55.798074684902254, -4.500766070414744 55.7951128233792, -4.497500730588026 55.79362877495783, -4.49576118353607 55.7907771520844, -4.5068428183066365 55.78584807247284, -4.510593134639801 55.78590089079912, -4.514330118300278 55.784461001170804, -4.517530641430945 55.78051978927814, -4.517194104110592 55.77945523997309, -4.521975380229117 55.774420838374866, -4.532275371641146 55.776758096971555, -4.53297290542245 55.77794716652634, -4.541951247999029 55.778530269439166, -4.546703403505276 55.775102562884996, -4.546981412923246 55.77274011986851, -4.550952820441794 55.76638658529277, -4.558053620760294 55.76667301393384, -4.5574555192250825 55.76993201284333, -4.5605687318669235 55.7713286879093, -4.564644774919298 55.77507356032961, -4.5646574706821434 55.77715951504518, -4.56715051767129 55.77874060265988, -4.572377237465261 55.77896473496467, -4.573274151383576 55.780547663122555, -4.572140789651626 55.783000066479715, -4.57465923782614 55.78345698721954, -4.578779474115834 55.78189286530373, -4.583091012827636 55.775297390939215, -4.585312716791453 55.77664929342527, -4.5895558082207195 55.776256115537386, -4.594528662180443 55.7742959777869, -4.597537091972453 55.77172193972853, -4.606158184197475 55.769609534914515, -4.6069683405414334 55.767391976172576, -4.610699557476185 55.76668084157972, -4.616554449755475 55.76150025748217, -4.618114730425139 55.764681262023785, -4.624098609049383 55.76414371905217, -4.629961071140939 55.764787704922675, -4.6344267309654015 55.76826130694918, -4.6346655963726215 55.770879087458475, -4.631863835505913 55.773903514966314, -4.6353212130695765 55.77558308517832, -4.63868677540171 55.77478989756127, -4.644555214830611 55.76890478228853, -4.6486717605496235 55.76243694145807, -4.650472651996231 55.760854749220954, -4.655065184220758 55.75984500281901, -4.657615144869423 55.76142844798858, -4.656666602539218 55.76449701959106, -4.6584514265799415 55.76626512637404, -4.665589288018123 55.769135928210176, -4.664625306323982 55.77099142825673, -4.6690750152928535 55.77429072765259, -4.671180568651991 55.7785163752997, -4.676785957853691 55.77899561832902, -4.676799004117531 55.78248658675115, -4.673208238797198 55.78515885582399, -4.672829339393382 55.78810363912878, -4.678301062621618 55.792632503188116, -4.6793845991384275 55.79519038447759, -4.681911912151319 55.79655818580244, -4.686162768957613 55.80385751324274, -4.68895872086644 55.804596393742166, -4.698013197619519 55.80352152753564, -4.70999220155812 55.80400396703095, -4.718517105813468 55.80472803542064, -4.728357552413293 55.809534642823294, -4.7303689654727 55.81101806523336, -4.722368177845772 55.82079101669269, -4.744926972632468 55.82428024156157, -4.748956500578737 55.82707565360339, -4.757944095906915 55.82928775954638, -4.765053184363069 55.833483137517625, -4.779111495268351 55.839458303482, -4.783728210746652 55.83979436386561, -4.783534161870175 55.84356058395473, -4.7759083320021185 55.844941908967634, -4.77118820658971 55.8446169551005, -4.767693613048862 55.84568891409617, -4.762133414390148 55.845795974448656, -4.7596775529852815 55.84698823112454, -4.753605268246773 55.84821645580924, -4.75094123425606 55.8497682371921, -4.738800330984457 55.85034936349751, -4.730011998592334 55.85262570053674, -4.724855989392559 55.85258720605573, -4.716728298567445 55.85060975263533, -4.715933248266742 55.849292525995544, -4.7100532612535755 55.84711221449972, -4.7041957477429115 55.846737775573665, -4.7033883217949155 55.842589337111896, -4.6948639121932025 55.84083494900765, -4.685136864441625 55.840391604715066, -4.681277905643458 55.838402125695865, -4.678424144227928 55.838227008661626, -4.667588809031621 55.83898484049757, -4.654758121965977 55.84021532506719, -4.643339980273162 55.83908604188332, -4.638898955197537 55.84072925339311, -4.632640406348463 55.84110971893489, -4.629476398504079 55.84657943591602, -4.62481964279621 55.84840833639777, -4.622917919129452 55.85025110810178, -4.6221338228137965 55.85438449529835, -4.624767783129413 55.855741234633314, -4.62571429533542 55.85882742312525, -4.622824773485143 55.86159910482549, -4.615461092678093 55.862305125942, -4.612248930002986 55.86209653899845, -4.608967439636416 55.86311715273182, -4.601409369297998 55.86285922452335, -4.603317736782475 55.865104639002574, -4.60908196695052 55.86789918670669, -4.608645800685527 55.87028579671835, -4.600222621883409 55.87191039174839, -4.6052242183215375 55.8754556481555, -4.601880557977612 55.876689435995296, -4.599116148167754 55.87967901100511, -4.6047223697556126 55.881687092821124, -4.6039434105643045 55.88407015061535, -4.609921034531634 55.885092225907215, -4.612316843518336 55.88771266027377, -4.618754088125755 55.889396869277135, -4.617264481389321 55.89157229342192, -4.613407759800689 55.8938752725281, -4.613378448911995 55.897433434683286, -4.6120137154499306 55.898336091781424, -4.612029352050086 55.901589489137194, -4.613914545358617 55.90311881692133, -4.615991463918112 55.90711224133056, -4.626286954742611 55.90633477407005, -4.630782768630999 55.90743781223945, -4.632805121006378 55.90955806811667, -4.635043875307934 55.913732028899716, -4.629572937744754 55.91641518441892, -4.627983022478181 55.91626484227823, -4.622829729489194 55.91965733723775, -4.617427322947823 55.92418736983246, -4.617405431703361 55.92858489957429, -4.611335012718991 55.93147322587571, -4.609874266412385 55.93629671037798, -4.596392685307267 55.93512562274173, -4.585672015252576 55.931854843691944, -4.574183918877846 55.93180795075598, -4.565399688760805 55.93255218025793, -4.555851365750604 55.93171446473077, -4.5338222745091965 55.93022830962708, -4.52780439222124 55.92912882396153, -4.520325501284699 55.92862684562254, -4.50259425208531 55.92817069918305, -4.48926790179162 55.9290673258486, -4.484523850761772 55.928361390538925, -4.474285166898249 55.925689672600505, -4.468344283890403 55.92372370525426, -4.463859614495843 55.92121157589477, -4.453003965098466 55.91356129630762, -4.446390197962002 55.908536626173145, -4.441688825182209 55.90647108811183, -4.43608600113195 55.90533010431391, -4.423216551394317 55.90372726742518, -4.418826945438352 55.90232161852078, -4.413965183586636 55.89941520293169, -4.404763923102994 55.89316634057564, -4.402091152329093 55.891845245166614, -4.393200496429333 55.88915139111506))
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>