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/S01010727 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01010727 geosparql: asWKT POLYGON ((-4.215050726474431 57.617175058363486, -4.220861204834346 57.61580615219759, -4.223852632756991 57.614342858490836, -4.230166832582126 57.6102780612603, -4.235624436782006 57.607279368272145, -4.260698899487258 57.601316169301526, -4.264770879829226 57.600048179369736, -4.2709575486472335 57.59753801516054, -4.269431703857225 57.59769418084492, -4.265517241420264 57.59147039683841, -4.261916736419358 57.58601838068676, -4.259162069305955 57.58633577473151, -4.257856039768803 57.584959124165174, -4.260374139518142 57.58440769144968, -4.2565595546326636 57.581943983249374, -4.25973978341719 57.580447193786725, -4.270884176316927 57.58798748898907, -4.272536182151726 57.58876967465834, -4.277932447442674 57.59052062973378, -4.299744194860236 57.58669290267471, -4.3108953602586375 57.58497978456818, -4.3188202323495135 57.57333305801859, -4.327672250249424 57.567644063779056, -4.3325044924618545 57.56423920318957, -4.333876956240278 57.56347702016205, -4.341584757009065 57.55774523300653, -4.34380768993003 57.556311162280934, -4.34718742238104 57.559630349177176, -4.347344837741388 57.56087396960834, -4.350009252217071 57.56261112395755, -4.34742952584589 57.56437077269128, -4.3480629064418475 57.564885280829195, -4.345748179995882 57.570229818617264, -4.349591058262954 57.57199142472536, -4.351252451763053 57.57078338044383, -4.354039015892219 57.571980456259396, -4.355514896823029 57.57100943929872, -4.360593187588966 57.57350245551521, -4.364235946770656 57.570873386575755, -4.366109397993635 57.57181641462391, -4.36602316244516 57.57218859905131, -4.371204160355488 57.574642485817186, -4.3668680148124 57.57663778860159, -4.374317462429924 57.580325821944896, -4.369118301369971 57.583721208647944, -4.370289576045948 57.58423836816023, -4.367762864655197 57.587062323492525, -4.370759478763106 57.587859749720295, -4.369410973925287 57.58847810987924, -4.368148641654396 57.58810641889252, -4.36795440166241 57.58949384945455, -4.369422293978337 57.590966920239616, -4.367411343230946 57.59143594211362, -4.359861186460682 57.58990755430978, -4.3605085185366885 57.587929664240164, -4.358834476709133 57.58735964375267, -4.356651607028218 57.58691472473667, -4.354738644833066 57.58597907314993, -4.34988737347479 57.58436806921065, -4.347604598530955 57.58490929888166, -4.346830240825863 57.5855943668381, -4.347514355498476 57.58618295810542, -4.346095703680959 57.58726025680415, -4.349078028216638 57.58845500992833, -4.3475081685305055 57.59012643228716, -4.344825047188807 57.58949570009682, -4.343719968755467 57.59096128112024, -4.342712195278932 57.59272317840143, -4.341752380280771 57.59326525870508, -4.344051174010119 57.59412898101509, -4.343083017499079 57.59608939932169, -4.341764156767834 57.5966827068837, -4.342237531569396 57.597178450742184, -4.341422418658156 57.59792078224011, -4.34277450910769 57.597940944658454, -4.343653051703119 57.596623384573725, -4.344448020823692 57.59682787018769, -4.345339842284403 57.59620549776001, -4.346430204689224 57.59653195486613, -4.34488971205876 57.59834815159602, -4.34361100775003 57.59864958210147, -4.344009294517076 57.59955957244762, -4.341717677057141 57.6002322698585, -4.34075885375246 57.600082622661375, -4.338723821763873 57.60155243350895, -4.333337365858716 57.599313444786866, -4.334019059122851 57.59885950795141, -4.335367469832977 57.597441826390124, -4.335586910716934 57.59540021666541, -4.336738572272657 57.5940800278465, -4.33545049534455 57.59338336413533, -4.335173856010487 57.592889484975366, -4.336280462747088 57.59062951594152, -4.334537595234624 57.59016229166362, -4.332318069315062 57.59163959294186, -4.328892167654423 57.59039991882073, -4.3259753445987466 57.59241251464983, -4.330446150056458 57.5942889457601, -4.321410757082252 57.60002700154443, -4.3246143077880825 57.60228569134587, -4.326185057515766 57.60460199327587, -4.32538336836088 57.6062839322541, -4.322654631393075 57.60830525199027, -4.324102205772151 57.608341842897325, -4.324956123611581 57.60754829225052, -4.326260236872228 57.60680747573154, -4.330544907556565 57.60833981395107, -4.3312210362529235 57.60830056237311, -4.332277184654901 57.60747161748852, -4.333357097493435 57.60798810262501, -4.333858818986196 57.60887652442417, -4.335059973896481 57.60908906681319, -4.337643133634148 57.61038257465836, -4.337920831488732 57.610813360074985, -4.34329006210314 57.612172528124674, -4.3450932337046435 57.612285472659, -4.3461852903812375 57.61182491455431, -4.346627357194114 57.61089119242885, -4.350930281736315 57.61280742768226, -4.350207165376715 57.61330736032624, -4.348984853548644 57.61346217933875, -4.347931795872529 57.61408648127744, -4.347417854596517 57.61473670093183, -4.345706891112653 57.61616932441362, -4.345093415885988 57.61602975514543, -4.343892542785622 57.616441112091245, -4.341848491156488 57.61799017351459, -4.339082413721218 57.61674549161749, -4.336502862862921 57.61598677814841, -4.333649264318687 57.61589681694123, -4.333951564526569 57.615195628589035, -4.325372777492302 57.61405010328257, -4.325946284194734 57.6134391269823, -4.326909843070971 57.611750081968324, -4.323898281173261 57.6096849450225, -4.321475977635365 57.61081672261941, -4.320785008605872 57.609957825311064, -4.317830206242431 57.61035351776682, -4.316274567917988 57.61116378425231, -4.313508619562494 57.610055338983315, -4.3122050603704025 57.61061833075777, -4.312548654496805 57.61154652990857, -4.310869514683962 57.61226014832586, -4.307900318883058 57.61296137356605, -4.305395731578868 57.61439085689711, -4.299941479131603 57.617635157202265, -4.294590434732179 57.61961039429189, -4.29075650632205 57.62148602107872, -4.285799874859095 57.62417411615965, -4.282154267420542 57.622815215091755, -4.280243354731257 57.62232454402882, -4.278721403560771 57.62241502151305, -4.275262381688425 57.621595078514765, -4.27416696720102 57.62026593516125, -4.274127890808001 57.61911093403019, -4.274950837538261 57.61902500142574, -4.276731854707002 57.617784635794855, -4.274096866609445 57.61700881277319, -4.273815858028897 57.61809420549044, -4.272841607263959 57.619205196188126, -4.271634183696463 57.61923580400128, -4.266314947377535 57.62134045570401, -4.26250136170578 57.62493839551433, -4.257125849298509 57.62386131147394, -4.245168290528315 57.618710116473466, -4.23708142441769 57.62226794001744, -4.2290350401499275 57.618312864397886, -4.220220516999442 57.62149606261782, -4.215050726474431 57.617175058363486))
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>
<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>