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/S12000014 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S12000014 geosparql: asWKT POLYGON ((-4.0201297707821 56.02804308992718, -4.017855504411445 56.02976171033844, -4.013081772880251 56.031137401462495, -4.010395956360717 56.03087836030284, -4.0012991893994645 56.03494636357195, -3.9922979523723567 56.0381952089008, -3.9870127794430004 56.03927617729864, -3.978374810238863 56.04034965122806, -3.979150532917549 56.04361548798398, -3.9759481930904967 56.04764793190402, -3.9733633289544845 56.04862841363171, -3.961263414685222 56.048199519274554, -3.953635559978765 56.05002617699413, -3.9469065431073496 56.049180890954304, -3.9428351621431426 56.05098402092655, -3.9393978035666315 56.05077299407832, -3.934632436733892 56.04940724495847, -3.9303394116558654 56.049902315048435, -3.9251241524447607 56.048843395535116, -3.9237477781761463 56.05003134229718, -3.920014189195947 56.048346988318286, -3.9146341208297666 56.04930052574358, -3.905274351743535 56.04776770240422, -3.896981379834707 56.04545207728433, -3.8931934316156545 56.04780179232557, -3.8901319832381347 56.04853528793858, -3.879421013119894 56.047027497943496, -3.873256486486094 56.04877115055791, -3.8736201240124823 56.05040841295367, -3.8656399246030166 56.049653168839924, -3.8606354197890327 56.05069658019356, -3.857781533594888 56.04995413884144, -3.841379403569449 56.05076981487459, -3.8345992598392726 56.05190693181302, -3.833147591170118 56.05041888102872, -3.829283278930298 56.05128047040853, -3.824820160101841 56.051061697206066, -3.8207045584698527 56.054400762199236, -3.8138017449609287 56.05346446463627, -3.810702625822698 56.05559304394344, -3.815278319126105 56.05826574652993, -3.8147353075472576 56.060039483600974, -3.811908400991584 56.06215959188817, -3.8021751978491354 56.06334092735942, -3.7998207367687518 56.06279575775564, -3.7982597983149877 56.06604545440202, -3.8003069296109877 56.06809137134, -3.8068212880044356 56.06974132503549, -3.807785796220378 56.07291101671977, -3.8128077039989114 56.07485031991467, -3.817856965498082 56.0757196010889, -3.823651253248444 56.078786493579734, -3.829498993549187 56.08095728025138, -3.835897139360829 56.08465357830423, -3.833335883917196 56.08750526044411, -3.8285676953476115 56.08660368701332, -3.826021950900883 56.09030951058032, -3.8231477447360254 56.09108623460134, -3.821865945465951 56.0967224778011, -3.820383839859795 56.099280254396874, -3.8215534065197145 56.10072500484623, -3.8143856568481347 56.10144073055906, -3.8121198635093343 56.10225765439047, -3.803070690396266 56.10865443928086, -3.797236553163383 56.107209143465866, -3.7947241539432777 56.10523003717078, -3.7936603754202336 56.10275461006195, -3.788728779473467 56.09776147138048, -3.774948676702764 56.087060218511944, -3.7718670700183257 56.08365470289173, -3.7655510706025956 56.07922512020908, -3.7562228856067588 56.074668336904644, -3.745083194659554 56.07242442960466, -3.746373249802618 56.071108592071866, -3.7401248520696657 56.06953077087725, -3.7391628182996826 56.0685370067491, -3.732350733999107 56.065443668324676, -3.727743538004636 56.06145282169819, -3.7245055297652985 56.055806323040855, -3.719768900761432 56.049728590326765, -3.71637809909028 56.0466666068628, -3.7078355197076958 56.04173767913721, -3.7003953873831037 56.03915996102149, -3.6926172527716865 56.037641374674834, -3.685129571234756 56.03893717974909, -3.677873362594136 56.03733507512874, -3.6402352406023795 56.03130309868942, -3.6403366331051643 56.029189113873784, -3.638117088688918 56.02846921649923, -3.63469756003944 56.02520769554245, -3.6355232541299562 56.021966301227785, -3.631244412754077 56.02163390373045, -3.619616709705912 56.030254665420806, -3.5968199106845713 56.02706359617646, -3.5964295418573893 56.021533272614356, -3.5898302463879252 56.02110099601814, -3.578379831016572 56.01918847348663, -3.55747775017186 56.01455240106447, -3.5422128715077785 56.01214737271417, -3.5296113569160528 56.010808177186874, -3.5234228488338077 56.00953963729731, -3.516269212523251 56.009046420953524, -3.5142379773198713 56.00800659674839, -3.5091543551411655 56.007324842314844, -3.5012013460470435 56.005516944631964, -3.5164277145347365 56.0019168703935, -3.5176477517558915 56.000069717131, -3.513624462848446 55.998551147975384, -3.5115309823357355 55.99397065734483, -3.5175324980761262 55.99234325958947, -3.52333991293966 55.99333462329206, -3.5287785981188247 55.992733594310344, -3.529017253158921 55.98882801220584, -3.5301858759896776 55.986434097804114, -3.535521587550077 55.98750979225962, -3.5364030609975714 55.98572774369233, -3.5470082773975404 55.98656735992681, -3.5762370706406585 55.98966831206996, -3.581565245671175 55.98969079371759, -3.5833825249738585 55.99198769607843, -3.581094198255463 55.99452864683462, -3.590170162192601 55.99658193364155, -3.5988442917063908 55.99703856520001, -3.601298782363473 55.99260716367034, -3.606992935185508 55.992050561348265, -3.6099303285215893 55.99290635374654, -3.6141002275095113 55.991476904669135, -3.6152601819666903 55.98699926721338, -3.618510006873473 55.98516748844913, -3.622516971831641 55.98458289410604, -3.625831929390081 55.98511873435223, -3.627282502003141 55.98294301608632, -3.632403211258338 55.98186339832267, -3.6300442381389315 55.97734052817077, -3.6316392054049564 55.97396219658716, -3.6449345559180832 55.969566508916145, -3.6494023398300524 55.96863780924199, -3.6494023002070346 55.967168551379515, -3.6548655532247 55.96333280783451, -3.6527815919461895 55.959470719740956, -3.6562030250927835 55.95867437734011, -3.664621362423893 55.96017671110921, -3.6674460338419252 55.95765473356041, -3.668696280270267 55.955105511981095, -3.672589788930959 55.95468869828396, -3.6743529148227227 55.95304814917155, -3.671707460897305 55.94908529427249, -3.6677014881753234 55.948326306396865, -3.667583857936394 55.945439760767066, -3.6708711207748967 55.944356350657735, -3.6737784871613237 55.94501068393772, -3.676914911195895 55.94258664734958, -3.6833738525184905 55.94228284950881, -3.686840607623203 55.93959890321308, -3.690551832442102 55.9382710846696, -3.6991357634648105 55.9392384635189, -3.699925900837226 55.9357112627653, -3.7033527233247696 55.934562249031124, -3.7082946442154987 55.93532407387987, -3.7090777837565967 55.93387635805985, -3.715616090495662 55.93402900097065, -3.7160604598848197 55.93553512707496, -3.7247485184178273 55.934988747499304, -3.729814326180584 55.93604012811083, -3.7355010696352178 55.93324802717873, -3.7385001993454012 55.933265919243354, -3.7422146028246486 55.93102885209563, -3.742541095947293 55.926248018532796, -3.7507975423296718 55.92413507567436, -3.754405829398998 55.92100048587884, -3.7543767505005308 55.919860498769566, -3.7623818417132564 55.91679958568543, -3.7602852472086656 55.91552207459915, -3.7637845274961044 55.912838930717434, -3.764275560979906 55.91096901162049, -3.767106115400063 55.91013137276908, -3.768066559417891 55.907890761993805, -3.777447421330998 55.90641035120668, -3.7838561225528253 55.901047183201406, -3.7981951172804735 55.899506699037325, -3.8001839357838882 55.89841546379361, -3.8095009620058153 55.89677090517318, -3.8214991037340296 55.89539692239795, -3.8225645402442203 55.89649552066196, -3.8177902570803717 55.89813426402326, -3.8143125839478813 55.901992239009715, -3.8100034113728025 55.90364537730545, -3.8180350726125813 55.90568918650761, -3.8187585863967075 55.90402500835517, -3.825035763192732 55.90514889375492, -3.8330381770409656 55.907804062599496, -3.8349263646802783 55.912217149183235, -3.843108567549712 55.91288300453288, -3.8597021914162473 55.91838484647328, -3.8658975005793295 55.92082801830289, -3.8767503131108643 55.923321359333436, -3.8823431764739174 55.929543668513844, -3.8905263564406716 55.935985725180956, -3.892087566938776 55.93766377312048, -3.9015710896844116 55.93646182055823, -3.903617809762061 55.93784375081836, -3.898359063029676 55.94074042614909, -3.89851041980458 55.943539222528386, -3.892657588415889 55.94491886309951, -3.8924019625832194 55.94716232439725, -3.8889846381316113 55.94914058922088, -3.880942930359418 55.95223905182501, -3.8760192047938653 55.95197175501115, -3.868994220852378 55.953698795915784, -3.8679595762603203 55.956329627575855, -3.8603478770089894 55.959238720248635, -3.8678214401677717 55.96260521186458, -3.880823059470528 55.962251005078585, -3.886005590161782 55.96165232650633, -3.8985729171108603 55.96114028029348, -3.9042561960688187 55.96206901875583, -3.9061146337815775 55.96030941897185, -3.9118614381480556 55.95956795126385, -3.9140429670641512 55.961020485572604, -3.9193364986396517 55.9621270801362, -3.9221049775674848 55.96442129834421, -3.925960440520194 55.96459476538015, -3.9288721548579977 55.96344561267951, -3.937986503030422 55.96289281815614, -3.939843239670599 55.965064427702245, -3.940459177167644 55.96850380542684, -3.9496860604760013 55.97137769937376, -3.949139869639626 55.97393135355215, -3.946068392885698 55.97598658437229, -3.946149496508777 55.97731440717237, -3.943268159406842 55.98045489004879, -3.945522424661139 55.9824449599845, -3.9492965571670053 55.98386086456781, -3.954788021326563 55.98477327202969, -3.9586651874531236 55.98663125289558, -3.9648182714148543 55.987107484744975, -3.98523915701291 55.98278749606748, -3.9893319719746025 55.98729029668121, -3.9788478603450965 55.990942315763995, -3.9771321867230656 55.992730377675315, -3.9822492832898897 55.99527855918419, -3.979641213826694 55.99959188312321, -3.9852662369590903 56.003766539447696, -3.9884258382038227 56.00779467633511, -3.991320257674261 56.00596855967362, -3.9963166649496102 56.005752897480654, -4.003983192932884 56.007171536112295, -4.0119446089816435 56.00690709622256, -4.01258934087336 56.005951167523754, -4.019390091447604 56.00594955734258, -4.021875189536964 56.010073355038344, -4.030349926227968 56.00973902657183, -4.02429179026631 56.017508811396205, -4.0236560913125405 56.020997112232244, -4.020953091933301 56.02254674243272, -4.019184604407999 56.02675371115471, -4.0201297707821 56.02804308992718))
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>