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/S12000005 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S12000005 geosparql: asWKT POLYGON ((-3.628353303713719 56.13277597268054, -3.6372720874770907 56.12965854356988, -3.641570775656288 56.13017659471997, -3.6507329715933254 56.1291747399973, -3.6541603224452546 56.125095185862264, -3.6605531908158886 56.12418984163832, -3.663887789367374 56.1229507109752, -3.6596514368507314 56.119837491112186, -3.6554571126414745 56.1183967152747, -3.6510223339914054 56.117756106210685, -3.644298380818209 56.11559397115634, -3.62923778195219 56.111456099053804, -3.6287929548644287 56.11044658409547, -3.63493887834401 56.10883953882283, -3.649007440262842 56.109195220365095, -3.6532970744949393 56.10756352112073, -3.657997198124001 56.10950162959765, -3.662049169913451 56.10934024972979, -3.663677897006311 56.10814113262198, -3.66979945867674 56.106943296934574, -3.6724598147862078 56.10780949110537, -3.6747406037309474 56.105932845717824, -3.6750280342083133 56.10133357526675, -3.6796405700742465 56.10210205283796, -3.686177636456547 56.101871902342545, -3.701918678712611 56.103010069796525, -3.7095286336767828 56.104563815135265, -3.713735693901295 56.10465012288809, -3.717447581661446 56.1027536949822, -3.717678702580139 56.09545112939838, -3.7187269167871713 56.09235250564844, -3.7214522073817466 56.08779277208893, -3.723319577544404 56.089368923242155, -3.7287227312284568 56.08741617200639, -3.7306887174840635 56.085594970220406, -3.734377426422413 56.08531402162833, -3.736346042742893 56.083312120617094, -3.7357811751023444 56.081223577670926, -3.7399183080252585 56.07656897810869, -3.745083194659554 56.07242442960466, -3.7562228856067588 56.074668336904644, -3.7655510706025956 56.07922512020908, -3.7718670700183257 56.08365470289173, -3.774948676702764 56.087060218511944, -3.788728779473467 56.09776147138048, -3.7936603754202336 56.10275461006195, -3.7947241539432777 56.10523003717078, -3.797236553163383 56.107209143465866, -3.803070690396266 56.10865443928086, -3.8121198635093343 56.10225765439047, -3.8143856568481347 56.10144073055906, -3.8215534065197145 56.10072500484623, -3.8270946346471444 56.10100893294481, -3.830034483530159 56.10204010513932, -3.838092255706199 56.10360438920769, -3.840585246621279 56.106348510697934, -3.8400025762311434 56.10879698908738, -3.834496471440486 56.11481615134346, -3.8365235056132114 56.11732372786945, -3.848318117332499 56.11962256972594, -3.8530949820157048 56.1199059218583, -3.856042065340124 56.11907351857793, -3.856955762793734 56.1170037679035, -3.855422571705362 56.114225782387436, -3.8562679494780734 56.11073898065336, -3.859008374221803 56.108603910376694, -3.863684613145179 56.10673351660378, -3.8680906880769403 56.10712788157761, -3.871826325951618 56.10981035863686, -3.8693253096708777 56.112393167536965, -3.864708834654664 56.113879171842804, -3.8633800785290373 56.11689979876238, -3.865441573180186 56.11967779497373, -3.869933058092894 56.12077630923562, -3.8767999370243023 56.119476933131374, -3.8790935321821034 56.12108652768946, -3.8799295562050804 56.12440596700862, -3.882944777910591 56.127735225783226, -3.8794994236448366 56.130957175616686, -3.8779436105439684 56.134887241339285, -3.8703868102341192 56.134697273461526, -3.8575024538593845 56.13531943381985, -3.86000313472407 56.13751577593479, -3.860397217171192 56.14140723794593, -3.863752039253224 56.14208362428238, -3.868760521323463 56.14684891163912, -3.873609638580056 56.14824645201042, -3.8753564933722515 56.14990223490556, -3.8734856170671814 56.15125607840033, -3.8641885040966306 56.152360941514694, -3.858896630743857 56.15216389425896, -3.85434429034268 56.15345085405238, -3.856770278486526 56.15574540864748, -3.8574780172733742 56.16083362922123, -3.8563517592688465 56.16252196503667, -3.8581799349900447 56.16452982304289, -3.85339792835408 56.16906786421651, -3.851723411783549 56.17259308463002, -3.8480439081416935 56.17613929140799, -3.8484719269867487 56.17934543386342, -3.844090460052784 56.182219075578445, -3.8411933291865314 56.18850766579618, -3.8419740509157263 56.1906751845448, -3.832521504778474 56.1907662718513, -3.8294563393114047 56.19655648555638, -3.799246992091388 56.20574062523373, -3.782071334734431 56.21343882221405, -3.7808847259141696 56.21723453441641, -3.7751048241753753 56.2155249052351, -3.764265847896938 56.21302012580428, -3.763298829205126 56.2118029217812, -3.755315341275693 56.21035295064856, -3.752194148273115 56.21191577446402, -3.7485463004753385 56.208116950475834, -3.7482854835714767 56.206282995696085, -3.745751472911947 56.20338491357935, -3.745017717869865 56.20107243535001, -3.741131632691256 56.19748787267942, -3.741235698800204 56.19195999958573, -3.736626575764096 56.18910891701227, -3.730860211553459 56.19239327073258, -3.726334294750331 56.19317100781825, -3.7179626447594223 56.191571273913006, -3.714000271365176 56.19342798059149, -3.7108094434344743 56.19216474426645, -3.7062527972482218 56.19335090965514, -3.6925370606484935 56.194438062075754, -3.6864589157323686 56.19586000153348, -3.675115426484298 56.1929815621398, -3.6669412786376863 56.188635005310545, -3.6623567350374104 56.19292013716808, -3.6596185987034593 56.19408010415997, -3.658934116969514 56.19614259379629, -3.650557390265888 56.20300310209971, -3.6456408674832437 56.19905646306327, -3.6380681219720854 56.19686110244215, -3.625950589992444 56.20097394109002, -3.6227887110965122 56.20396906363637, -3.6181556575174545 56.20561756147821, -3.605809873353927 56.204463025630886, -3.6028569417327785 56.204548952855745, -3.6010304452650685 56.20222918438184, -3.596305940380081 56.199326825687294, -3.584317757934543 56.198021724156476, -3.580221096117541 56.19592117107216, -3.5754406802245415 56.196118726639305, -3.5714112706946537 56.19292261610071, -3.5799521520956747 56.193025845654674, -3.5850363709546005 56.19164706807266, -3.5853740297330123 56.189479868671626, -3.5823444153331256 56.18442920133312, -3.587522047767138 56.18362412759775, -3.5882246407476686 56.181327362040896, -3.5979817825404714 56.18109739635606, -3.5971804801760805 56.17740021952651, -3.594933709603278 56.177684445100894, -3.5914106710611606 56.17414539612954, -3.5933453054549136 56.172980145026486, -3.6052840402298907 56.171387077629504, -3.605908150645438 56.170211692730476, -3.6105058274589825 56.170576046906795, -3.617668247817486 56.16827109996054, -3.625888358888231 56.16982646655294, -3.62703133114169 56.16771590484826, -3.6331281200094754 56.16409419527717, -3.6446825371982565 56.16174929761887, -3.648012205968194 56.15854102178646, -3.6418709253475927 56.155522652581666, -3.6366661443423824 56.15129554913439, -3.6404133918690227 56.147769229812106, -3.6447238497886705 56.146516780926156, -3.641484336388639 56.14449766423106, -3.6392420826479177 56.14059967435467, -3.6408948748899306 56.13897466104041, -3.6447915149249237 56.137625287200756, -3.6400442696162525 56.13366452006534, -3.636868255118981 56.13424133724615, -3.6319440875936606 56.132910941470264, -3.628353303713719 56.13277597268054))
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>