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/S19001482 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S19001482 geosparql: asWKT POLYGON ((-3.388575631212743 56.083714965194154, -3.3886776196551307 56.08297700271532, -3.3910168672483634 56.08058695800863, -3.3930233170744324 56.078247922956955, -3.3929649047410595 56.0775367099428, -3.3910867971566487 56.07569122290122, -3.389700669067436 56.07386732328148, -3.3884427421676624 56.0715988699137, -3.3876735254029717 56.069566594171924, -3.3871168887097474 56.065723631972546, -3.3872508782370074 56.06169324285526, -3.3934536046402286 56.06167726828729, -3.39217660963356 56.05636897949012, -3.392681289904738 56.054058640728606, -3.3927553905662564 56.051992438620005, -3.3959488205891115 56.05217534353635, -3.396402822254125 56.052820835407644, -3.408404160362599 56.05177295209856, -3.4078059815899144 56.049704343119586, -3.4122182361660984 56.04926735332366, -3.4107045349340015 56.04767436569845, -3.4069650149337614 56.04638719777138, -3.406490861942295 56.04526940112555, -3.40855356331424 56.045120966246174, -3.409732077633617 56.043902530865424, -3.4071708966060017 56.04234132492043, -3.412836240977733 56.04318410960131, -3.4301970792967595 56.04598526487903, -3.436100754063364 56.04759700043503, -3.4416278218818115 56.04942851378287, -3.44511181475918 56.051571272208704, -3.4518164216801703 56.05779151960619, -3.4573106374339053 56.062525103206895, -3.459719257763422 56.06038499715591, -3.4619027062259966 56.05957534331587, -3.461526278976144 56.05763195650368, -3.4639855886628643 56.05787993734754, -3.4638331361351495 56.05917710588933, -3.469878145048472 56.05808087062411, -3.4704901991614667 56.05854982005245, -3.4689488260176264 56.0594038505909, -3.4669872936913273 56.05976864404859, -3.466645249590391 56.06121996440943, -3.469005557579127 56.06554040614884, -3.4726171492134394 56.07052934753605, -3.4771735827123997 56.06995372050348, -3.478007914551785 56.071606084975855, -3.473471434054181 56.07186700407387, -3.476008466210947 56.07477500785898, -3.479675742326089 56.07687177330908, -3.48920502775656 56.0786780221044, -3.4948872383862892 56.07935513414803, -3.49374972119584 56.080904200601815, -3.4963497025159183 56.08152555020735, -3.50081917176414 56.08197280335376, -3.4994051570240194 56.08261740789117, -3.494716525524283 56.08158565818062, -3.4888278116808245 56.08264525876273, -3.4863281076370374 56.08533515122776, -3.482725332838828 56.085974592471814, -3.482422305652832 56.08333058154482, -3.4806958422430427 56.080878057263575, -3.477930625265437 56.08138886105668, -3.4741086350705697 56.08140219210673, -3.4731393774512673 56.08203171843508, -3.4710763840234446 56.08231182238294, -3.466526921094947 56.08179715104979, -3.4672026152348203 56.08361408262677, -3.464739840017981 56.08393087416113, -3.4651919921385606 56.08501100246224, -3.4686974970536317 56.087342607360256, -3.46982848231424 56.088983495603436, -3.4670875953205162 56.08906291711363, -3.466201201349367 56.08875836678457, -3.4654956196086157 56.08706383791013, -3.463447791305928 56.08685349146906, -3.45905457559735 56.08705789580871, -3.4596864882472507 56.08543743763466, -3.462117615838296 56.085130108436715, -3.461719575520863 56.08438001875196, -3.459546524847378 56.08471124596102, -3.4595169706673623 56.083929843650196, -3.4571557890899136 56.08402140450791, -3.4570077859711574 56.08278716651185, -3.45892859043442 56.08196894149203, -3.46249793799447 56.08117190391572, -3.4622561929685256 56.079880832859565, -3.460363848696962 56.07913047114438, -3.4566103813169735 56.080154262616865, -3.4538238418022873 56.081441432832065, -3.4535255885004603 56.08272459151886, -3.4543534267788036 56.08337078309256, -3.4528308325097323 56.08433220679678, -3.4489013954320225 56.084540136686265, -3.4471543720781135 56.08424693430563, -3.444097814690272 56.08437373022826, -3.443662802539209 56.08335600270212, -3.436119545009626 56.08455497282649, -3.428665624356979 56.0866270495617, -3.4256092775200053 56.08417821829253, -3.4192126293433 56.08479521951327, -3.41703732616711 56.08519254755973, -3.4158417561233163 56.08364479664505, -3.4133267767013553 56.08249055054609, -3.409618657329843 56.08282802340428, -3.4099925057225264 56.084252739145924, -3.408507907519385 56.08442849126951, -3.4063068889330603 56.08590583616966, -3.404968751588501 56.08376308171192, -3.4012858307653717 56.08456872125115, -3.4005254915490393 56.0835440106977, -3.398059478863111 56.08156818619995, -3.388575631212743 56.083714965194154))
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>