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/S01012455 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01012455 geosparql: asWKT POLYGON ((-4.63551832383133 55.43409460147803, -4.634906791540784 55.432984169224056, -4.635884185051077 55.432468713179084, -4.635042606378353 55.431938530237936, -4.636278596008908 55.43079726301007, -4.636979294688838 55.42849168064632, -4.63859752199124 55.42835619090215, -4.638785969928184 55.42739031013785, -4.637182098921339 55.42609455763344, -4.634655315455232 55.425178233631286, -4.633748388594733 55.4246134958495, -4.632420839230773 55.42461512099787, -4.631954274140332 55.42496674427861, -4.631745106005137 55.425861147903085, -4.63235309485938 55.426684026336666, -4.633437500289893 55.427065173978754, -4.632572744991137 55.427856887407565, -4.630893159640304 55.42839813582887, -4.628565841298748 55.428680142719294, -4.62887311288238 55.42807128085963, -4.62787925086349 55.4276252246638, -4.627670108767803 55.42685667565522, -4.628117318237905 55.425975141233785, -4.628076698796569 55.42488835895091, -4.629412677058798 55.42334949913695, -4.629474809437504 55.42262006235607, -4.627906273216442 55.42113466617644, -4.6271713802005685 55.4210156277959, -4.625026595769746 55.42132237786579, -4.624003469120677 55.42067017622481, -4.623354660955613 55.419704312431996, -4.621326990753651 55.41915454151245, -4.6208443666011725 55.418787356436354, -4.620855742921191 55.41800508018232, -4.622545444483353 55.41751040984461, -4.624711494867181 55.41800325554382, -4.625048521283319 55.41736679665638, -4.623687518361897 55.416821058534765, -4.622896961504055 55.415960042996964, -4.622584655013418 55.41503389499974, -4.621769067471651 55.414085624197725, -4.623669161223008 55.41217915854488, -4.625695868013306 55.41164493699688, -4.625828025506496 55.41077918180193, -4.627441199089065 55.410164247457885, -4.628523305665405 55.41090963288528, -4.627747989162818 55.41152603342319, -4.6296492415563595 55.41169479496917, -4.62905310649195 55.41214287892773, -4.6301973384593715 55.41251260696934, -4.629603660393541 55.41357918523625, -4.632604953508033 55.41408514680677, -4.6329509442225145 55.41536960845193, -4.634054393872565 55.41541987807256, -4.6359047881226 55.4160469517422, -4.636215612995794 55.41735812347483, -4.637313090486056 55.417156749387665, -4.640282875944284 55.41761400063902, -4.639591535914158 55.41838400025142, -4.641185142951012 55.41858329219305, -4.644586784079217 55.419588423459466, -4.64425745090297 55.42009892789456, -4.645617630705364 55.42121265872397, -4.641717455472001 55.4211466600753, -4.6420759078171585 55.42193015518374, -4.645781441510824 55.42185475305669, -4.645214163624731 55.42400639534761, -4.644039844398633 55.426063296311575, -4.642073958813684 55.426447375016615, -4.640433880390707 55.42697719726633, -4.641671544687804 55.427666680591564, -4.643605009161874 55.427124403959716, -4.645149835691752 55.42729773615765, -4.645670072091584 55.42831318619602, -4.645137709948738 55.42962827928916, -4.645440162371906 55.4302196969105, -4.648264012733888 55.4309359279059, -4.647878925912213 55.43130903008806, -4.64941671247888 55.43197534409557, -4.650427604703425 55.43183982375204, -4.652005945761756 55.43053804161981, -4.651793787669533 55.43208874132397, -4.651393613067865 55.43225023178941, -4.651609267680331 55.43310849337003, -4.649240330225077 55.43385199235042, -4.64617035491063 55.43498815270376, -4.645612526288233 55.43562944817678, -4.645708101996109 55.43611278193585, -4.646893874368657 55.43655566682345, -4.64518661539357 55.43682520011113, -4.639324074636488 55.434030488843234, -4.638397883540483 55.4336549660032, -4.637292405457016 55.43414623813398, -4.6366420041901995 55.433638908717775, -4.63551832383133 55.43409460147803))
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 lang='en'>
<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>