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/S01003723 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01003723 geosparql: asWKT POLYGON ((-4.920868472846117 56.777866303143824, -4.910651623003804 56.77891425142605, -4.903470329395089 56.78111306782704, -4.89437920600527 56.78276260483238, -4.886664468713541 56.782303349689094, -4.879590751946892 56.7827276222567, -4.871138226782082 56.780325057109344, -4.866720272134864 56.78143340448147, -4.853719176042813 56.77970044711199, -4.843238307041974 56.777288512726145, -4.836856510558454 56.773209590340414, -4.831460408817646 56.771984106120414, -4.828893831824902 56.77376807080797, -4.821187672399312 56.773385494840404, -4.806290623763814 56.77425208927589, -4.803582496600147 56.77327943343041, -4.800654157033887 56.77110729380944, -4.795302232586714 56.77089489719571, -4.784102954369824 56.7719544760472, -4.779790619299671 56.77141259775653, -4.776647109861195 56.76830100120687, -4.769476218434195 56.75827742985514, -4.757886680304619 56.75352830828288, -4.75113940817504 56.75567273536745, -4.747498836829522 56.7539914743763, -4.739699377852744 56.752931897693536, -4.736241891777815 56.750608161957786, -4.741592361343185 56.74792905842931, -4.746603731169563 56.74240808555411, -4.752746704222979 56.73851562695437, -4.749162292915705 56.73802855438519, -4.742140832606554 56.73862371243177, -4.736046030901739 56.737571347031476, -4.728422304691949 56.73798122232218, -4.721404866650709 56.74044454751061, -4.718904479521679 56.74069686818772, -4.709899230043972 56.74010216992936, -4.688594749593396 56.736744065147626, -4.677947703674264 56.73695542358541, -4.669864311612502 56.738036665267295, -4.663479629462397 56.73793965705555, -4.656015175031637 56.73644533334539, -4.657830047229297 56.7334317789428, -4.661491083645674 56.731789792014155, -4.663317884393494 56.72641218560949, -4.660223769351448 56.72357535487394, -4.6616002194815 56.72061602832098, -4.658371959876987 56.715175651282344, -4.659604019788661 56.71327056081774, -4.661744830816694 56.71316418159377, -4.663452343181063 56.71375492576246, -4.667402157078229 56.71391304706928, -4.672594251048197 56.71361299133848, -4.677002419183807 56.71447098495906, -4.679410214245404 56.714086662511484, -4.683463415409765 56.715005993823794, -4.687551281016932 56.71411794678024, -4.713213762767446 56.71258181297855, -4.723032895539546 56.711639477546505, -4.731146442865721 56.70949348462133, -4.737003647845231 56.709140184294526, -4.741105171101411 56.7071175996586, -4.747961533242251 56.706355306031405, -4.753990526455076 56.70478405713739, -4.762227889359483 56.704592591537995, -4.779718439511019 56.70557847391077, -4.799260884637099 56.709095320674685, -4.813692842824191 56.70737689477645, -4.827485905418936 56.70761270008412, -4.836107727739125 56.70666161176175, -4.842413007693868 56.707281809754775, -4.848074409099271 56.70586714274354, -4.860548917337933 56.705575792451384, -4.861868344927546 56.70289035759399, -4.870069741528426 56.70244989301392, -4.872319048893186 56.70344962792858, -4.875887808064606 56.70353801874263, -4.879402507636299 56.70505667144428, -4.884318923077084 56.70567091045683, -4.891075107563484 56.70534343275379, -4.901343875521694 56.70203956511974, -4.904961156035769 56.703393329722054, -4.919726350558614 56.70331757415111, -4.92967269652756 56.70409958703744, -4.933843396208634 56.70561926836973, -4.937145578686562 56.70857047674334, -4.942323683369085 56.710632385235726, -4.94620402336128 56.71051365737494, -4.950309991884518 56.71223208544335, -4.956540428582959 56.7131089712658, -4.956261017547743 56.71558741282089, -4.954970153278762 56.71538435681539, -4.953904516830628 56.71576018872592, -4.954737498441549 56.71658533510264, -4.957984830763174 56.716723956494775, -4.959292671637041 56.71714229926899, -4.961069903127472 56.71646187210308, -4.963330267166285 56.717351881395714, -4.963444258162336 56.717969370814735, -4.96625574155601 56.7171384106475, -4.971144619916736 56.71675486174022, -4.972199449842186 56.71705959745553, -4.973258568325623 56.7166028665844, -4.973819026067769 56.71562772028997, -4.975413597725046 56.715742429103294, -4.977693360989858 56.71501379327822, -4.98108467924624 56.71573262199673, -4.992306409244061 56.71440280154907, -4.99916130343648 56.71291815633339, -5.01376870391749 56.71401099363012, -5.020601500101854 56.71138771223657, -5.022674457138294 56.71136953922936, -5.033344252824882 56.709679484988754, -5.044768337514319 56.706380938054835, -5.056749175769971 56.705701757018105, -5.063087940376211 56.70336098187419, -5.06822751955867 56.70262390237587, -5.074430584036651 56.70346694659338, -5.084140407935316 56.70057586385389, -5.090689346859894 56.70047572642672, -5.098396228940081 56.69970771429633, -5.105256216971061 56.69819854664932, -5.108175102539523 56.69575262013164, -5.111994302136981 56.695127005300776, -5.121461118085564 56.69482568296514, -5.130141086890981 56.693098720076726, -5.137445603130123 56.689984376067386, -5.142388041520669 56.6883315451179, -5.146487663767176 56.68754479030041, -5.152102747757906 56.688670155169, -5.15550542035026 56.68892551068831, -5.157437966220185 56.687815781213736, -5.158972245154877 56.68585317691447, -5.162513899592311 56.685482898051816, -5.166540792707311 56.690666405369655, -5.168650915337315 56.69249248330575, -5.171770273137076 56.69255715061125, -5.173457573625042 56.69130961997439, -5.178046874333955 56.68945731432025, -5.183265396282978 56.690046939467926, -5.186025515335773 56.69195251030932, -5.186278939570782 56.694316981129205, -5.185223858912384 56.69594406561516, -5.183933260047527 56.696525424997816, -5.183884299669134 56.69845934959634, -5.184902440275634 56.699741480310124, -5.189159269381347 56.70033940833796, -5.196236532531191 56.70009360736782, -5.206465802807998 56.70204773147989, -5.21602214492412 56.70175298705878, -5.218759493316344 56.70267837525355, -5.222796919453946 56.70104458228127, -5.227279079032619 56.700910468730605, -5.231894285073287 56.70137502235252, -5.2361857419123 56.7032052171627, -5.2453309278961 56.70389301662032, -5.247024660421458 56.70515230477301, -5.23754602595098 56.71351658544759, -5.239352467830078 56.715252155293165, -5.239889096839526 56.71795030203781, -5.231899396760607 56.72676074109926, -5.228460650257812 56.72975349094761, -5.223495105359455 56.73211147352525, -5.211581472481433 56.73910728571439, -5.207556463598904 56.74312132452187, -5.194540857913638 56.75204947191707, -5.190566802234129 56.75708636257466, -5.183526305354303 56.761680129658096, -5.167575810299677 56.775354517514344, -5.163950228561918 56.7812254631083, -5.160441771964082 56.778520003344866, -5.160296207989005 56.77464943111452, -5.161078687490702 56.77227446448734, -5.162768922101591 56.77076636108428, -5.164149415489781 56.770830195264836, -5.165030627708968 56.7686594359244, -5.167137352198463 56.76782385672458, -5.170079417365813 56.76427926403248, -5.164965127463812 56.76088558084523, -5.160745987919313 56.755086894425894, -5.15813716454024 56.752860860422246, -5.164255406902969 56.7499279822182, -5.175077763697989 56.74323438532339, -5.17810178842722 56.74301343414585, -5.173039138784263 56.736715308264706, -5.175894466490861 56.73488965196276, -5.151366127564656 56.73630378153921, -5.143291088024196 56.735770754055665, -5.138868507963146 56.734318213660316, -5.128682064065108 56.73218347926535, -5.118382853294786 56.73183055129517, -5.115253172832783 56.73136055985173, -5.110573239029791 56.731522483619244, -5.107042782584121 56.73392075626969, -5.105930143145462 56.73312153569339, -5.105678851620527 56.73186037030424, -5.104312484816797 56.73175061560587, -5.104093050588752 56.731072934301345, -5.102892170180556 56.73078824844169, -5.10236581030002 56.729560900437924, -5.09994429769515 56.72855152806979, -5.099303104503791 56.727318039228024, -5.0998288495244 56.725363360372505, -5.098724825377775 56.72228069598919, -5.097085112238066 56.72043382940896, -5.095497280528571 56.719017121589154, -5.092168530674562 56.717499821188156, -5.087961106391536 56.71642670210465, -5.081220460143042 56.71695324894221, -5.079358294127614 56.71637010588937, -5.074561183758612 56.71747741865567, -5.071106036983552 56.719207634465356, -5.067575750546875 56.71961825492008, -5.063406456778042 56.719208543279144, -5.038681145110015 56.729638987299374, -5.03609819820756 56.732281723074806, -5.030166750050487 56.731527320859215, -5.02272005736444 56.72982067369318, -5.020470835535512 56.73663465947179, -5.021336410212651 56.74232141571406, -5.017592281233595 56.74376944996114, -5.005158988205284 56.74252399761664, -4.999023962796047 56.74374148993821, -4.993958505948448 56.74237125232833, -4.98121883454421 56.74487897395721, -4.974643876990268 56.74675294225701, -4.97031647027611 56.74995719747411, -4.967708474985525 56.75111593462514, -4.962053916750792 56.74953372566593, -4.958108236778815 56.7503285702261, -4.948414276854465 56.75397393552011, -4.946735028705015 56.75947856053598, -4.944947994790473 56.76068928951261, -4.943986739582787 56.762833242561385, -4.939946528986069 56.765580176986255, -4.940991871848612 56.76889915548171, -4.938674554456976 56.772755863451415, -4.933223091015161 56.77317191394484, -4.928521809326018 56.77523292284582, -4.923904369584855 56.77795690655452, -4.920868472846117 56.777866303143824))
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>