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/S01010819 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01010819 geosparql: asWKT POLYGON ((-4.614381328795644 55.930227470029365, -4.6148098900346515 55.9293168454695, -4.6160289765452 55.92954082027803, -4.617405438959253 55.92858487253791, -4.618193801948001 55.92593724011765, -4.617563049333036 55.92522529954943, -4.617427405327034 55.9241873411985, -4.618136272849568 55.923186252497096, -4.619133504937322 55.92266349250855, -4.6200707535867505 55.921845387341705, -4.621850285436217 55.92176705032942, -4.623782268704514 55.92086570529022, -4.623729600468489 55.92022865868235, -4.622829829065199 55.91965730283749, -4.623771128887948 55.91840404731617, -4.624960198545087 55.918093766277906, -4.6265087282922615 55.91702947292768, -4.62798313176994 55.916264857099044, -4.629573018912964 55.91641516208737, -4.631887447164183 55.91549562570763, -4.63377263894119 55.913959642114506, -4.635043925063622 55.91373199195375, -4.634110019261788 55.91308235137705, -4.634195666483025 55.91264638173554, -4.633324582067804 55.91148305592461, -4.632805174099085 55.909558080536954, -4.630782844755115 55.90743783405639, -4.628340996581158 55.90739023673578, -4.627849791584219 55.90646863669437, -4.626287036806849 55.90633481284306, -4.624637500868568 55.90650302985234, -4.622336966876023 55.90648555704466, -4.619013522079676 55.90667497279393, -4.61599156695418 55.90711225899699, -4.614890480336679 55.906800395422806, -4.613257722056348 55.90591107993519, -4.615413313137836 55.90544014277529, -4.613914625317295 55.903118848562855, -4.6120294160253 55.90158952112008, -4.611842197584124 55.899209800044645, -4.612013818776191 55.89833609057098, -4.613378484812086 55.89743345377826, -4.613634408864859 55.896410542794364, -4.613407870369546 55.89387526037558, -4.614490389619598 55.89232425204421, -4.617264487705289 55.89157227718941, -4.618580103203967 55.890265906705025, -4.620133705645939 55.89026362435562, -4.621285678364805 55.891928902480686, -4.622450447323628 55.89228261770964, -4.62155884084196 55.89266166258741, -4.621554934934508 55.89388629721715, -4.620521993776875 55.89388664698408, -4.6199153336072785 55.89469670347034, -4.61743344131206 55.895787644815506, -4.616602304236278 55.89678967573814, -4.6169752344172865 55.8975753319051, -4.617996089978227 55.8979874111675, -4.6171654114806895 55.89851255187048, -4.616074345782063 55.898775703636986, -4.617629858091822 55.899567814549215, -4.618412971792327 55.89903669914742, -4.619792644365015 55.89871515153769, -4.620235338465685 55.89793749473015, -4.62201029059938 55.89828676050992, -4.622935044178026 55.898061319073264, -4.624677889061423 55.89674136145672, -4.624887797325943 55.89623841817131, -4.626869155944787 55.89620512501823, -4.628177983346947 55.898999515584954, -4.631081494616081 55.89840715715409, -4.630940055520495 55.89773605539483, -4.63227396615447 55.89622826206552, -4.63586901408006 55.89823279346042, -4.638407310042689 55.89907318769824, -4.645201100392528 55.90119372007783, -4.646572218361221 55.901833146162176, -4.646288939388306 55.902602953886344, -4.647531172535558 55.90468121519878, -4.651258986963488 55.9043565847115, -4.651992732306174 55.904061388673, -4.653160730157454 55.90499797148709, -4.6538233293385 55.90600595314638, -4.649355974395958 55.90835104584742, -4.648089470034341 55.90854161322412, -4.648586321549019 55.909004546317895, -4.64792042661378 55.90990078751624, -4.647077085564071 55.91032722587186, -4.647468788924065 55.91138035448208, -4.644881116717725 55.91096236596709, -4.644385867190753 55.91265616895044, -4.646712169368779 55.914853257849515, -4.64790943950724 55.9173891916855, -4.648568264854735 55.91789634234627, -4.650659414044509 55.918282778754715, -4.651366238044439 55.91878887234669, -4.64746365493433 55.920005406657836, -4.645306279659696 55.92076186894182, -4.642156809732323 55.92246539083806, -4.645201019369898 55.922975266347756, -4.645722168003613 55.92545384041873, -4.6461402691679226 55.92735937284112, -4.64575505757727 55.92763197280874, -4.642143289507495 55.92796135172942, -4.641738581299851 55.92774960704815, -4.6402489639867595 55.92784057238724, -4.638573034392587 55.92766505418721, -4.638309336356367 55.92864599352325, -4.640087790012104 55.928640808840775, -4.642024007059325 55.929080919599855, -4.642458353104541 55.92870356972574, -4.6438842028581275 55.92853090417646, -4.646168779814669 55.92873973019144, -4.646189183419281 55.92772613373795, -4.649699415013169 55.92823282390359, -4.649366084361266 55.928880930879046, -4.649654308231329 55.92958480805002, -4.648164476406543 55.929338225678926, -4.646100296202269 55.92935564577921, -4.646022735551661 55.92962696331317, -4.641529016945052 55.92982037887045, -4.642634280653247 55.93113828477277, -4.643725092392912 55.931442708608344, -4.642644610184053 55.93169763197858, -4.640383567482483 55.93148278713562, -4.638058524555445 55.93152723737923, -4.6362808197977206 55.93189788517315, -4.635447174418537 55.93240110274687, -4.631978335206352 55.932367450112366, -4.630540394633117 55.93215370069137, -4.628544405481509 55.93228172112979, -4.627581899865482 55.9329170667219, -4.625164908298054 55.93321310894989, -4.622951606514393 55.93304096854008, -4.621590354044858 55.931994963960854, -4.620166607922038 55.93123160815854, -4.617562262253731 55.930790855447384, -4.615316754587521 55.93058960311304, -4.614381328795644 55.930227470029365))
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>