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/S02000796 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000796 geosparql: asWKT POLYGON ((-3.11891490784447 55.866371484368216, -3.118397208055418 55.8666409998099, -3.118029992369371 55.86609622446006, -3.115714108674978 55.865578111125664, -3.107383605560627 55.869319437769015, -3.102853650538782 55.87187605816024, -3.103665783544597 55.87233601980268, -3.10277185463031 55.87293707425982, -3.101357876926569 55.87300538542405, -3.09927145075347 55.87518780405056, -3.102289307374093 55.87621211361509, -3.101426249471654 55.877343021973, -3.103966720340102 55.87843446076537, -3.100378481334875 55.878161089373094, -3.095428433277268 55.87777394458863, -3.090877517249432 55.878506243806534, -3.086189028666007 55.880614371115875, -3.086399129139673 55.881268453738734, -3.08542050081313 55.88197172511347, -3.084281763190016 55.88165430363104, -3.083919897125613 55.882130381515715, -3.082804706177891 55.88195814511043, -3.082814962350998 55.88340167874952, -3.081889156907715 55.88353304088592, -3.081536556704287 55.88273418181442, -3.078907425576097 55.883285987195706, -3.079521816191479 55.883838738758485, -3.080132706196844 55.88364847342225, -3.080422199153824 55.88463070709626, -3.081095402154897 55.884583998533046, -3.08203387865267 55.88547353945811, -3.080925171813263 55.88562067089827, -3.079812590457886 55.884154932218465, -3.077079391482837 55.884520166531104, -3.074115102233458 55.88631770152805, -3.070913695006617 55.88498080091395, -3.07346694228253 55.88416740439976, -3.073131173561343 55.88358628463568, -3.074918750345831 55.88174663580329, -3.078916680115741 55.880107840074956, -3.07932693068905 55.87757476022528, -3.078303954351848 55.87757473818941, -3.078368896095718 55.87703503969872, -3.080551914292874 55.87621618849351, -3.080760966004467 55.87570002025487, -3.080631058667961 55.87535292711503, -3.077378806338819 55.87533647508163, -3.076382016429773 55.87397042447918, -3.075595186061311 55.87499266296393, -3.076482146916645 55.878165754770926, -3.070953572636504 55.88055918713177, -3.070269833618858 55.87929782278671, -3.069702242975636 55.879332808109, -3.06786630448356 55.88164628895406, -3.064401197493543 55.882952235087934, -3.062120048787694 55.88898318197531, -3.060298314273892 55.88843281016845, -3.060670005636561 55.887405272710886, -3.059632032750208 55.88510495674708, -3.05780553932421 55.88495893061554, -3.057488211833342 55.88563556236857, -3.053278224513798 55.88366791719958, -3.0558106552312 55.87684425695179, -3.058584079721836 55.874798717369764, -3.060777546396051 55.87318041557067, -3.062250005957853 55.8736359673488, -3.064750496570034 55.87177133507597, -3.067835203996178 55.87178055549594, -3.067934224622329 55.871312446898685, -3.065808215551917 55.871294937973964, -3.065641287615633 55.87045175113615, -3.068578569767252 55.86975237423224, -3.067856899322025 55.869659800547026, -3.067803728393911 55.86888750724103, -3.070064499498928 55.868580334942074, -3.069125790882215 55.86682246217637, -3.070694719827531 55.86631782260686, -3.068798756469595 55.86521583697156, -3.068745140291255 55.86243445873175, -3.072254815118998 55.86234340966883, -3.071538298128535 55.85768612121081, -3.073405347328821 55.853554507237334, -3.071684784750932 55.84966082085939, -3.074454938122466 55.85049027575211, -3.076463139942969 55.84975388210673, -3.077977076466946 55.850782949382506, -3.079663298238641 55.851109607861474, -3.08916252673468 55.847512547575704, -3.091440817086585 55.84901989479859, -3.095182305824182 55.84802519705731, -3.09994606779303 55.84082123317523, -3.106684355669919 55.84248601696115, -3.110426880600909 55.844923313876095, -3.107668497326448 55.84620615777096, -3.110768201977224 55.849610680086094, -3.11803765572707 55.8485654019442, -3.122145646086951 55.85255351885307, -3.12549367724831 55.853987580927324, -3.117870366173574 55.857202015335346, -3.118240595437408 55.858411696947705, -3.122443667921937 55.859011398963915, -3.12311537819647 55.85958932300776, -3.127344698670618 55.85943383653246, -3.128088805715981 55.85975049287088, -3.123822574262886 55.86360837852473, -3.11891490784447 55.866371484368216))
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>