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/S01002375 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01002375 geosparql: asWKT POLYGON ((-3.792798363626368 55.940053133966515, -3.790833418377411 55.93633438976645, -3.792518971965233 55.9360852092439, -3.791978975548307 55.92924542174892, -3.790473592441643 55.92818894808187, -3.789394975335035 55.928402326707165, -3.789587189991392 55.92805804990004, -3.786558377569205 55.92624184089226, -3.783383455476466 55.92298086358405, -3.782612011688597 55.92116779283821, -3.780126984366498 55.91932558406873, -3.790419179106991 55.91486287328458, -3.803122120315921 55.91206243805072, -3.800347872614114 55.90813102270721, -3.798165502176714 55.9090256171127, -3.793905596133551 55.90962694893316, -3.786145058320482 55.90960498598056, -3.781287276895946 55.90484965553255, -3.780721854251743 55.904031084123474, -3.782136216656685 55.90312994697019, -3.78149211043366 55.902339477305254, -3.783806819362754 55.90119163984496, -3.79377807914617 55.9002828973425, -3.795226240905356 55.89977651758785, -3.796183008620417 55.90005911392465, -3.798398366861953 55.89954149956121, -3.800681439841946 55.89807028819439, -3.80859175644867 55.897235399589604, -3.810068228094336 55.896656534646844, -3.821550081879131 55.89525600582837, -3.822593618654491 55.89636387309972, -3.820564255170034 55.896744378782394, -3.817829023730233 55.898105819494305, -3.813932638164149 55.902351005153946, -3.810782907142692 55.903412849593806, -3.809097349835023 55.90465979293546, -3.810160296976078 55.90515639733317, -3.813681523789855 55.90485294928273, -3.816607346208595 55.90550176936009, -3.817794978495985 55.90524160321699, -3.818138660314972 55.905748758178746, -3.818829488246245 55.90410301106482, -3.823641298442523 55.9050562859825, -3.83302192418807 55.90790049320501, -3.83520091753469 55.912370277537285, -3.842921448356484 55.912884010568796, -3.852972820050211 55.91630080479128, -3.851821641195371 55.91699211491183, -3.850921172526282 55.916565314164615, -3.849760272688754 55.916717558390694, -3.849105859201318 55.917760839987004, -3.844383373266205 55.91974580101962, -3.840639667177415 55.920781351580466, -3.840337210726931 55.92182830829838, -3.838786494055063 55.922201949059506, -3.838422554915827 55.92330373751416, -3.836302636486418 55.92348813989198, -3.835115999742427 55.92310143732116, -3.833925652233396 55.92399086193027, -3.835231145743272 55.924282518514985, -3.838812761199115 55.92713318203392, -3.840502469239402 55.926422250745496, -3.841392235052667 55.927013969449085, -3.842379628358433 55.92663618793763, -3.844401226294893 55.92854333654245, -3.839277609187639 55.92952760475903, -3.839972733664956 55.93135945394649, -3.838575453478661 55.93232094373179, -3.837581608512204 55.93224887310025, -3.838328796927062 55.9331049791317, -3.836824449223661 55.93409370612343, -3.838435657938087 55.93540835981526, -3.836329334512873 55.93587459950565, -3.8360034544859 55.93551194835438, -3.83288301119671 55.935805648006, -3.832943298351059 55.93620514262603, -3.831752654884354 55.935998920846586, -3.830671615133692 55.93699190360589, -3.827166679272947 55.9365919378262, -3.83001764968103 55.939209199296464, -3.829450960139548 55.93941532036639, -3.82957249970288 55.9403022554623, -3.828043236196217 55.9407889721656, -3.828014890663306 55.9415528651036, -3.831579670584178 55.94118185410735, -3.831839010628408 55.94199650508231, -3.830599734882266 55.94229094122163, -3.82999009169652 55.9430378451837, -3.829576495554781 55.94275642240723, -3.829084344475749 55.943186095697094, -3.828438770123832 55.94307885660739, -3.827715035502987 55.94369169104994, -3.826835701836565 55.943381223349526, -3.826325819147889 55.94377520241073, -3.825200181573828 55.943675063434306, -3.824833081711367 55.944381446273844, -3.824011429798076 55.94393530709283, -3.823306528542027 55.94426925590386, -3.822609537792635 55.94374937050845, -3.822103403447812 55.94422415428447, -3.820554104750883 55.94362699811595, -3.819228042907473 55.944374492891264, -3.819585576000266 55.94482752056512, -3.816213236896127 55.944967155945584, -3.815701601925494 55.944642200629964, -3.811541163206754 55.94573689797921, -3.809268745516148 55.94405388934797, -3.809552346474428 55.94326790396451, -3.808473606857658 55.942798480017935, -3.80703239427392 55.943484627585626, -3.806393675697656 55.943179471484555, -3.805281772948058 55.943716985503585, -3.803554085669829 55.943409797471375, -3.803202177699009 55.944106903871194, -3.802258452931932 55.94412970220036, -3.801920884233946 55.94375721152011, -3.799231318301663 55.94446152218179, -3.799124999708006 55.94320497776795, -3.796977587510496 55.942822950351, -3.796500404037352 55.941508909236475, -3.793998591780199 55.941078080908134, -3.794471585600932 55.940226468621354, -3.792798363626368 55.940053133966515))
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>