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/S02001714 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001714 geosparql: asWKT POLYGON ((-3.7421094909585935 56.0039360032929, -3.7429822837395763 56.00348543522447, -3.744195048062032 56.00371028199264, -3.744936428219163 56.00335354210728, -3.7446308700212474 56.00276589860806, -3.7466298768251667 56.00201488362967, -3.749426364930865 56.00224182545968, -3.7501080207582533 56.002420861387186, -3.752428503392004 56.00266647075703, -3.7554705398075128 56.00114045837458, -3.7565316732550915 56.00084676915419, -3.7589323483923978 56.00038256433769, -3.758982683581586 56.00008393357069, -3.761391968469265 55.999815889307726, -3.763336403968541 55.99990492144031, -3.7666060286752976 56.00021755060242, -3.766953308313963 55.999745286963794, -3.77209264252482 56.000965539561655, -3.77461042392794 56.001315730927864, -3.773967751037049 56.00163950087456, -3.774383671937344 56.00231647284862, -3.7730563348200743 56.00239847458906, -3.7722409214507264 56.00275055512718, -3.7712351514395843 56.0027053949098, -3.7704234133637713 56.00303839545674, -3.7696130291262615 56.00285234169486, -3.769094810858773 56.00309341361295, -3.765961649117977 56.00366852559497, -3.7662932167401504 56.00425686934574, -3.7679362609214584 56.00379151679063, -3.7702577663942023 56.0061876334464, -3.771396970972946 56.00608873124234, -3.772929530571007 56.00628237198905, -3.773872708365867 56.00586442208388, -3.7755338290788805 56.0053642367026, -3.7763818769033426 56.0062488604332, -3.777200541977359 56.006733095753006, -3.7777491635906855 56.006455597328404, -3.7787010138646417 56.00706210176034, -3.781134907523644 56.00823094622653, -3.780676427894109 56.008526199682926, -3.7786650883936153 56.00891065903762, -3.7797308366384432 56.0115312174622, -3.776151985218989 56.0120773856614, -3.77008905623362 56.01291012307007, -3.7696302263136365 56.015154288798215, -3.7770557111952643 56.014848581725744, -3.7771007438217072 56.016057359428935, -3.774215823930729 56.01608446402536, -3.774385920805647 56.01880625708865, -3.775548019585145 56.0226819609579, -3.7743402259495933 56.023671108965225, -3.7742775663385344 56.024266635729425, -3.774711589394665 56.025096895810364, -3.774874927071797 56.02591089618302, -3.774041366630793 56.02653800854853, -3.771967032132523 56.02658848577105, -3.7711087128559178 56.026285565626296, -3.7709001442325722 56.02582882079498, -3.7711285323741643 56.02466962145976, -3.7709073484074094 56.02393721310377, -3.769969168678327 56.02342526543036, -3.7680970442416792 56.02300551657351, -3.7660434386564576 56.02271969345101, -3.7646203218672083 56.02237227575685, -3.7595536221977017 56.021328165640234, -3.757780830807714 56.02102506351188, -3.75539123992496 56.02048791381759, -3.755125681514558 56.02014222848169, -3.7531503788419722 56.01840822804534, -3.7506098163819543 56.016479269603465, -3.7482710031437847 56.014993404011605, -3.746942506730188 56.01346911054153, -3.7468728327667287 56.0127378013964, -3.7504250992549153 56.011359939583414, -3.749379432710977 56.010814129718746, -3.747084920627293 56.0093471957822, -3.7452779940336485 56.01005861614112, -3.743152094842973 56.00887459885501, -3.7420830855049667 56.00981096676374, -3.740867356684654 56.008410715667026, -3.7449295010420607 56.00636733430164, -3.7421094909585935 56.0039360032929))
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>