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/S16000095 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S16000095 geosparql: asWKT POLYGON ((-4.658263330122127 55.570194488196044, -4.661782197549543 55.576994522963226, -4.672484527400223 55.5901611522904, -4.686749363401656 55.600637598176476, -4.694758641640812 55.60445009151531, -4.686384053220019 55.610571074236624, -4.687759079569334 55.61543983811272, -4.699935825528449 55.61961450666539, -4.711340161121072 55.633173282100415, -4.712151525146335 55.62721099449589, -4.705174636131747 55.6192023684723, -4.69225414432349 55.61007102975924, -4.697035123174417 55.60592362476695, -4.710052686975625 55.61239250364977, -4.725976260627875 55.61874151655921, -4.750221389539479 55.62646797769405, -4.750792123601021 55.62898916511411, -4.768660859425224 55.632310155763086, -4.776858651392046 55.63624172527267, -4.771858848132548 55.63893559898799, -4.775661702394878 55.64454769793487, -4.775155367206247 55.654259222759364, -4.772677775963391 55.65640973533168, -4.766178572921405 55.66837884806857, -4.767791337564161 55.67583110561906, -4.759615784001701 55.682843327908216, -4.754528123499815 55.683436820549574, -4.734604287092549 55.679868097898954, -4.726467663463322 55.68070178813184, -4.725525486061368 55.67601076832138, -4.718298955217062 55.6755311162192, -4.6997723226867905 55.68513485676344, -4.686223102126426 55.68806242479566, -4.6852596562494915 55.69062641790735, -4.674563638772015 55.69149733359768, -4.669171652875982 55.696414680216805, -4.66184463195079 55.6976596298889, -4.6560861662081585 55.70173764978723, -4.6413895660825375 55.70322070856616, -4.628077424286581 55.71450221061406, -4.620342309848663 55.715452241948626, -4.613571921087935 55.712140508823204, -4.60648841344782 55.71180311465952, -4.579631489093109 55.70541348285336, -4.591242675216285 55.69827561402602, -4.5909235141285665 55.693045638451096, -4.597705990630027 55.68628401328366, -4.605856990500182 55.68475294111699, -4.6098598765346335 55.67782674990561, -4.620706255548101 55.67702922726241, -4.637544709786568 55.671676964395, -4.638611872857752 55.66898344811878, -4.6212035657696475 55.66748714188273, -4.616731324533846 55.663055030620875, -4.603238038657032 55.65868247828639, -4.596820774906892 55.65508107652217, -4.603852131717416 55.647342965518256, -4.593578365508481 55.644180971282104, -4.580963284051393 55.64671411104843, -4.573016357739912 55.64957760521187, -4.570218149656974 55.65517141811559, -4.565185145692722 55.65578368447738, -4.554829143839709 55.66381079984314, -4.5465043115607715 55.66700944001764, -4.5347207943694885 55.66644989660121, -4.531062478357536 55.66065340391566, -4.520585791025019 55.65941480299955, -4.508513505355856 55.66108249336848, -4.502229786026119 55.66514317258982, -4.492638073669721 55.66507059454393, -4.496205825905472 55.65652299660227, -4.510509426713886 55.65652138044568, -4.514707478710302 55.654081380090176, -4.531365156477056 55.65022246323177, -4.535303448080422 55.64758114093063, -4.548120966038089 55.64545217470958, -4.557021902981994 55.64684436741803, -4.558008122893046 55.64137413253975, -4.563657829954004 55.6337381782157, -4.569613099660698 55.63271824468759, -4.558546115817299 55.62586592647391, -4.567386015093354 55.621579672241765, -4.57272529560731 55.623647453617785, -4.584134028615883 55.61862821424133, -4.587465912270653 55.607463375073884, -4.579778171530132 55.602906444863734, -4.592504758489522 55.60174620673807, -4.592230607780546 55.59795074047696, -4.605875553423589 55.59324254160934, -4.6386079034900956 55.56955700430456, -4.647831047227367 55.56335888730233, -4.652088074282808 55.563075290316945, -4.654702294982866 55.57059979284674, -4.658263330122127 55.570194488196044))
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>