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/S02000921 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000921 geosparql: asWKT POLYGON ((-3.873573908914499 55.92255039629667, -3.861744581154076 55.91957820946403, -3.85744589681585 55.91748253934975, -3.846278398744298 55.91416371897125, -3.842921446672342 55.91288400879671, -3.835200925614082 55.91237027921415, -3.833021919391895 55.907900493276415, -3.823641295118834 55.905056283335796, -3.818829494598521 55.90410301007232, -3.818138666919712 55.905748762574454, -3.817794973489994 55.905241598797666, -3.816607344777935 55.90550177297578, -3.81368151886823 55.9048529466593, -3.810160296934233 55.90515639643515, -3.80895900703721 55.90443716386763, -3.813932633368611 55.90235100522461, -3.817829019019247 55.89810582136117, -3.820564259922794 55.896744377813434, -3.822593623365033 55.89636387123268, -3.825604055046944 55.89507912471234, -3.826814099988912 55.89530380626367, -3.828159764056047 55.89433125271603, -3.830454440293857 55.89449485872097, -3.831119649038196 55.89401765753541, -3.827559214159237 55.89148316174817, -3.828122432514861 55.89050760855161, -3.825956742015676 55.88828906176925, -3.826547505352629 55.88792982013806, -3.822787091667756 55.883779876596975, -3.81241729186347 55.88392403668098, -3.802363266394459 55.87570525278645, -3.802972525723559 55.875399778317316, -3.802462083566704 55.87473326986249, -3.798912970642847 55.87472229842447, -3.795421783560513 55.87354213687285, -3.793832725376696 55.87374503917789, -3.787805533060149 55.875459308866354, -3.785988205634201 55.876959478372896, -3.784063152001795 55.87750862100202, -3.782079948950328 55.87644100387773, -3.778599461675328 55.87618581880652, -3.774000005194107 55.88083530109626, -3.770347892417493 55.88103167516222, -3.770851579771839 55.878049917358744, -3.770160243243923 55.87690959874079, -3.762113724002047 55.87290030082852, -3.768445533838039 55.872407784756334, -3.773678777994998 55.871285977777994, -3.773455709426062 55.87080838141853, -3.774386768971925 55.87025757295043, -3.785080987426652 55.87080997825174, -3.788924386233112 55.87230975021908, -3.790267571754919 55.87087604585251, -3.791090477377739 55.87126003948258, -3.794712835805824 55.871235537209365, -3.79788617395283 55.86946754858235, -3.801557756377684 55.8733552533015, -3.807232961656247 55.873231102174906, -3.807268051674622 55.87358439321325, -3.826975055756432 55.86980659531741, -3.820653631386192 55.866215805200156, -3.822977636093356 55.864653684774915, -3.826173502135366 55.86362674960435, -3.84379428266162 55.86384922727397, -3.845617077220297 55.86353432607826, -3.848519216865663 55.864074882074895, -3.850624195905115 55.86297383036732, -3.857941346917952 55.86394194942095, -3.865988915386532 55.862804665323736, -3.874269996168448 55.86254399318023, -3.875595011143611 55.86317982582404, -3.877911172148722 55.86249745036031, -3.875252393711841 55.86038120600077, -3.877734689480675 55.86049612215843, -3.88710632139538 55.85769267332512, -3.894236417138718 55.86204049660237, -3.91701937118158 55.86000707648485, -3.904613864452882 55.864459463429384, -3.90872973457921 55.86854754440542, -3.904032230178884 55.87053449740096, -3.904753596493928 55.87155679727057, -3.906409983765806 55.872088321795566, -3.909789547036056 55.87288066611389, -3.913821919366015 55.871344194986484, -3.916189407737237 55.87008514915684, -3.917991210349337 55.86805303791265, -3.918303632942407 55.86661028960539, -3.920805574458711 55.864845800864295, -3.922843803134389 55.865344179236004, -3.922854473126088 55.86588321998116, -3.924733492243283 55.86574599560142, -3.928707293893988 55.86660041148339, -3.932458379365964 55.868437762015404, -3.933417625187521 55.87037281973862, -3.934564162725997 55.87092995116384, -3.936277307913488 55.870345823610776, -3.939730499499913 55.87058064734587, -3.94184158664425 55.870707471264716, -3.941126391448247 55.87105312817622, -3.942212840922683 55.87087197665618, -3.944762592060272 55.87158697707474, -3.944297225816558 55.87314905074621, -3.949286532540815 55.87351045911755, -3.949535350540076 55.87400978178947, -3.947423179424133 55.87428586966306, -3.943839105456143 55.875816370101575, -3.940265935419822 55.876609677533, -3.937241215154969 55.877825610379666, -3.93771162879715 55.877961991960504, -3.934283464369591 55.878626655348924, -3.935126706513758 55.87945662666409, -3.93854384717062 55.88037965470189, -3.939086538145788 55.88083864336421, -3.938019015471697 55.88125329106367, -3.938431024733707 55.88164640877515, -3.939443792233568 55.8812997435366, -3.94109605961936 55.882418283396, -3.943736033550715 55.88175045130123, -3.944203978711647 55.88215526523211, -3.941796466294598 55.88246567823128, -3.94168420101394 55.88292297955358, -3.939979851891924 55.88337223279876, -3.931634354067071 55.88467178187975, -3.928468574596635 55.886285123694734, -3.926293782032569 55.88563619786604, -3.925801638923238 55.88603032741365, -3.925972158973516 55.886890384647096, -3.924591506275807 55.88807126570478, -3.925415465159272 55.88985572680138, -3.921971589195126 55.89135641020617, -3.923701890347767 55.89238981571024, -3.923219014491166 55.89394307900053, -3.926973561263379 55.896769086054114, -3.920688243494049 55.89839506631785, -3.896773094581264 55.906853887862695, -3.891841658135085 55.90793150663559, -3.892078997121871 55.910988078015244, -3.890877914541714 55.91129410300388, -3.892104941644293 55.9131624419867, -3.895770628051925 55.915487508406535, -3.894491657273562 55.91583970172252, -3.895870910402091 55.92049152122023, -3.873573908914499 55.92255039629667))
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>