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/S01006913 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01006913 geosparql: asWKT POLYGON ((-2.3734519307627058 57.318200247603016, -2.370607618795904 57.31517284003763, -2.369105708510727 57.313459199259405, -2.3681965752244114 57.30902629548335, -2.366763081941481 57.30675782377765, -2.361536451825088 57.30693465589244, -2.3611555840621006 57.30363904270702, -2.358748629543701 57.30359203817211, -2.3543960527234375 57.30142155115386, -2.354800630967292 57.300351440531486, -2.356405785822858 57.299879790454874, -2.358716399455385 57.298579693923806, -2.358838647634953 57.29750139217664, -2.3534384950844784 57.29675315949031, -2.3563601552904374 57.29516392481297, -2.358746535625657 57.29288605937102, -2.3597109168935155 57.29275414707077, -2.364356450544484 57.29119338355867, -2.3658152442566136 57.29085951903118, -2.3651698127458367 57.291721497637916, -2.3650102464796507 57.29263991229496, -2.3656619329012494 57.29396233614216, -2.366562429529768 57.29474663063717, -2.370073413366238 57.29718314129855, -2.3712628487193905 57.29790130837618, -2.373470926499211 57.296207810221404, -2.3719866727206087 57.295709027211586, -2.3739793909420834 57.29446551590806, -2.375335441718552 57.295206755878006, -2.376123747432583 57.296048627896575, -2.377489872917782 57.29682067065366, -2.379035194000224 57.29691460703244, -2.38438555110168 57.295551223037656, -2.385465949994066 57.295492705468305, -2.386901458351202 57.296228115791685, -2.386989540778068 57.29684620805652, -2.388418927241208 57.29689246242377, -2.38968248607172 57.297158729408494, -2.3902813407374586 57.29583133053256, -2.3926099582331277 57.29622369081609, -2.3950852509537737 57.2964944039392, -2.3964294501091934 57.296513455175734, -2.399881439470358 57.29656008261822, -2.4016537088711085 57.29671812691141, -2.4017759927124476 57.29530541972378, -2.403417192365607 57.29328611810575, -2.403920716703454 57.29266655881795, -2.40533431489807 57.292967426284356, -2.4056284066114735 57.29255326442821, -2.4099128586808276 57.291848768070174, -2.412470623971689 57.29174858588317, -2.4130556915433177 57.291968631863234, -2.4152666075839306 57.29047365249077, -2.414883370697836 57.28979798469558, -2.4144651764502685 57.28602203538342, -2.4145818816738163 57.285565291813114, -2.4196715137700577 57.284395594781586, -2.4204380266557335 57.28615368321061, -2.4223795515694535 57.286210053354324, -2.423678208835736 57.28807208808912, -2.423758382311783 57.28847278785226, -2.4281334510736934 57.293143354440886, -2.431018743059005 57.29299874975799, -2.4318319017607983 57.29584353891902, -2.4404597315599803 57.29579563620334, -2.445071853759539 57.29436009067019, -2.4462275615736315 57.29525428412673, -2.4490353337929784 57.29802593263887, -2.442461357020812 57.29979956261346, -2.4400874794427434 57.30109971743626, -2.4424120170439574 57.30222952059383, -2.443022952643614 57.30335023114761, -2.4421032944350007 57.30416192925222, -2.438692193869479 57.304820638077025, -2.432649321089744 57.303278565998646, -2.4292006478452364 57.30356885720624, -2.4264855779949683 57.30415300234887, -2.42477720901676 57.30424861531767, -2.423875898013839 57.30236524243469, -2.42491384049329 57.301705996422285, -2.424473051538118 57.300899025528764, -2.4224580860880947 57.300294976863825, -2.420583821222787 57.300382107802925, -2.4179190380541753 57.29953762107499, -2.4165085391178045 57.29954230262962, -2.4154173802922987 57.29990522892053, -2.4144185906776934 57.29963005623175, -2.4140640213621047 57.30056544487781, -2.413602399436007 57.30083645116328, -2.411671205576649 57.30474136106811, -2.4097624124941563 57.30472963162211, -2.4095232413739467 57.30710189023543, -2.4081625426956386 57.30713326402084, -2.4081898598252183 57.311094621324685, -2.410874455727812 57.310681647403676, -2.411458268605564 57.31242241464056, -2.4107134934436187 57.312622472890325, -2.4136959528015196 57.31507400015128, -2.413113265250346 57.31788755197532, -2.4103566975766233 57.317607016018506, -2.40772048412141 57.32057510175409, -2.4057192375047567 57.319465751706254, -2.4043642393933182 57.3194004365171, -2.404414055173952 57.318524474900386, -2.3996284510170405 57.315966771834475, -2.3993799909729865 57.316117386368006, -2.3961131460599012 57.316469069372204, -2.3944744000899276 57.31692336417418, -2.3928638797811583 57.31646535861993, -2.3934220268230066 57.315666340168576, -2.392135195880648 57.31544548344337, -2.3904408414449803 57.31598227195857, -2.3904901208595146 57.317065603587274, -2.391344961398403 57.31929124055884, -2.3936690757309473 57.3211034164899, -2.391938886755027 57.32184477701915, -2.391335605772584 57.321523408322484, -2.388601445658516 57.31890889691944, -2.3870489692146952 57.318114212018514, -2.386610883124976 57.315923744510535, -2.383714397188607 57.31677700556255, -2.383175994758687 57.317685915187155, -2.3818361434180124 57.31816608218746, -2.3799210940604354 57.3192228761957, -2.376449769459032 57.319107557534316, -2.3734519307627058 57.318200247603016))
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>