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/S13003011 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S13003011 geosparql: asWKT POLYGON ((-4.631757422501383 55.842414854286396, -4.632640406348463 55.84110971893489, -4.638898955197537 55.84072925339311, -4.643339980273162 55.83908604188332, -4.654758121965977 55.84021532506719, -4.678424144227928 55.838227008661626, -4.681277905643458 55.838402125695865, -4.685136864441625 55.840391604715066, -4.6948639121932025 55.84083494900765, -4.7033883217949155 55.842589337111896, -4.7041957477429115 55.846737775573665, -4.7100532612535755 55.84711221449972, -4.715933248266742 55.849292525995544, -4.716728298567445 55.85060975263533, -4.724855989392559 55.85258720605573, -4.730011998592334 55.85262570053674, -4.738800330984457 55.85034936349751, -4.75094123425606 55.8497682371921, -4.755356458031512 55.85173682726154, -4.761594448846794 55.8554260838417, -4.769390564199441 55.86470693310016, -4.77780555207162 55.867492404406825, -4.781057323235208 55.869931286615966, -4.784096495319835 55.86961990316173, -4.794188260573146 55.87223601171972, -4.7957133668630645 55.874992397022204, -4.7986735594700125 55.87724416406802, -4.798476028060178 55.87869769261236, -4.787312631418255 55.88014029798765, -4.77628589006277 55.88326154996757, -4.7720199190855785 55.885222920070554, -4.770667364030501 55.88700173801902, -4.773363765264829 55.894976957343644, -4.778425227588397 55.89909193156317, -4.777919661660277 55.901329850790866, -4.7696729297814 55.903969684677094, -4.7705017599845325 55.90630243222923, -4.775275660865283 55.911638496452525, -4.771786426571698 55.91040030204741, -4.766212887852176 55.91045192945927, -4.762868302879909 55.91171358796633, -4.757859402613392 55.9116713723823, -4.7530833928113205 55.91062772568064, -4.740881820121831 55.90975668620999, -4.7371175971069714 55.91288493505176, -4.741546244099145 55.91433960399857, -4.742229425105948 55.91834681223144, -4.741105800482663 55.91964013193038, -4.741657027061004 55.92359088194138, -4.741180427989111 55.92760864243817, -4.7391527530057544 55.930522928377805, -4.73330270132981 55.931122206966826, -4.722055791867763 55.931505297306636, -4.716710699637742 55.93054118830712, -4.711538824688806 55.92881974144879, -4.708346559129031 55.929282790598684, -4.710119783691456 55.92434327591938, -4.708594412130078 55.92081373810925, -4.698747028188872 55.92217700071993, -4.690089886447095 55.92194586994163, -4.68623813168096 55.91898197109843, -4.686610567313398 55.91703144988505, -4.676897346659823 55.921055966282914, -4.6726278192456565 55.91984183886567, -4.67079362104397 55.92170443168429, -4.666337142659022 55.91930594540613, -4.662125051660172 55.9190599669558, -4.664092887093831 55.924410509170194, -4.664191901155481 55.928884567547165, -4.654485152603368 55.93104201332008, -4.647935918490477 55.93004778926544, -4.6469711968291 55.93165596258241, -4.646473214834793 55.93815619315707, -4.630647409419874 55.939493925567454, -4.6160884566208775 55.93740521264589, -4.609874266412385 55.93629671037798, -4.611335012718991 55.93147322587571, -4.617405431703361 55.92858489957429, -4.617427322947823 55.92418736983246, -4.622829729489194 55.91965733723775, -4.627983022478181 55.91626484227823, -4.629572937744754 55.91641518441892, -4.635043875307934 55.913732028899716, -4.630782768630999 55.90743781223945, -4.626286954742611 55.90633477407005, -4.615991463918112 55.90711224133056, -4.613914545358617 55.90311881692133, -4.612029352050086 55.901589489137194, -4.6120137154499306 55.898336091781424, -4.613378448911995 55.897433434683286, -4.613407759800689 55.8938752725281, -4.617264481389321 55.89157229342192, -4.618754088125755 55.889396869277135, -4.612316843518336 55.88771266027377, -4.609921034531634 55.885092225907215, -4.6039434105643045 55.88407015061535, -4.6047223697556126 55.881687092821124, -4.599116148167754 55.87967901100511, -4.601880557977612 55.876689435995296, -4.6052242183215375 55.8754556481555, -4.600222621883409 55.87191039174839, -4.608645800685527 55.87028579671835, -4.60908196695052 55.86789918670669, -4.603317736782475 55.865104639002574, -4.601409369297998 55.86285922452335, -4.608967439636416 55.86311715273182, -4.622824773485143 55.86159910482549, -4.62571429533542 55.85882742312525, -4.624767783129413 55.855741234633314, -4.6221338228137965 55.85438449529835, -4.622917919129452 55.85025110810178, -4.62481964279621 55.84840833639777, -4.629476398504079 55.84657943591602, -4.631757422501383 55.842414854286396))
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>