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/S01001804 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01001804 geosparql: asWKT POLYGON ((-3.110612619126846 55.91076724522697, -3.106299097080346 55.908164359565504, -3.1054737959104 55.90670714644078, -3.104987754526129 55.90705295591749, -3.097906030574802 55.90381865527423, -3.095553237157758 55.90149441653388, -3.092384807891462 55.89972830510625, -3.096034684025919 55.8992075078233, -3.106448632626384 55.89920397208113, -3.112212070168536 55.89861825078679, -3.112022651490662 55.89811777206079, -3.117396816896722 55.89776360793761, -3.132656942443274 55.8929426883843, -3.132715373399824 55.8924386328748, -3.143779894715875 55.89014865096737, -3.147114479961362 55.88971048094395, -3.161892413865873 55.889223959984896, -3.162273285436724 55.888985180770945, -3.165230420331884 55.889897598395486, -3.182331109493127 55.891938054196466, -3.187451877776207 55.89209640912644, -3.194003205892587 55.893156804557435, -3.193849779402828 55.8941288405478, -3.196214412342993 55.89458212008101, -3.199217078787317 55.89601755232848, -3.198489748359586 55.89836088027238, -3.197379104045674 55.89812907760863, -3.195059994886624 55.89760349316358, -3.194894135263985 55.8989709069177, -3.190623181758891 55.89904045497051, -3.190792111713161 55.90036392022603, -3.189078422678569 55.90026672318192, -3.189229739962597 55.89914458294497, -3.180726450111395 55.898514556177616, -3.173846082184296 55.897870495915235, -3.173274054598605 55.89852290672813, -3.171884976643335 55.89859903590275, -3.16984260533493 55.89769295162712, -3.167480245867935 55.89944059685631, -3.163994341976549 55.89999476447632, -3.162167052947971 55.89878999801288, -3.162861924393053 55.89572692564166, -3.162100716741987 55.89550192620969, -3.163516959510938 55.89473376526865, -3.162584625601212 55.894571848908974, -3.163435365160762 55.892542069509915, -3.162049165408378 55.89270791677633, -3.161808985246385 55.892162067101964, -3.161505198922097 55.89430348726789, -3.157756721929675 55.895156479675464, -3.155970419190152 55.89477790395093, -3.154697082189325 55.89497854998482, -3.153586514123565 55.894198237327245, -3.154548571863813 55.89267066697012, -3.151800606906609 55.892211191944426, -3.149104222714704 55.89349436231979, -3.146676692889008 55.893597867996945, -3.14380392259175 55.894873578039174, -3.141523923434439 55.89673676705789, -3.141191977881292 55.89741375631911, -3.141933605988731 55.89816166039503, -3.138514617113371 55.901553897057866, -3.136717697908005 55.90131838023302, -3.136307291075233 55.90209544731497, -3.137740910673062 55.90244162640571, -3.13697582851407 55.903271789063034, -3.134663491761713 55.90276654750463, -3.13336906036195 55.90390165426858, -3.133602727970808 55.9043656503736, -3.132423874646166 55.904161605901265, -3.13168240106602 55.904521370946874, -3.131763655460814 55.90512069244521, -3.12887486746347 55.904428047045315, -3.129942509344619 55.90487662349289, -3.129800622726759 55.9054979226428, -3.126866783055846 55.90747465410605, -3.126712454904554 55.90987518327329, -3.126853331794124 55.91087127947896, -3.125700348084347 55.91027980045963, -3.122863770500509 55.91121323111329, -3.121336420130293 55.90984339092878, -3.119226617246868 55.90990751398273, -3.118232155470226 55.90926061193961, -3.116317661787054 55.90999681960104, -3.114699519535879 55.90935554213909, -3.110612619126846 55.91076724522697))
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>