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/S13002997 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S13002997 geosparql: asWKT POLYGON ((-4.39047710002484 57.611377394242,-4.39596527740444 57.6014844295053,-4.39169514346521 57.6065245718883,-4.39281293435916 57.6026728238019,-4.40161588937181 57.5963559009653,-4.40220421717913 57.5932742702083,-4.4203339692475 57.5846333961335,-4.41932321994858 57.582312816869,-4.42328897203982 57.5781257765536,-4.43084758686086 57.5737814404649,-4.42010492785314 57.5624733710808,-4.41656883325557 57.5560443467716,-4.4072108282714 57.5516180083726,-4.41968700150377 57.5420655885341,-4.42435378792499 57.5341098040653,-4.41106120497484 57.535798687595,-4.41057806242321 57.5338210934957,-4.39842599570001 57.5353582954674,-4.39878458323378 57.5385504913072,-4.38990987141217 57.5424218476762,-4.38770679748012 57.540982763747,-4.38023651166406 57.5436354022858,-4.364611646498 57.5380946976306,-4.33613974754357 57.5406504264366,-4.32055548659865 57.525136771316,-4.28260410315492 57.5105511443424,-4.27977237212059 57.5023868422869,-4.29216108546204 57.5033969026365,-4.30834649930606 57.5013648311322,-4.30289398931446 57.5005812169051,-4.30481447951056 57.5004652856776,-4.31728548771694 57.5013994198713,-4.32778730359966 57.5039843836477,-4.34567504890011 57.5040006873917,-4.35496470845016 57.5060452288372,-4.34497248111313 57.5034054170192,-4.33682136283378 57.5029870980478,-4.35695999723215 57.5020399242224,-4.35726480784097 57.5010727611528,-4.36409299936964 57.5003159411656,-4.35998945241793 57.4997099026243,-4.36499610687382 57.4998272115611,-4.36530307172555 57.5023778836082,-4.37146450870503 57.5030520615514,-4.36983603685863 57.5013627547373,-4.37205971016941 57.4989757079642,-4.37268570873949 57.498977406196,-4.37276250612789 57.4998529891804,-4.37410078801733 57.5010804173425,-4.37673021884663 57.5010605334023,-4.37441492951589 57.5010583330749,-4.37318609686348 57.5001559264768,-4.37294009852302 57.4990399929382,-4.37410722810583 57.4985058934081,-4.37237280023856 57.4977567282729,-4.3733322411075 57.4974960391072,-4.37674721670843 57.4991929658902,-4.37913775077174 57.4986087149612,-4.37823231481457 57.4997499522648,-4.37928562891043 57.5011614669169,-4.37850126992931 57.4996766076108,-4.38273461040345 57.499376511533,-4.3797010588476 57.4982880757051,-4.3827939645892 57.4984947658805,-4.38789196576756 57.5041752647802,-4.39110214199936 57.5021939733371,-4.41173618512179 57.5018267208361,-4.42101078041491 57.4952688663295,-4.42924748840171 57.4923570252779,-4.42862531488752 57.4939937389805,-4.44303356650621 57.4957758275091,-4.44416562012336 57.4936367728739,-4.45432102877131 57.494186297627,-4.45795118302232 57.495852531087,-4.456616538891 57.4998333126586,-4.46428938469305 57.5050760904667,-4.47228685504054 57.5069395819899,-4.47465656423589 57.5101793452472,-4.47863240348911 57.509494707311,-4.48159139032309 57.5109021741518,-4.49207666379119 57.5099405446888,-4.4973701540874 57.5073367983901,-4.50393411463844 57.5071320813667,-4.50298197926555 57.5063206324818,-4.5102999853884 57.5065715319218,-4.51120271430363 57.5047670865489,-4.51798930314894 57.5018900581789,-4.52810148342503 57.5028721337385,-4.53438725601636 57.5011958327754,-4.53717168073135 57.5037682763191,-4.54404042913896 57.5017427822823,-4.55004516144099 57.5031087439951,-4.55572008089374 57.5027125780718,-4.56347268165917 57.4971463794917,-4.56789199983004 57.4966871327086,-4.59241658508897 57.4985513459056,-4.59258830329381 57.5005024252196,-4.59668970540048 57.5022638480049,-4.57691686560902 57.5127611100765,-4.56886377933016 57.5126009925566,-4.5582732284238 57.5144768954381,-4.54475855194484 57.5195318472144,-4.54196412876931 57.524277204446,-4.54299987378905 57.527036675748,-4.54062148416811 57.5287479165454,-4.53169466764482 57.5290451505898,-4.52272570320153 57.5270420782999,-4.51764002156737 57.5282165823256,-4.51372803134974 57.5276637663688,-4.51410284493935 57.5236224111523,-4.51047361308195 57.5232382358173,-4.50978431452383 57.5242655652414,-4.50494579461668 57.5244919977632,-4.50416277735798 57.5235711911275,-4.48193807670066 57.5279966460974,-4.47660329386551 57.5248557666031,-4.46783888440424 57.5313015440722,-4.45747316680305 57.5432439830786,-4.46299203288709 57.5446891653525,-4.46176441846387 57.5489365653559,-4.48078501862279 57.5475800593081,-4.49075389462829 57.5492788493783,-4.49906092075759 57.5525221505393,-4.51349599338469 57.5545440767885,-4.51977032068432 57.5527843222941,-4.52387178304287 57.5553811738752,-4.52876959051729 57.5544114608988,-4.53892916241672 57.5566041352956,-4.53338123760957 57.5676841061528,-4.53626280943152 57.5698745631508,-4.53104890303595 57.5716173564557,-4.52916149578051 57.574955826994,-4.52284879486529 57.5750524824362,-4.51910109032953 57.578013628042,-4.51762300944999 57.583421262598,-4.52165528471529 57.5854804046106,-4.50537972744022 57.5921053667679,-4.5089274483845 57.5946318745906,-4.50442497451029 57.5951112104457,-4.50531968264677 57.5969742403751,-4.49319191017212 57.5997042489577,-4.49646564266896 57.6039483908909,-4.48793762802395 57.6045254828765,-4.48908602996187 57.6058220698693,-4.47398253781893 57.6086275542065,-4.47922382213449 57.6150081757579,-4.47122900323154 57.6174112702753,-4.47670837417681 57.6211760658317,-4.46815577466268 57.6250581094509,-4.47101080219979 57.6279560245185,-4.47590362638498 57.6260855082389,-4.48236642647931 57.6267081930643,-4.48618666327128 57.624422552582,-4.49824519005395 57.6229221895679,-4.51505856117257 57.6350018666839,-4.52050217181361 57.6343039224156,-4.52425207129122 57.6387967166373,-4.52435246556336 57.640652130214,-4.5195322871457 57.6431052945256,-4.52502053477114 57.6472057781148,-4.52194404659139 57.6516568625001,-4.51287365743898 57.6544119549305,-4.50566426139789 57.6534227856825,-4.50105269828733 57.6542465191711,-4.49769773208917 57.6575288904993,-4.48850186460439 57.6579609538671,-4.4789724482705 57.6536542645415,-4.45710975423555 57.6483104200754,-4.4519308720252 57.6458275802103,-4.43384635829127 57.628281923046,-4.42861657475442 57.6294320454175,-4.42720160601818 57.6286603551873,-4.42391696134381 57.624241333352,-4.42989025150113 57.6232226305491,-4.42744116995682 57.6202298791022,-4.42234809806109 57.618539430181,-4.41869518388068 57.6115789059635,-4.41056758308085 57.6066645100218,-4.40235357003515 57.6056349388101,-4.39540192574898 57.6102696493379,-4.39047710002484 57.611377394242))
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>