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/S22000047 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S22000047 geosparql: asWKT POLYGON ((-1.9863557322035055 57.31092316588521, -1.9874051404500386 57.31016537656256, -1.9904556724161453 57.30610301855594, -1.9986016920964487 57.29525038720096, -2.001648309410692 57.29118974829509, -2.0559888958521015 57.21859822447054, -2.060773140432404 57.21219227693418, -2.060984020998738 57.21190985840513, -2.073997526036993 57.181989982534375, -2.0749363661197058 57.1798295906112, -2.076382263383474 57.16726297748772, -2.0779906622633226 57.15327564173987, -2.0769930034243025 57.14555465145635, -2.07641942732583 57.14111415474774, -2.0671959227814796 57.14245464385325, -2.0617268491253484 57.14324909755005, -2.0486876598410166 57.13998772996827, -2.0464653656998113 57.13943170346577, -2.059021160886124 57.13433839484002, -2.056996692536907 57.133320534204906, -2.048267681459187 57.12893078833048, -2.0852059857412626 57.08583818759205, -2.092097735243635 57.06799645082495, -2.1613044694789174 57.01804615657513, -2.177353110882268 56.979527057988555, -2.208965532686198 56.967816005279396, -2.208887101658274 56.96774019930642, -2.1983755342045352 56.95757724565332, -2.191267436046419 56.950701293428985, -2.2004700048443286 56.948619510510525, -2.20042516910533 56.948172994053586, -2.1964774722173757 56.90881683294749, -2.216363233773258 56.89474005061648, -2.21349214451299 56.88376106548573, -2.2329157886416957 56.86306190345921, -2.254638239319418 56.84932745595223, -2.2622498989433555 56.84451155777806, -2.2745754614723386 56.845540734117364, -2.277261978392208 56.829619003527, -2.2898785629310945 56.8274093215673, -2.3160930338736065 56.80161536828857, -2.3161944280023223 56.801515520658725, -2.32906663663509 56.797133745154945, -2.3582450345413397 56.78719253178564, -2.363324230073934 56.783287458793, -2.375933233851967 56.77358880166569, -2.4011815107456833 56.77379500264829, -2.413007624709463 56.76407118092688, -2.42456006922487 56.75474066966008, -2.444944679484666 56.751697950621306, -2.4597824369141765 56.747130824282976, -2.4702321172906405 56.75268956349118, -2.5303029091492912 56.78525186549126, -2.591021542011138 56.78833510522273, -2.64085637659458 56.7826360819654, -2.6662954920456943 56.79039040021921, -2.6771415048490264 56.78364806413219, -2.6697428132117387 56.771640867551795, -2.692541737650208 56.77206424117665, -2.7037835109843833 56.78526127036996, -2.695464632392854 56.789449468283195, -2.7293144431058085 56.80288433595801, -2.71404800520115 56.806925551328085, -2.720807685827491 56.81525947850366, -2.649997942295455 56.814891330619034, -2.67177167599245 56.847384785593526, -2.6612710918772704 56.88624089513296, -2.6809633324864888 56.88798121643818, -2.6812881342197525 56.91034396881555, -2.7246870956636657 56.92583000613985, -2.740019909602626 56.948486483656744, -2.7650491385604643 56.95977643395965, -2.8245085048643825 56.961936400826296, -2.833166106647072 56.97700745627108, -2.8510873801250565 56.97256566726263, -2.892428573617468 56.98680409335854, -2.9129591599928566 56.98163589274355, -2.9102447240837828 56.97578754268778, -2.944675195219515 56.97377145549236, -2.954079211358462 56.9668005699616, -3.0263400003856664 56.97277321409051, -3.08316969544428 56.9584319136029, -3.11077344788557 56.918732158034445, -3.1067539800520434 56.89503652205613, -3.1203378660152157 56.888446935368876, -3.1604468033227993 56.90682167059881, -3.1739158792163273 56.900398017337615, -3.2325507104365863 56.91771649391565, -3.269936425438553 56.919875882328874, -3.2842471726092475 56.9285567063293, -3.2848544083953226 56.90726680243704, -3.3370615886244392 56.8958392132387, -3.3721380146020383 56.87461515292647, -3.404961810770862 56.884020776183064, -3.4212868972469144 56.87957293234095, -3.429832073152688 56.886546176123154, -3.4657067386045215 56.87281074041185, -3.502540528743023 56.88711221163903, -3.5292499283670558 56.88360747445345, -3.5376553015418244 56.89225532309304, -3.5607818547229835 56.88410329094678, -3.568460467568072 56.89462163029072, -3.558717372044265 56.900345535851685, -3.5832172096324517 56.93103466069819, -3.6458207580318547 56.923042601642145, -3.6547348904340136 56.93289802544039, -3.6754707476724926 56.932442085153795, -3.683551922261844 56.91611581139337, -3.71026916469047 56.91357862729523, -3.7406863950511156 56.933608362760936, -3.7834979669573867 56.92308077229589, -3.801646251740968 56.93588025559666, -3.787842604702174 56.96323123658757, -3.7603908249712763 56.97438472215464, -3.7482296648997275 56.993109148750705, -3.744396101540534 57.016711900579146, -3.7573908360915307 57.03779704710534, -3.732439533910643 57.05674318146012, -3.7471005915428823 57.05766707534327, -3.7580642149717822 57.06930608218585, -3.7497329553291507 57.076282560354244, -3.714890745625836 57.07898809516519, -3.6836938661033125 57.095238566543706, -3.6691067635139993 57.07036594471999, -3.656942700868776 57.06853739534722, -3.6464806878821387 57.088342503224446, -3.593256264746208 57.08403991039169, -3.5577275355194775 57.09638191741895, -3.5526299263049834 57.08261843794424, -3.538433264704193 57.090821091655364, -3.5168134251978604 57.08637248182254, -3.486281718159361 57.0945188032698, -3.4491214715604936 57.09221379831481, -3.4230292190241753 57.1053556340597, -3.410211979663826 57.098254713025455, -3.3786472793990536 57.09769391624452, -3.3559688780641213 57.110146147947404, -3.336341879950415 57.11098125582794, -3.3223073464689326 57.1311464864905, -3.349375352483728 57.14812096689322, -3.3417571241230646 57.156373630612876, -3.3567078653331044 57.16667922760422, -3.3535225974465024 57.17815919917429, -3.313851676323276 57.19228134174187, -3.2796227895809267 57.19015992275643, -3.2657725317725372 57.203214667419815, -3.24257063492709 57.20214381703641, -3.2451876501373107 57.21955187479952, -3.2085235193288315 57.25612247067853, -3.1649132242858693 57.262238738093124, -3.1432327341498403 57.27791632140933, -3.1102440647630365 57.277129404939096, -3.0923936389997357 57.284265858143435, -3.0540522698834196 57.275115583809864, -3.0437149379829047 57.278745748293865, -3.0190682977726846 57.26165942277303, -2.9842346788939826 57.277738654611326, -2.9766841458533233 57.29887675946381, -2.9518685036135626 57.31824001260985, -2.9721775043211798 57.32808020822771, -2.9698764982613164 57.35136889828697, -2.983531844330722 57.36328241630745, -2.954556606297886 57.3777168198553, -2.9680334608592136 57.38358174822371, -2.926943196022568 57.40316120709306, -2.9305906264835198 57.41180292005978, -2.8599691587733815 57.41640123005986, -2.82872184325542 57.43487296815059, -2.813892593425264 57.415889991540915, -2.7933779095301627 57.41490702815476, -2.802033324259562 57.40495304585513, -2.790232073766043 57.405656466305715, -2.7838372648867082 57.39456661424263, -2.77308258888518 57.40057979585929, -2.732894488477951 57.395547080576605, -2.726820804998966 57.38879120632138, -2.742173881727927 57.38049127413482, -2.7177314332809077 57.37654574301578, -2.689782119646306 57.38391522100541, -2.6772873469978524 57.39996357131251, -2.633513585443923 57.40035308510714, -2.62478993435999 57.40695412300548, -2.654657046224136 57.40838338427312, -2.678587996723736 57.44059487361643, -2.7072334510199183 57.44279087427928, -2.6975502065787453 57.45833128773659, -2.672416056448516 57.45285358500098, -2.6446423534811316 57.477809750367975, -2.6136032943771803 57.48339860987625, -2.554871863014558 57.473471130087596, -2.5085745311075605 57.450504185851024, -2.5102094258463765 57.44226945943288, -2.498079456401588 57.44824654296764, -2.4876452893176078 57.44420922635913, -2.460295170851722 57.45925023614388, -2.442469267474185 57.4480201909193, -2.4389147335687027 57.45395490876168, -2.40201682113177 57.455209761968014, -2.4137875125138737 57.4763257404873, -2.396935856626557 57.48152696420021, -2.4049620689006184 57.48946902202106, -2.3616794014790963 57.48476797183668, -2.3445553574085936 57.496547620007284, -2.284164518510769 57.5006052425537, -2.250769913570055 57.498382273316246, -2.24138546169298 57.49065274956093, -2.219344707275405 57.49582131123928, -2.2091992792814557 57.48390970700036, -2.20524440875393 57.501288560342736, -2.1732014170835097 57.50715748037472, -2.1578263033836067 57.502246324121955, -2.164599684742176 57.48789396999002, -2.148660798685095 57.48069932988479, -2.1379554018270173 57.48098103745984, -2.1314477960375897 57.49391024527364, -2.1201956469228103 57.48637989865633, -2.1123077150408998 57.49227078611473, -2.067280998198545 57.48208090402071, -2.0594217574606244 57.48972005221265, -2.030889365350445 57.47975070361862, -2.031696913824299 57.46873785839657, -2.0461274689938067 57.463469571011736, -2.040218147174384 57.4556747119471, -2.049051202614127 57.456030868301646, -2.022454310872514 57.440376228630825, -2.021569204293286 57.42644088864733, -1.9913581740365818 57.41540631064049, -1.9767805280301431 57.392073037784066, -1.996583652437347 57.35349367706923, -1.9863557322035055 57.31092316588521))
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>