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/S02001238 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001238 geosparql: asWKT POLYGON ((-2.1548250559887374 57.12522158405414, -2.1522024981793786 57.12593775713744, -2.1512614685579097 57.12486627594368, -2.150585285180988 57.12505644473579, -2.1497168258590085 57.124077964935815, -2.1517622995982735 57.12346465471314, -2.150154708767363 57.122092185150564, -2.152199992043306 57.121460901623216, -2.1509969371875672 57.11999078928414, -2.1495954345250685 57.118598165716975, -2.1489924694275473 57.118765349839535, -2.1490636427543053 57.11955325031897, -2.146034734555891 57.11898555109124, -2.146046125382707 57.11757711037501, -2.144281965848947 57.11722103174699, -2.145063023937015 57.1170155722078, -2.1479712259516766 57.11543294571729, -2.149651150226487 57.11477159172425, -2.1524421216671388 57.11408281630825, -2.1547809666721283 57.11388052252589, -2.1593919995045043 57.112967447022406, -2.160330926777212 57.1128395868751, -2.162292454789471 57.113219738617104, -2.165028697156113 57.11403721494194, -2.1672162075402306 57.11434422875844, -2.1710923414079017 57.114168314219704, -2.1730846800809105 57.113682286841254, -2.1747239587112226 57.11291734721525, -2.1753022249020026 57.1123003000047, -2.1758726980078182 57.111087682932634, -2.1763296191752746 57.109607674480905, -2.177708632072148 57.10798682628883, -2.1788784303271846 57.107161411825324, -2.1814873951467364 57.106951046629995, -2.1822483712252594 57.107610198129365, -2.183794515618774 57.108130753804296, -2.185733721514182 57.10834168994229, -2.1890714717407906 57.10794323579053, -2.1899203539151393 57.10736344049267, -2.190814237754951 57.10526722543669, -2.191904024222814 57.104683455821274, -2.1947456329380466 57.10418499878425, -2.1966389677977847 57.1041775484629, -2.1982544290041006 57.10468434267773, -2.200597195592591 57.10505700340992, -2.2021000485015647 57.104875823548284, -2.2034284741720604 57.1045092267794, -2.2083916442296863 57.108210815637165, -2.208774584073247 57.10878509558257, -2.2100698241448256 57.11011242683768, -2.2080916881393304 57.11065471421667, -2.206376656807515 57.11101687533532, -2.20723849460098 57.11160833586186, -2.2082888475550972 57.111306495614365, -2.2111461883252765 57.11357808766765, -2.21050352613285 57.11380374642714, -2.2167514491283353 57.11775462907507, -2.2189880886169924 57.118999386805534, -2.217138458274307 57.11898464037205, -2.2129893601409947 57.11826413451355, -2.209017656374775 57.1194203438177, -2.208250421933665 57.11936824425794, -2.2067291942072087 57.118234908263965, -2.2077287739130274 57.11788507957879, -2.2065816687277855 57.11741218518208, -2.205818488082497 57.11787092259699, -2.2080679344918375 57.119416768645394, -2.206388754408076 57.119720991540966, -2.204960705929107 57.11885346944653, -2.2027960925083203 57.11964502369032, -2.2022769808555562 57.119153258336, -2.200346334227171 57.11942584437403, -2.201569798034874 57.120098761699744, -2.1980862867533797 57.12035983343011, -2.197179276309827 57.119380627664206, -2.1957334255331316 57.118382109406674, -2.1939203862402716 57.11904070272682, -2.1912966613542 57.11942202636072, -2.1900672032065236 57.11969052456789, -2.187406028283521 57.1207394360957, -2.1874198623398633 57.120209414433795, -2.18349523568809 57.11804132323614, -2.182428076432747 57.11797069428317, -2.181448871020995 57.11830481375454, -2.181067966891159 57.11808078830771, -2.178661709921546 57.119054415503946, -2.179372711167942 57.11924203462434, -2.1803769676148814 57.11998804631799, -2.18174091919963 57.12061303862154, -2.18106620013863 57.12109910141052, -2.176432357828036 57.122525038904854, -2.1766378633546255 57.12406085264743, -2.1805850337738715 57.1240282745585, -2.1806685768199014 57.12422577242635, -2.182270715158169 57.12424142628082, -2.183288692687248 57.126553374941956, -2.1811248862804473 57.12640892194951, -2.1769773804301913 57.12598274786861, -2.1742253503644013 57.125767509518035, -2.173706798096312 57.125174000549165, -2.172752312065118 57.12547226466516, -2.1706032914217683 57.1240971285614, -2.1637312414962016 57.125937416615216, -2.161561587562195 57.12339619702863, -2.1548250559887374 57.12522158405414))
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>