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/S02001063 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001063 geosparql: asWKT POLYGON ((-4.591463495665752 55.46045646520086, -4.590396929773731 55.462043100426975, -4.592763700151504 55.463152509135284, -4.591592783135069 55.46435484235496, -4.593251584044747 55.4652814830692, -4.593346140699722 55.46720307667092, -4.591447379806556 55.46768375824184, -4.592113695038347 55.46843368638681, -4.59016901600333 55.468456900686476, -4.587091693889477 55.46954673375172, -4.58733460455591 55.470324675005614, -4.584382932370653 55.47070959949936, -4.584497585938142 55.468352134970594, -4.585624172893966 55.46646766478771, -4.58406994137885 55.466653310146754, -4.581728363859678 55.468590380547106, -4.580249485552513 55.46847776275187, -4.577695911827907 55.46596082433995, -4.571237482193006 55.470285475572794, -4.569423786581924 55.47037751968505, -4.566467616800694 55.46750027397993, -4.563547060542684 55.46881992742755, -4.560392690797444 55.468733176770485, -4.55693468577854 55.469821224109644, -4.557134936013317 55.4709585941289, -4.556308030969726 55.472117438799096, -4.552711383122063 55.474493630479486, -4.55180926654666 55.47791914162321, -4.550618682740907 55.47884282776429, -4.547349823484546 55.478955873994096, -4.544546488446635 55.479661382800465, -4.543014648716252 55.47873145119384, -4.539423308943193 55.47875211476615, -4.535568782827186 55.48034213302845, -4.533313589332999 55.479975371203544, -4.532259053661028 55.47908036883163, -4.532931888255174 55.47772714069489, -4.531589095824566 55.47603811724358, -4.530268631002807 55.47543622589099, -4.530517424926634 55.47487378723517, -4.529140384187273 55.473634874112236, -4.528058278567323 55.47403476297632, -4.525647795977465 55.47271828051171, -4.528155602258476 55.47135415139516, -4.526638527137545 55.470830463817904, -4.522499226679463 55.472477628983036, -4.519465079664815 55.47204577892865, -4.519301831937404 55.473703040776925, -4.513558496669572 55.472724566042885, -4.510807906467096 55.47326642383151, -4.505896855300222 55.47517381351709, -4.503594180035631 55.47779166287638, -4.500630101711457 55.47796913545799, -4.498393021753584 55.47938108282974, -4.493853982397611 55.4801837781117, -4.489094216015011 55.47899534091586, -4.484051967322465 55.4788461338129, -4.485482744650423 55.47788753704545, -4.482398987276344 55.47219226843275, -4.491826107211424 55.470877066263924, -4.493177662568811 55.470229322444816, -4.492793791655027 55.469913561605274, -4.493557327330304 55.46772277636767, -4.491849985705942 55.46699354988386, -4.490907781316778 55.46560153789883, -4.493191691754586 55.46393708409699, -4.491056793197932 55.46319857624303, -4.490896314873328 55.46215918068819, -4.488486737026812 55.46132735054595, -4.489385136754358 55.458765327247406, -4.490978900642778 55.457950880939876, -4.493888046559396 55.458179235550894, -4.494150797662516 55.457580637200216, -4.493154827562472 55.45708858817756, -4.490294818812893 55.45663450485366, -4.486728586649606 55.457021658559306, -4.481347777942258 55.45823651305174, -4.48066906911897 55.4570008819715, -4.481404834102313 55.45561971294583, -4.476627497908695 55.454601962404, -4.477866665301814 55.452419630381414, -4.474059569128377 55.451229325653614, -4.476223017334652 55.45092487250417, -4.476818237651928 55.45032857459996, -4.476273380005326 55.449962087331485, -4.474973085838717 55.4501591911694, -4.476021340285287 55.4492211433811, -4.474562831377858 55.44815407332048, -4.478455551431821 55.44768874671005, -4.478680162318499 55.446731412235074, -4.481095441492919 55.44666446215615, -4.482081970701527 55.44676129259312, -4.48201710015562 55.44623228949161, -4.482377541685588 55.4449216369921, -4.48362292262077 55.44486040106668, -4.485590636210774 55.442456446058216, -4.489098857769406 55.44367943936482, -4.490055442601509 55.443803782617756, -4.491701459175374 55.442323110047816, -4.492864494873859 55.44271287234169, -4.493824893297768 55.44239667558525, -4.4943101811732 55.44131714218919, -4.491782839790827 55.440604637631296, -4.493316861348167 55.43860488390594, -4.491981887424004 55.437993908530494, -4.489141402506388 55.438078728408776, -4.487563337133727 55.436367066911465, -4.485700733428417 55.43591057302911, -4.484668814163728 55.436596689648084, -4.483118641153101 55.43683491299883, -4.482628619500241 55.43532580462621, -4.480547897115024 55.43466692571531, -4.477418272185051 55.43517979461651, -4.476573792221082 55.43381267790738, -4.478183777693794 55.432764354496, -4.481114167176976 55.43210270624263, -4.480983940609821 55.43153906771654, -4.479782457505926 55.43103311933475, -4.477991085936433 55.43170761944996, -4.477232260648798 55.43094997807411, -4.475178851522889 55.4307219144708, -4.473328499624461 55.43020206856302, -4.472062091231105 55.43042542157159, -4.471084579383255 55.429959805155804, -4.47151681168214 55.42878256603173, -4.468510835778375 55.42823206470305, -4.466340079540429 55.429920762982015, -4.465154257947585 55.430169386738584, -4.463676864820886 55.43055870637931, -4.463053635915775 55.430957764340114, -4.461810601971935 55.43054235812878, -4.458565997898167 55.43022113700747, -4.456338535908578 55.430751285495916, -4.454329078012483 55.430719723394844, -4.454642008254439 55.42914046722582, -4.453716907749084 55.42824220657585, -4.452153459497566 55.42902860786215, -4.452235058074714 55.42779555397051, -4.451968399133667 55.42706384073665, -4.452592832003791 55.4259187802579, -4.454110284803367 55.42541194329639, -4.453214427097642 55.42472881990696, -4.452696736612125 55.42351677156557, -4.453240857340041 55.42260701202744, -4.452323183665818 55.42182544919004, -4.453341529795559 55.42142750027034, -4.454615928359591 55.421590680736415, -4.45517466826495 55.42142666334673, -4.455062663495487 55.41962223195941, -4.453765132286637 55.418830322307656, -4.452397995879579 55.41844429253558, -4.45109144333563 55.41776040140066, -4.451644639100369 55.416742601643456, -4.45320706294291 55.41594723804474, -4.450154085714551 55.415127594685075, -4.450897541656109 55.41437563922486, -4.451106994825091 55.411890626446294, -4.45377102593267 55.411028216049814, -4.454362868257777 55.40936245979961, -4.45822990429736 55.40828702934855, -4.4606974820442 55.408336253507194, -4.465287665046094 55.40746176685862, -4.466975448960912 55.40589967490821, -4.466560090392582 55.40481145509699, -4.465264489724859 55.40404660873183, -4.468154362904158 55.40276584621451, -4.468829078418964 55.40345332637331, -4.476798218038225 55.40105395980691, -4.481524621356034 55.40085922088959, -4.484406966802574 55.400225399897316, -4.487450328788375 55.40162326524607, -4.50135940347084 55.39904370297319, -4.501958135699681 55.402519023826756, -4.510018013546001 55.405320013310565, -4.51678930236483 55.408956010532414, -4.521611011045908 55.4075173306426, -4.527590837248859 55.40912863066158, -4.522939784602018 55.41101345554803, -4.525250966250393 55.41380614759047, -4.527138783241121 55.41417162152448, -4.528387973365976 55.41591655693025, -4.52922061476665 55.41608809660015, -4.531328762096145 55.41816578787757, -4.530794265435893 55.41944425096291, -4.531503768854909 55.421640765913196, -4.533578542843692 55.421957338175815, -4.535576750865077 55.42403723861081, -4.536041399409956 55.42413545811463, -4.540034770525661 55.42331548957349, -4.541864252056395 55.424239262362455, -4.542741029082736 55.423618786817535, -4.542374001842764 55.422826428473165, -4.543324854258798 55.421638124630036, -4.54578826193037 55.4206250568555, -4.551475650044617 55.422555923527916, -4.552073689084846 55.42349623703407, -4.550959986323208 55.42460711073808, -4.552157989411278 55.4262719863865, -4.553562997960847 55.426979722375044, -4.551349342972725 55.42843716618662, -4.551255930232211 55.42919416262754, -4.553144900537387 55.430296293440776, -4.555590917709385 55.430721596359916, -4.556423887760434 55.432358108842294, -4.561330586945215 55.432453172331144, -4.563072231200616 55.43298296139758, -4.565630372217067 55.43269561145544, -4.568188292362532 55.4333789875383, -4.571058019441018 55.433507445091955, -4.570253715733446 55.43134908190776, -4.571693873347721 55.43015924830255, -4.574590268642265 55.43021514512972, -4.575015326948085 55.42921743087578, -4.579441743007321 55.42995109599962, -4.580129158044375 55.43346019082857, -4.587926533482854 55.43448192410147, -4.587805175979268 55.43528449254771, -4.585169475895051 55.43632001745721, -4.587632806153314 55.43748139896188, -4.588043811574996 55.43723899813338, -4.590683114294857 55.43939434482658, -4.591745974320456 55.438724634983636, -4.591673082617658 55.43785426461992, -4.59241339025642 55.43781160295754, -4.595026232202293 55.43570673909562, -4.597535568242672 55.4361208746895, -4.60201809558201 55.43936941301631, -4.601496293322435 55.441526147698205, -4.602493220693948 55.44252013063884, -4.605078883865074 55.44166829446809, -4.605724869747313 55.442832068224085, -4.599964040458677 55.44443783712398, -4.600772078907477 55.44494200896704, -4.600128828386995 55.44490260477912, -4.598836841307908 55.447976413992755, -4.597700884815538 55.45090392732473, -4.595163915036917 55.4520094655683, -4.596997040385994 55.452707641725624, -4.596206757022381 55.454881725442476, -4.594681926747912 55.455507340750444, -4.591463495665752 55.46045646520086))
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>