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/S01004326 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01004326 geosparql: asWKT POLYGON ((-2.857911554461384 57.70542477159622, -2.858452206462712 57.70468611481487, -2.859673297134365 57.70478247848529, -2.860459703913898 57.70372032315441, -2.859887534939965 57.702942720642156, -2.857982136375364 57.70249554142048, -2.858201343539623 57.70280759713959, -2.857550107070843 57.70276844358564, -2.856937838610697 57.7023797750115, -2.856987681495164 57.70448943758991, -2.856218674924692 57.70461141722557, -2.856158195057029 57.701820225399196, -2.855362142364466 57.70173378005005, -2.8553429334513 57.70223741299903, -2.854008908313727 57.70269507816253, -2.85228629576949 57.70222162719463, -2.852666777331577 57.70270412699066, -2.847454488624108 57.702633286235624, -2.847453828383809 57.70204016556321, -2.847009641213142 57.702234660362045, -2.846638272835823 57.701839750699406, -2.847341812245561 57.70190119067123, -2.84756282336578 57.7013032594025, -2.846700530032218 57.69995950050226, -2.846920432009545 57.69885968349408, -2.84484412656485 57.69751072684759, -2.834563532506264 57.694021817115676, -2.828663541605362 57.69276709035232, -2.830415286437397 57.6916148140322, -2.834395864368542 57.68964838019536, -2.832293547331236 57.684281622924665, -2.83064709218035 57.68342112698689, -2.8305995821706 57.682810619095974, -2.829230182375788 57.68306213182504, -2.829444202890076 57.682153482894314, -2.827926751903424 57.68179513238424, -2.828797159988135 57.68098997925373, -2.824350845053453 57.678665575038686, -2.819211481288175 57.679040297790024, -2.817410809509018 57.67651884041866, -2.818189037468098 57.67608264299225, -2.820615910817967 57.676632829954016, -2.821432098253713 57.67639398440183, -2.820845045871528 57.67564325260539, -2.821912159652662 57.67463925138553, -2.827130993131184 57.67336563124083, -2.827292721066124 57.67457723103278, -2.828536927587475 57.67473077625315, -2.832311847290331 57.67337656355104, -2.850629818937041 57.65804702895063, -2.856431275597037 57.65675029804857, -2.876350741958495 57.66067403094089, -2.873761489968554 57.665210249605686, -2.87659329454531 57.665864360883106, -2.886490934145562 57.66687330887813, -2.887372678418337 57.667972000370746, -2.886459840413759 57.668346693145914, -2.88597246497384 57.66967056646756, -2.889058069596035 57.6697117978493, -2.889553077725338 57.67144198208919, -2.891152743459435 57.67172715399898, -2.892602250018167 57.67272299947634, -2.892700871340303 57.67400683166905, -2.896701508329932 57.67374498723792, -2.903926148145966 57.67233718015375, -2.905544137967447 57.67402334446639, -2.908818925299616 57.67356868988625, -2.912233529516347 57.67401121608689, -2.914816159395902 57.67470220431348, -2.911139607545979 57.675851577900445, -2.912154941451854 57.6782246872497, -2.907299036789499 57.67984056383114, -2.903898248620961 57.67929899321806, -2.901943876302761 57.67825297958226, -2.900419302601003 57.678982456782315, -2.900387492702469 57.67972824981502, -2.901686489996318 57.680042376718774, -2.901630132333052 57.680473948911704, -2.903253320215236 57.68033661311979, -2.904891493735408 57.68080099082191, -2.904739539143126 57.68277827615333, -2.907236449973749 57.682051021669544, -2.907784024740924 57.6824314792798, -2.908925050508716 57.68365881549686, -2.907811293253189 57.68402222526905, -2.911392915195537 57.68661106262197, -2.907142419985639 57.687665635875575, -2.904765143292096 57.68649693797095, -2.904199474677793 57.686680635701016, -2.903077311046574 57.68539513879373, -2.89718255934114 57.68723959780101, -2.905800482424226 57.692364224095364, -2.906445377715801 57.69199131626504, -2.907750112263127 57.693185646828844, -2.900214103374648 57.69572768575098, -2.89810156778781 57.695778625941784, -2.898683425252254 57.69624159554203, -2.895026803918154 57.697337958913806, -2.896993575414887 57.69771268133223, -2.895817701874963 57.69838737819381, -2.897209338431503 57.69904567243838, -2.896740186468376 57.69974964712655, -2.893470651202208 57.700028872413675, -2.892649875299103 57.70055269890798, -2.891364330347229 57.70043645444842, -2.888531335833351 57.70135970982828, -2.885981716197594 57.701242758722294, -2.884570355646165 57.70172841674767, -2.884797346123227 57.70209556071973, -2.884186940129885 57.701842864537944, -2.884605925289977 57.70216243541978, -2.883809107682117 57.701993456164146, -2.883562138221234 57.70260616556002, -2.881678845324065 57.70236007231951, -2.879649395808288 57.70338931535836, -2.879726159305261 57.704298323295106, -2.877464475664139 57.705389942432, -2.876687353186273 57.70512904903106, -2.877512522147954 57.704590812802444, -2.875954450304573 57.70514283639964, -2.874616844933569 57.705094449932346, -2.875613888695527 57.70458606273246, -2.874977538981262 57.704668959188254, -2.875026952891948 57.70421487835407, -2.873811499672506 57.704640752390475, -2.87441474281555 57.70405143087962, -2.873164897279955 57.703324201723895, -2.872382151979518 57.70377757420959, -2.871221307424717 57.70346939394719, -2.869908798685759 57.70426286775881, -2.870336690532734 57.70367512305651, -2.869417787945286 57.70318138569777, -2.867111108335387 57.70337076063286, -2.866927387241386 57.70385015849564, -2.865170636859254 57.703469983860074, -2.863491202032665 57.7040301656531, -2.86356991428228 57.70471475757676, -2.863316834417639 57.70404141926018, -2.862457481535912 57.70406342413513, -2.862427506635652 57.704876861424985, -2.86225894144924 57.70404345832212, -2.861498860553134 57.70405016380222, -2.861110757249628 57.704924671046534, -2.863574692121571 57.70487638297021, -2.864192006404638 57.70521357313113, -2.862569129878288 57.70525445197469, -2.862637008656823 57.705572364841615, -2.860982210493588 57.7060574905532, -2.861254261623852 57.70567142965382, -2.860618106300423 57.70545745690328, -2.859516735892218 57.70640338950356, -2.858051885062016 57.70631251957636, -2.859134991665976 57.705509122931666, -2.858436148584858 57.705763808232376, -2.858509691089504 57.705357778270134, -2.857911554461384 57.70542477159622))
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>