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/S02000047 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000047 geosparql: asWKT POLYGON ((-2.230453102447333 57.23121240950367, -2.227087937289685 57.22808349856495, -2.223160819844591 57.23052491899807, -2.221879771954584 57.22958398502454, -2.219843650088634 57.22977621156143, -2.215273953291487 57.23008056468225, -2.211739868174038 57.229017610603776, -2.20878076833657 57.23001967688668, -2.206568327134988 57.23128093994075, -2.205551626804413 57.231229791734414, -2.204635949210255 57.230695015665944, -2.205033797812211 57.22929177772218, -2.204889196815413 57.22878232512575, -2.20432302185624 57.225365073956205, -2.202095349051985 57.222795986626224, -2.194193908445164 57.21953429127666, -2.188508221924835 57.218103708566474, -2.186591707092238 57.218071417443795, -2.185081611449279 57.21740892339055, -2.184233225388284 57.21661967016667, -2.183765120683866 57.215713079218915, -2.181000834163537 57.215860825376076, -2.179640198830785 57.215251944952996, -2.179661854933174 57.214625844168104, -2.179666390730845 57.21454744813178, -2.179668919808162 57.21450986801395, -2.179681711767381 57.21429562566526, -2.17968710538122 57.21420572036271, -2.179690079934142 57.2141549041541, -2.179696455663754 57.21405418917974, -2.179702180312499 57.21395543521172, -2.179712243550693 57.213790196843476, -2.179579383257648 57.2129613882906, -2.178234774721791 57.21221772013592, -2.17707311141641 57.211635471300724, -2.174763653929766 57.21022906365751, -2.170100440736868 57.20739619350981, -2.171209098484587 57.20373862768668, -2.172615850937489 57.2037097566027, -2.173486626313147 57.20233416428893, -2.176599203212913 57.202545405760695, -2.175526203797526 57.203250710792275, -2.174781590502298 57.203176754904206, -2.174502969955593 57.20376103884765, -2.177188925389761 57.20347258679728, -2.17724873426476 57.202596148502714, -2.178204260270123 57.202462289538715, -2.176711321184197 57.20176373139882, -2.176841627031691 57.201323387734384, -2.177585612121181 57.20116063518497, -2.178715766879144 57.20213817730858, -2.180155462945941 57.20209120077418, -2.183793409508516 57.20143914500294, -2.183953374147547 57.2003250317647, -2.1856089942237 57.20044835449116, -2.188359536719383 57.201082043895404, -2.188977045611344 57.20388283396158, -2.190744705037328 57.203264085881216, -2.1923794102351 57.20545027971026, -2.193024230790752 57.205072158865136, -2.194781519917928 57.206410422593734, -2.196103076752387 57.20586369948865, -2.196671973681859 57.204967883555085, -2.19593806026672 57.2039090380447, -2.195340071248117 57.2035057416954, -2.194480257385142 57.203668769168, -2.193282531124901 57.2025208031357, -2.193669726734768 57.2001889177711, -2.189930762554746 57.19619212822285, -2.186870367394844 57.19133775598949, -2.185876268871181 57.19106974372433, -2.18589095396017 57.190701420370516, -2.189779310939721 57.19072254715946, -2.189872041840659 57.18944682450056, -2.18270673241496 57.18919696687297, -2.182480420330302 57.18803072600522, -2.183403177988866 57.18641521621576, -2.186811487817975 57.18623552201095, -2.187162682331893 57.186980578630106, -2.189346618416842 57.18700425118675, -2.189177236732473 57.18623197483826, -2.190828765702855 57.18568150445785, -2.191179524802497 57.186327748712294, -2.196993332722977 57.18763026271157, -2.199904244987873 57.187463963225525, -2.200165206475874 57.18677186062313, -2.201936916879819 57.18704748854376, -2.206845803158024 57.189159470921936, -2.207833218998297 57.1886790898855, -2.213699431934796 57.18929192766485, -2.213655716861898 57.188007211260334, -2.217724838862295 57.18786547159244, -2.225954409721886 57.18629686132012, -2.227774328325461 57.19168335142173, -2.232222888961133 57.191306892628674, -2.235688937870236 57.19254905921343, -2.249838528359239 57.19288099745181, -2.256445693085468 57.19360426721719, -2.25876854390133 57.19449779224145, -2.258096441111549 57.195406459414606, -2.260056768307761 57.1964983376349, -2.261723042710856 57.20053403593931, -2.265203430738962 57.200820271758474, -2.263593419294448 57.19906236083411, -2.264257383185616 57.198236220100036, -2.271909420296353 57.1992938571566, -2.279520602949844 57.20126236498474, -2.278966083547295 57.20235054800403, -2.278401976546685 57.204363986648325, -2.279251460227563 57.20507174996407, -2.280581071819805 57.20578742134908, -2.281562596281125 57.206431991067454, -2.281534969358356 57.207150690485186, -2.281255886871398 57.207456745308576, -2.278120231801173 57.21091319246589, -2.27812030315685 57.21312299759165, -2.281860415834172 57.212926008858126, -2.283476157908982 57.21420693700802, -2.281679552396064 57.21526198118879, -2.280919438857545 57.21763518927962, -2.281717010934071 57.218001702499095, -2.281440106938216 57.2186041842007, -2.279047282098355 57.2196784983078, -2.279337190512603 57.220791732629486, -2.278874147013116 57.223065453806974, -2.279180304721958 57.22413374338777, -2.280211351857818 57.224697367536585, -2.279206653480888 57.22542722832036, -2.27952735382927 57.22622599616659, -2.280301901460552 57.22610841318658, -2.280443953411932 57.2270106926178, -2.27917575218801 57.22825551409728, -2.274683131535761 57.22854415925808, -2.274754156642883 57.228944673215445, -2.277095793056697 57.22919713024618, -2.277519384569334 57.22887039237633, -2.277639164888545 57.22954318373585, -2.27450454548447 57.23054776734812, -2.274325522073536 57.23278486244379, -2.275290314717532 57.233676542649995, -2.274691181293949 57.23425140961254, -2.274610046562469 57.23533655205048, -2.272850311062627 57.23482836343994, -2.271300501618476 57.233574120137526, -2.266059310590687 57.23265110974277, -2.2642960425013 57.2316128171401, -2.264112788584883 57.22915188133035, -2.261278081787635 57.228807479921876, -2.259324521627408 57.228928306774755, -2.255772447372868 57.230220158631205, -2.253958028061519 57.23131975441115, -2.252831615461756 57.23372945429837, -2.242462211882187 57.23377686469691, -2.240898392181256 57.235289001260675, -2.237277759955089 57.23384066668413, -2.236522542561337 57.2323149926758, -2.236635887781736 57.229323469304106, -2.234599058447137 57.229390174606, -2.230453102447333 57.23121240950367))
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>