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/S01000601 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01000601 geosparql: asWKT POLYGON ((-3.02065461957752 56.5417257626389, -3.018403491522232 56.53716294304414, -3.013576503690344 56.537060087599606, -3.010743271191311 56.53020108160209, -3.012486540630006 56.530339623273136, -3.009988147197634 56.526873990971374, -3.01104218204937 56.52677557319109, -3.009043384963793 56.52435703615641, -3.005909967417381 56.523888301927634, -3.005339356784933 56.52505191516075, -3.00182407311233 56.52428069626003, -3.000268173202308 56.52445495346984, -2.996270164616874 56.52200737275577, -2.991522245981828 56.52318638255358, -2.989457867018343 56.52318487295571, -2.988244861080683 56.52217030251673, -2.981470796788979 56.52169392440466, -2.976051864345304 56.52349835381239, -2.974624153103956 56.52054733298096, -2.979691374947144 56.519146563816946, -2.978162976812177 56.516958265971226, -2.980491405248705 56.517128582082584, -2.979954590931239 56.51396131166646, -2.986545875223013 56.51371147845552, -2.986012876620949 56.509457081515016, -2.985239650190355 56.50783703146598, -2.984245462713072 56.50771913158765, -2.982568144645419 56.50317729381241, -2.980092810680533 56.50105855229508, -2.980694454661462 56.50108075609165, -2.980916974103236 56.500261412888506, -2.979908001993237 56.49767286909197, -2.985832785469523 56.498138091038946, -2.986577165720364 56.49552668298422, -2.993208647433473 56.49693829030006, -2.995078589551698 56.49825302327985, -2.997186090643587 56.49809236649426, -3.000811490348921 56.498545321068654, -3.000857631506001 56.49767309754454, -3.00131742371476 56.497228742396885, -3.005176121768372 56.49917181041608, -3.008452230384482 56.50127222999033, -3.009580366760462 56.501765709455306, -3.031707807632608 56.502401449092055, -3.031671108733921 56.50045211461697, -3.044743773674454 56.49868882193879, -3.04837688103623 56.50714045285283, -3.054203555337903 56.50632744833512, -3.056286264047866 56.50468355209223, -3.062553703537771 56.503965233442976, -3.065129286278653 56.50578256479131, -3.06753218486185 56.505647447767444, -3.067694929619558 56.50683384721465, -3.069248824175548 56.50779361723556, -3.073431525179577 56.50752565686938, -3.073634019468659 56.507993851667266, -3.076082840229446 56.50782890655942, -3.078011539081269 56.50937549406705, -3.077771576874392 56.51065338305407, -3.079617782771884 56.5110057162031, -3.079797812465538 56.51276512358397, -3.097446447164676 56.512790137784776, -3.110931938419737 56.513820284554726, -3.111435976064458 56.51771508515521, -3.121070254609729 56.5186976775964, -3.122874734438848 56.51761221285856, -3.124737278466675 56.52287830394405, -3.12160120888096 56.52291564178829, -3.11677944469245 56.52366886280181, -3.111130152719323 56.526145352708966, -3.103096294294787 56.532182690012384, -3.103642913898415 56.53755958024792, -3.102353038721304 56.54017654701478, -3.090954413677093 56.54926166262709, -3.087686539151675 56.54931725934099, -3.08532007108149 56.55131453909521, -3.085160073135659 56.553112836653085, -3.079671263494762 56.55402315021922, -3.077256817671397 56.556074595168, -3.077700980108005 56.557391464394016, -3.072844981725343 56.5588440369228, -3.053787881562929 56.56101059644058, -3.052030968327858 56.56160946792327, -3.050433360221501 56.56091320743538, -3.050648980296887 56.55988715766481, -3.048097708286845 56.55823758855006, -3.046091920096065 56.55276498491174, -3.044107880741024 56.55396761625627, -3.043030697182428 56.54378828431143, -3.039425960144744 56.54281222788322, -3.037192291651691 56.5455801356021, -3.034515658823805 56.5458360420373, -3.032039406963936 56.54210112943014, -3.027000279732618 56.54024182686048, -3.02065461957752 56.5417257626389))
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>