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/S02000820 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000820 geosparql: asWKT POLYGON ((-2.999129937973793 57.669025047507475, -2.995447940828418 57.672305856381314, -2.994280267534316 57.67252164523623, -2.993037551850064 57.6713215103219, -2.985557472632142 57.6720140989488, -2.985932180772294 57.67164430982985, -2.983296892130979 57.66997553274463, -2.986537807043755 57.66770458844394, -2.984231177417678 57.66609666274252, -2.981713409603747 57.66721213675977, -2.982055640659892 57.667469979558085, -2.983249710138559 57.66699359933177, -2.984105203963295 57.66763370583393, -2.981065898233282 57.66846686621223, -2.981038107185874 57.66889716226993, -2.978460366951622 57.66707078640155, -2.97652336538659 57.66807873171819, -2.974046181337613 57.66634617856478, -2.976527381986984 57.66573418267178, -2.972318991482653 57.66381733093678, -2.970842260524716 57.662499209543974, -2.96816727727255 57.6646396595834, -2.963214992024918 57.665620662923466, -2.962398995615677 57.66455791901404, -2.961343242224316 57.665382903043344, -2.958834838553684 57.66224946005015, -2.952152028466007 57.66371024029574, -2.953862453226764 57.66545510008815, -2.954947431777895 57.66535666798562, -2.956904543474901 57.66802729160184, -2.956212026778948 57.66843493876438, -2.95787581003251 57.66945568888607, -2.957444066743398 57.66981495275201, -2.956357704179135 57.670005124124934, -2.957074845984475 57.67067417172697, -2.954210024530378 57.67145500128575, -2.954983280503756 57.67258186370558, -2.954215990544077 57.67273137818399, -2.954464000976586 57.67228724954805, -2.953383577282903 57.671677688879434, -2.951952187282062 57.672074726289594, -2.952125913707616 57.67265385415241, -2.951075266246425 57.673159260690916, -2.949776891970803 57.670312488933234, -2.95117949025516 57.66945756650946, -2.938860388751544 57.66760931269703, -2.937313889653379 57.66679437324127, -2.930826830942115 57.66810884351259, -2.931243158049554 57.66930946656694, -2.928903079285972 57.66957819100072, -2.930104942912612 57.672650448523314, -2.921776286074058 57.6741216980402, -2.9154328063096 57.67255985428208, -2.91223353454529 57.67401121605061, -2.914816162635454 57.67470219979872, -2.911139599231598 57.67585158065515, -2.912154933091884 57.67822468820825, -2.907299031782187 57.679840564765335, -2.903898255260126 57.679298990475814, -2.901943877979272 57.67825297957032, -2.900419299247914 57.67898245680617, -2.900387484386381 57.67972825256902, -2.901686493349502 57.68004237669486, -2.901630139039518 57.68047394886392, -2.903253318516287 57.68033661223364, -2.904891498810074 57.68080099258217, -2.904739540909378 57.68277827973375, -2.907236458267398 57.682051018017006, -2.90778403312444 57.68243147921966, -2.908925052140497 57.68365881368863, -2.907811294862582 57.68402222256268, -2.911392916962634 57.68661106620234, -2.907142411668254 57.68766563863, -2.904765148210842 57.68649693344444, -2.904199481452521 57.6866806383474, -2.903077311113608 57.68539514148805, -2.897182551023092 57.68723960055481, -2.905800473971158 57.69236422146105, -2.90644536924034 57.691991312732675, -2.909414362600909 57.69466482684537, -2.911960038743682 57.693829082836935, -2.913968341131901 57.6949733590995, -2.90786015878236 57.696941542694994, -2.906199760262153 57.696933565813964, -2.908438050686884 57.698552367238165, -2.906276981360228 57.69826638698228, -2.904265399113835 57.69896770319029, -2.90655956606131 57.69794768430263, -2.904050953037359 57.697695594719036, -2.903414381492701 57.69820574226355, -2.904471159057693 57.69849045175574, -2.903465188444398 57.69828255352142, -2.90286493527436 57.69877058401184, -2.903874963345184 57.6988506289078, -2.903236943461247 57.699020550861285, -2.903303956690399 57.69988620110201, -2.90433777314177 57.700154233161115, -2.902520031334362 57.700286694453204, -2.903433899233047 57.70045689184142, -2.902830071552605 57.70071578090905, -2.901424171816164 57.700636937007076, -2.901234211656168 57.701044054122825, -2.902617157796959 57.70096144011229, -2.901160127736177 57.70156352960442, -2.900210987741493 57.70112337633027, -2.89926703600639 57.70158804127363, -2.89950095460541 57.70129393864679, -2.898926787981374 57.7013474308019, -2.898702954308249 57.70182482881476, -2.898566657621338 57.701485837763094, -2.897448219603085 57.70183625252176, -2.897137670439148 57.70138785839592, -2.898379418279571 57.70095185447378, -2.898244532312897 57.700364315534685, -2.895646218027782 57.699680948192935, -2.888531339078524 57.70135970531415, -2.885981712864371 57.701242759643904, -2.884570354078051 57.701728421249946, -2.884797342877507 57.70209556523375, -2.884186935184613 57.701842868165556, -2.884605920147584 57.70216243096438, -2.883809102539846 57.70199345170874, -2.883562143144795 57.7026061610343, -2.881274154226879 57.702515725477454, -2.879444888248083 57.703620382808126, -2.879726164381996 57.70429832505635, -2.877464475685867 57.7053899433301, -2.876687361575102 57.70512904897296, -2.877512517201656 57.70459081642979, -2.874616844868597 57.70509444723798, -2.875613893793787 57.704586065392, -2.874977530484205 57.70466895475567, -2.875026956334075 57.70421488192334, -2.873811501306973 57.70464075058265, -2.874414746127675 57.70405142906022, -2.873164898871173 57.70332419811985, -2.87238215038822 57.703777577813604, -2.871221300692376 57.70346939309528, -2.869908798621139 57.70426286506446, -2.870336687263522 57.703675126672074, -2.869417788031393 57.703181389290215, -2.867111113261093 57.703370756107795, -2.866927378831365 57.70385015765497, -2.865170638536945 57.70346998384863, -2.863491198634451 57.704030163879764, -2.863569905872133 57.70471475673585, -2.863316839365289 57.70404141563339, -2.862457474889083 57.70406342687524, -2.862427501666447 57.70487686415363, -2.862258934716997 57.704043457469716, -2.861498865500982 57.7040501601755, -2.861110765702414 57.704924673683834, -2.8641920097174 57.70521357131198, -2.862569134932988 57.7052544528385, -2.860982218903988 57.70605749139426, -2.860618104601328 57.70545745601658, -2.859516732515275 57.70640338862822, -2.858051883490432 57.70631252407831, -2.859134998440967 57.70550912558047, -2.85843614864863 57.70576381092676, -2.858509689475499 57.70535778097586, -2.857426210236869 57.705077744553904, -2.856325356989993 57.705430056782525, -2.85631624577484 57.70619263297134, -2.855179791981632 57.70564038365096, -2.85145504209126 57.706575437027354, -2.852436093174071 57.70566199122343, -2.847992076712155 57.70615683970935, -2.848908018521304 57.7054756338092, -2.847012138567614 57.7052580940082, -2.847633189415745 57.704801458059066, -2.846547269746916 57.70505221146465, -2.846998454915227 57.70478768802477, -2.846135369113263 57.70388308647075, -2.847684355249429 57.70331462912953, -2.847453821736224 57.70204016830251, -2.846638272877747 57.701839752495644, -2.847341815537892 57.70190118795439, -2.847562820010567 57.70130325942497, -2.846352224534995 57.698345835836314, -2.834563524016624 57.69402181268036, -2.828663548211678 57.69276708581776, -2.824228955261013 57.6924996259613, -2.82249647736893 57.69303189394088, -2.822503924213579 57.69410076850763, -2.81706145268576 57.69452228531629, -2.813867825455538 57.69573754367754, -2.809226141058352 57.69597391045182, -2.80808917162839 57.69689740652901, -2.806917767529081 57.69703063910487, -2.804160132901551 57.69523370298727, -2.801440065647686 57.69512520008668, -2.802046382212082 57.69295655733102, -2.801969158589693 57.67590392016859, -2.801964358951298 57.67474442364059, -2.802858563216847 57.67189567639026, -2.801310850063486 57.67105810408946, -2.80262950442158 57.669606764745524, -2.800641502471233 57.66931493893881, -2.799709102358975 57.66917201902417, -2.799621503748149 57.66817089585941, -2.80254226285795 57.665878413893374, -2.802152746726692 57.664996107072355, -2.800109298866677 57.66462771950396, -2.80004177625916 57.66365894746106, -2.797979628457821 57.662108993758665, -2.790932996462351 57.66177599092304, -2.788884569604806 57.65851910163138, -2.785349956840689 57.655487005526794, -2.782977720816842 57.654271093255026, -2.778978591975009 57.653756829929605, -2.772303876670136 57.650339450282516, -2.772356188664778 57.6496474705311, -2.775706460485607 57.64804601862769, -2.775165023977448 57.64622586431048, -2.778304430391272 57.64418548027817, -2.780268344714609 57.642026525987895, -2.780860282220149 57.64072936927519, -2.782176084190521 57.63570891026867, -2.786018485091933 57.627510845214125, -2.788660435169083 57.624251640592874, -2.793324727179197 57.62162645058794, -2.799350559752931 57.616998327839276, -2.802827236681096 57.61297000039252, -2.810427894726343 57.61003803870476, -2.817931552934835 57.60656726587186, -2.823518312734707 57.60496798108812, -2.824618782221956 57.60405355489655, -2.826436773339281 57.60452674261965, -2.828986030732663 57.603306358700635, -2.829392528856674 57.6042558567948, -2.83155701858736 57.604511115643255, -2.83489611295659 57.60417469748273, -2.846457564117614 57.605544009431405, -2.853569343861247 57.60481347681329, -2.860214120452381 57.60557687104437, -2.860631810984209 57.60625672104769, -2.862749640637073 57.606646502024866, -2.872667159644947 57.60638077455172, -2.875667385376467 57.605183279647335, -2.88159325157419 57.60871723179622, -2.88324853482778 57.60865177112546, -2.883746436680345 57.60916929618456, -2.899585981345583 57.607422664891764, -2.911313156129876 57.61059041602189, -2.922075277292387 57.60729649688788, -2.93525204278619 57.604271245236305, -2.937086194956791 57.60921620020819, -2.940434063844203 57.61247905767452, -2.939650738807484 57.61325740613972, -2.940286372938631 57.61453723208705, -2.941889976440816 57.61505527786827, -2.942313483360999 57.61589651013495, -2.938512861838227 57.61716441726506, -2.937219824956796 57.621557644047094, -2.942558098020305 57.62017051675856, -2.945337806030077 57.622772755588954, -2.943003396476557 57.623787284818874, -2.94165325609515 57.62790250646144, -2.943985122914555 57.63000506695562, -2.950751522577662 57.63388883993038, -2.955996767297901 57.63337319281022, -2.958897256651398 57.630952807550095, -2.961233592711525 57.630009829767836, -2.961330346620389 57.63050315194323, -2.963918744451118 57.630860725838055, -2.96458267631761 57.63126887405893, -2.964321779499594 57.6327979444733, -2.966851258599272 57.63345234634987, -2.968157510616504 57.635337726230425, -2.967915401637649 57.63693852709229, -2.969843324530998 57.637004597667364, -2.973153035178931 57.64113823345774, -2.974924303073847 57.64160068148959, -2.975507591113509 57.64273700067588, -2.978273897036595 57.64344324428977, -2.975626264382014 57.64915952898917, -2.978671978596821 57.65014136453436, -2.977781312836293 57.65128908096505, -2.980764412506536 57.65193070619877, -2.982850251256466 57.65408836974128, -2.98668776134946 57.65594488546016, -2.988875805863662 57.65875739156446, -2.991536588697656 57.65984144857292, -2.992635176749324 57.66140482535777, -2.993111871293129 57.657996585034574, -2.999767873523792 57.66053120889742, -3.000348915271545 57.66032899546059, -3.001029840918802 57.66083837169258, -3.002960545108616 57.660497286187855, -3.007912544940242 57.66473342550067, -2.998538095070976 57.66823024758111, -2.998252212990626 57.66880740593659, -2.999129937973793 57.669025047507475))
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>