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/S22000076 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S22000076 geosparql: asWKT POLYGON ((-3.801646251740968 56.93588025559666, -3.7834979669573867 56.92308077229589, -3.7406863950511156 56.933608362760936, -3.71026916469047 56.91357862729523, -3.683551922261844 56.91611581139337, -3.6754707476724926 56.932442085153795, -3.6547348904340136 56.93289802544039, -3.6458207580318547 56.923042601642145, -3.5832172096324517 56.93103466069819, -3.558717372044265 56.900345535851685, -3.568460467568072 56.89462163029072, -3.5607818547229835 56.88410329094678, -3.5376553015418244 56.89225532309304, -3.5292499283670558 56.88360747445345, -3.502540528743023 56.88711221163903, -3.4657067386045215 56.87281074041185, -3.429832073152688 56.886546176123154, -3.4212868972469144 56.87957293234095, -3.404961810770862 56.884020776183064, -3.3721380146020383 56.87461515292647, -3.407021536019319 56.84340568408458, -3.3516368262715104 56.82027618976403, -3.3667311935297457 56.8134362976267, -3.357641868837672 56.804434221434796, -3.3895910794546245 56.7682970388861, -3.3778397688260995 56.753230067927696, -3.3578412899532255 56.74765445803756, -3.363946560872323 56.73811804344814, -3.3326598305976893 56.712347293975355, -3.340931583554068 56.701710574956714, -3.3262212868636074 56.68374607659202, -3.344358334579119 56.68239257561506, -3.3347081775239023 56.67216401426395, -3.3629644098449134 56.670551943124174, -3.3633293601636636 56.66394741266267, -3.3617776967725987 56.64942711601672, -3.37542450757417 56.653338188781525, -3.3793387741694003 56.64796691898612, -3.3652074395343585 56.63340002772282, -3.341654304021296 56.62746234179532, -3.3496803953066316 56.61760018123977, -3.3392389199565273 56.612196427844815, -3.350910669002282 56.6055114849216, -3.3382204647029594 56.59584640584739, -3.347938989054606 56.59023389869307, -3.384290851241362 56.59128120744621, -3.373218767609654 56.578753485831186, -3.388173033084218 56.57414001642956, -3.41943793697226 56.60052567006756, -3.425152706047688 56.58532099289984, -3.4627838894842773 56.596239603958296, -3.458438231874142 56.571916010420296, -3.4827880609761754 56.586884321511285, -3.5092852345370282 56.586592532360655, -3.494516712896732 56.57638465646832, -3.51782570727714 56.57610455475262, -3.4973073686463976 56.56287334934396, -3.510207298573048 56.54602315840298, -3.482966792882816 56.5369798752895, -3.5725729021715535 56.525707122790855, -3.5961448159120604 56.51563960611134, -3.597864681118457 56.52367725320424, -3.613208368684564 56.525536538840605, -3.656090437178416 56.51091012896675, -3.6711394580438195 56.515353941201305, -3.676328906487597 56.50442068551368, -3.728282363682784 56.492090167905864, -3.6915509612940998 56.46824548138991, -3.7094322046097137 56.45833211785519, -3.6900080638760153 56.4287753762115, -3.72216514274747 56.41013717195399, -3.7205845737149836 56.4239519906824, -3.7420551990710083 56.435010073301044, -3.8209970479765576 56.425600421694035, -3.8617390172342176 56.44735226420237, -3.89902676857628 56.44384873219153, -3.9227512886839278 56.45039345064822, -3.9465485605934814 56.43789477568814, -3.9675389514521258 56.453600140278276, -3.9802157305351074 56.45054091383339, -4.016498020679036 56.46676947446716, -4.037368997166291 56.457280711636415, -4.09861772409462 56.46534499039771, -4.108805624184544 56.48893640156773, -4.145680146789384 56.50619078370934, -4.14581224725412 56.506274727856386, -4.146830312793451 56.5063240923449, -4.093906801233467 56.55397431663627, -3.9963114001975084 56.58242727340464, -4.0048655025279984 56.586262902200076, -4.0920763087309595 56.57262040115717, -4.161576497252151 56.51442604734079, -4.2589085138086915 56.4854853029638, -4.299587473687508 56.48044999659188, -4.300502137921778 56.48126279407363, -4.300780677729772 56.48143736456618, -4.298168890975778 56.49303367590744, -4.310213507313646 56.515577842753295, -4.333132471800114 56.529307854257944, -4.331225028663783 56.53901783611669, -4.365032602611955 56.5470913203119, -4.378494262654091 56.53359227645806, -4.441977611026501 56.52573664716246, -4.477391072998314 56.513010265617524, -4.494354519462509 56.5126570178906, -4.507415923198624 56.52336259114412, -4.528794789549544 56.5185656137428, -4.528501614942871 56.50489758954335, -4.577263431799634 56.50522004599874, -4.625147470656685 56.486484002197805, -4.628471543124342 56.47430745419892, -4.639066077452529 56.476037273294004, -4.670833245920732 56.479752275992404, -4.686206198553443 56.49054711135179, -4.658299179971117 56.510682552732604, -4.653722616439658 56.526009852223886, -4.659453393223677 56.53445230906091, -4.6976255011676775 56.54104366179048, -4.6975847492619485 56.549955765571575, -4.599524278167007 56.57665892926056, -4.559839604378328 56.57300292368389, -4.609816693008002 56.61480993282619, -4.6264236626726145 56.615597015584314, -4.63679633231476 56.6250253263943, -4.618305324510602 56.63781413109915, -4.628874822805894 56.65378558562437, -4.5911353260942 56.68322488607929, -4.649600938681681 56.64974694645446, -4.666320997494039 56.65049342011181, -4.657082541073309 56.64617211448994, -4.664565847123178 56.64136999522129, -4.700995470723456 56.64558412140297, -4.706461311961146 56.64321416934801, -4.7166429426972165 56.64322275160869, -4.717536232884999 56.676587508238086, -4.657568914433945 56.67474153216574, -4.592015902866685 56.69318241816301, -4.591207645281437 56.70446928330955, -4.605916653016316 56.70795935614953, -4.575995360319956 56.734788960873054, -4.589871678718036 56.759881268793215, -4.546793181773258 56.77236718516841, -4.549652889835294 56.78194741257676, -4.520946168992861 56.805003089549665, -4.460783064756138 56.77812992459638, -4.477348098489425 56.74371477350068, -4.440999049828973 56.73109761511037, -4.44888595152398 56.75607285877675, -4.367333492286113 56.83057687524348, -4.342433500137607 56.82144341538745, -4.303959214801363 56.851397209308125, -4.22759133949452 56.85763631033426, -4.191918285963405 56.87746659435765, -4.205540916029931 56.88444696955885, -4.174492114833151 56.911706826087745, -4.168270714014666 56.90469811824222, -4.111457893382194 56.894731249738605, -4.0408865699122565 56.89617406641796, -4.0347556117721615 56.90148757784944, -4.022892195961678 56.88875268846089, -4.011549614167302 56.89802126834589, -3.9993847932998086 56.89029044377701, -3.9677633319651098 56.89927957099359, -3.969405531261457 56.929377503927604, -3.95068538753256 56.947116339764, -3.938613361027675 56.94862052684921, -3.9363672648717225 56.93542889917189, -3.900287999420626 56.93184052969849, -3.894724337203625 56.9227555334908, -3.8548326870949574 56.922788012887814, -3.82427783805911 56.93838676124979, -3.801646251740968 56.93588025559666))
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>