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/S02001262 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001262 geosparql: asWKT POLYGON ((-2.1134899986727396 57.16594364625283, -2.1121818189565436 57.16338114230634, -2.1146678349917947 57.16331247923345, -2.1153984975684748 57.162755652946004, -2.115431010529559 57.16160941081052, -2.1147284599847262 57.16125508439329, -2.1137845689275903 57.16119710701955, -2.1137251589982937 57.1606408080918, -2.1135531316978806 57.158458095614876, -2.1126724593124178 57.15672262081497, -2.1109781091389155 57.15423839320312, -2.110051018791166 57.15375412925401, -2.1103018464662777 57.15215160049551, -2.1090359570395694 57.151455367438295, -2.1100432660871626 57.15114906371154, -2.110905289564623 57.15153000907178, -2.1125371228091163 57.150970627072674, -2.1122780564368098 57.150576469570936, -2.1151797885708694 57.14995868597381, -2.114917190830974 57.1505518060666, -2.1142836787259283 57.15088832361522, -2.1139760510916945 57.1515503015737, -2.114335570396858 57.15249019840329, -2.1141483435467374 57.15313063314426, -2.112162426128675 57.15327885101395, -2.113340547045554 57.15382304957497, -2.1144922724516033 57.15343314527799, -2.114601171034925 57.153125386625376, -2.119122791596602 57.152892441113266, -2.1196366218546934 57.15352540602555, -2.121364948077603 57.15364345039042, -2.1217011656540246 57.152854029661114, -2.1247912591037528 57.15261743011975, -2.125190531554638 57.15338058739519, -2.126172241078241 57.15343347325215, -2.1266692353267302 57.1542725684125, -2.1257911696679277 57.154397787277084, -2.126753924790936 57.1561996789397, -2.1268593922006196 57.15803210489693, -2.127101217667826 57.15941966156277, -2.1270488259208373 57.16024173106318, -2.1258313938182147 57.16022049362897, -2.124789421981876 57.16044035302829, -2.125426292809996 57.16093268582555, -2.123419350773493 57.16125855262221, -2.1213496952211766 57.16127128284089, -2.120745967731452 57.16096032705843, -2.120195321659851 57.16128076823085, -2.119548423815343 57.16084287345457, -2.1172229763072616 57.16083218122326, -2.1162253195669978 57.16160215444858, -2.1166290941557344 57.16236590855137, -2.1167025591980084 57.163054718985684, -2.1176475533732932 57.16371836069542, -2.1173537506677143 57.16427587832788, -2.116316909046799 57.16468076586577, -2.1149889407556577 57.16444209250819, -2.115127919073302 57.1654486526607, -2.1140031824490624 57.165546042155185, -2.1134899986727396 57.16594364625283))
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>