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/S02000480 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000480 geosparql: asWKT MULTIPOLYGON (((-3.823183745960344 56.09109915578192, -3.821807762564106 56.09361764787737, -3.822383709135901 56.09457966339594, -3.821528569408061 56.095194367762936, -3.821200868649357 56.09778724277444, -3.820294095194922 56.0986722776748, -3.819286225702067 56.099459957517944, -3.810139658602859 56.10223636053847, -3.804989110659843 56.10670000244145, -3.803063260822072 56.10680980988294, -3.799456553124196 56.10450429649043, -3.797164856954844 56.10225519169266, -3.797041071135744 56.100289009174254, -3.798530028212427 56.099809035568775, -3.796459471611604 56.09988410808018, -3.788898380703321 56.0925084172123, -3.779307309276705 56.08665104494656, -3.778899733010166 56.08614969474916, -3.779768912971867 56.08582338113489, -3.778419685584911 56.08574397428497, -3.770647843225252 56.078375720230014, -3.768929195170849 56.078040915019805, -3.762236599222521 56.07399404237001, -3.746694468307191 56.06806422126332, -3.74106895021671 56.06806851853932, -3.736935995504965 56.0655455766131, -3.735096802003676 56.06548977699389, -3.73355724221586 56.06457651392398, -3.735465732961373 56.0639522038398, -3.733752846462306 56.06294252653728, -3.734580314172324 56.06254618757467, -3.733691239677253 56.0596873523711, -3.728156009155839 56.04737385053782, -3.72720832163082 56.04089912638608, -3.728010767755024 56.03874020099061, -3.726848086779173 56.03714795923958, -3.728738411160375 56.034551436162744, -3.728624804540584 56.03236939709145, -3.716668271305313 56.02662346155206, -3.730129747354684 56.02439552986737, -3.733400546801102 56.023262270187196, -3.736054449846944 56.02161640579414, -3.740714830844317 56.02233615981502, -3.746303169683974 56.02223552290119, -3.746493358586468 56.02253835873573, -3.753463773601811 56.020561315375545, -3.769060914886242 56.023402851331824, -3.770617595580735 56.0241083994943, -3.770702698632347 56.026317791411316, -3.77406677868394 56.02664731032452, -3.775522568672719 56.02818952623099, -3.773301606872751 56.02840120538232, -3.773636758581131 56.029654352880286, -3.77280534070648 56.02982816850878, -3.7747604988801 56.03033495514914, -3.775189829052754 56.0309386063811, -3.777073197165575 56.030694252520675, -3.778278620800222 56.03369551373089, -3.779286821395128 56.03361689001541, -3.779853267422792 56.03298638166322, -3.782149962659872 56.0336574848989, -3.785288413869835 56.03068967842805, -3.787105088965535 56.03157180156791, -3.792321862811612 56.03020207226683, -3.792651195117922 56.03074600688843, -3.793170186666625 56.032229629436195, -3.793976714551951 56.03231674745663, -3.79540247483463 56.0314321750546, -3.796485218234071 56.03412697302031, -3.793970876498043 56.03411172807206, -3.79535035664006 56.034938722410075, -3.797915124987439 56.03818252037048, -3.80374526036802 56.03700888711264, -3.809534427098358 56.03620518309301, -3.809808191024991 56.03663830693676, -3.813359372193247 56.035954936305046, -3.815213460527269 56.03725581295676, -3.816704469454479 56.03712422698979, -3.821643646479491 56.03974726768755, -3.822759574425917 56.040943935413964, -3.825758860841172 56.041335189961394, -3.83370952126562 56.03749721046452, -3.83553235844533 56.04217436352423, -3.842883063617465 56.05054787690843, -3.834663108231684 56.052269966999006, -3.833619043058945 56.05091058211314, -3.832472527666035 56.04975939627866, -3.831653597523799 56.05043655062121, -3.829722026613508 56.05135485540825, -3.828097812694204 56.05096556361191, -3.824510200636889 56.051162457528, -3.819730718529599 56.054692783624546, -3.817980199696378 56.05435015475724, -3.816672295297079 56.05317424219865, -3.815415949643816 56.053435364842066, -3.81468370129822 56.05288898708308, -3.812578897989024 56.05354897619507, -3.810499122210546 56.055772167652336, -3.815333856102095 56.05818133493247, -3.815445272565999 56.0612350402087, -3.814609768141638 56.06157083472879, -3.812585283508432 56.061214184898674, -3.811796566025339 56.06220527104481, -3.80911264623767 56.06217275918752, -3.805536796582004 56.06298897337648, -3.803980741646349 56.06268823059613, -3.803175001896499 56.06332006241042, -3.799686066002015 56.062903719132585, -3.798749006141613 56.064166470713864, -3.797370523160611 56.06526490402339, -3.798130352746628 56.06640407288515, -3.800320410939871 56.067207865612275, -3.799831946008156 56.06844610648344, -3.804077800892624 56.06957926708578, -3.806412826362056 56.069697876003175, -3.8051845773888 56.07297785835308, -3.807639420568778 56.07290598059715, -3.814213230685174 56.07501114547403, -3.818108225847397 56.075493000122684, -3.827402230637661 56.08038804212064, -3.836444692453592 56.084675068547384, -3.833792309994348 56.08770694607321, -3.828493213801701 56.086455587331415, -3.825555902736205 56.090614782135454, -3.823496454454257 56.09057332972523, -3.823183745960344 56.09109915578192)), ((-3.769973128519115 56.013181372565576, -3.769824705046643 56.01414503156833, -3.769008719736037 56.01454315793501, -3.769765494126742 56.01460418355636, -3.769630242140306 56.01515428407793, -3.769571027615756 56.0156134359257, -3.772495059414181 56.01570623597708, -3.775913542202736 56.01818218779024, -3.774225542089305 56.01845809601037, -3.775474240079292 56.022439283483564, -3.773939022713976 56.02375513253112, -3.774538691741017 56.025633617788664, -3.773693347756201 56.02644555592368, -3.771370074956401 56.026173417743564, -3.771237643283486 56.02398267646307, -3.770287436126226 56.02320552643681, -3.754032616780735 56.020014057862184, -3.74393571296383 56.02168491901433, -3.740400345454022 56.02156413537152, -3.737198973717525 56.02089303156063, -3.735660256200002 56.02105582830811, -3.734610418346166 56.021259303517894, -3.733169937122393 56.02277127047188, -3.731219311918673 56.02364337272052, -3.728618713990347 56.0239763939721, -3.72803601568719 56.02355276416872, -3.719648103909021 56.02543798357448, -3.714827467987295 56.025975158642886, -3.709556641401177 56.0257462616526, -3.706163032942027 56.02610210924336, -3.703201374223549 56.02680993862615, -3.700113664603317 56.02830183549188, -3.69825251241055 56.02997277513452, -3.69727631685907 56.03343722958592, -3.696128762038522 56.03488803266128, -3.690220174338757 56.037044575338406, -3.686995129095607 56.0368367652389, -3.683786493463836 56.035879965421685, -3.682802105089094 56.03668127157676, -3.684187914004158 56.0371494883335, -3.682324439704256 56.036738186099264, -3.682994405272334 56.03558401746599, -3.680283971818513 56.03581870908197, -3.682715968830478 56.03537835725834, -3.681591179235531 56.034968798164925, -3.680113193943432 56.03310197533153, -3.680037499505395 56.03111719599803, -3.680783480094317 56.03069689177125, -3.678839111441661 56.02800522446981, -3.70546078913742 56.01989563603459, -3.71747128862688 56.01981085337315, -3.71729933993257 56.0202075923703, -3.721020848435909 56.02091055257132, -3.721568974035447 56.01988746075528, -3.7235483513598 56.02074045413054, -3.725831713120214 56.020492854288705, -3.727991002627323 56.01567297729805, -3.72876779174594 56.015823839211855, -3.732741077035469 56.013198013822105, -3.732339591464227 56.01390458047493, -3.734550742803963 56.015634786494864, -3.744075680466876 56.01196823660935, -3.746171542054375 56.017187136103814, -3.747906968975611 56.01689296023451, -3.748362111436352 56.01455481197439, -3.74708896927731 56.013058309526556, -3.750251059377627 56.01145748645327, -3.749162112798119 56.010234336461274, -3.748337446337946 56.010171921210606, -3.753187782231419 56.0073914272096, -3.757421147922634 56.00595625905032, -3.761745557869085 56.01427871523481, -3.769973128519115 56.013181372565576)))
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>