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/S02002018 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02002018 geosparql: asWKT POLYGON ((-4.084459432799523 57.61877714824962, -4.085695239340828 57.617777042632234, -4.086850897798514 57.61734465871347, -4.089038060596555 57.61606860636727, -4.089987943659825 57.61497022065834, -4.09334145404962 57.613018896364586, -4.094537387454742 57.61171962795833, -4.0958603168946555 57.611115469165945, -4.097318569525535 57.61007148481675, -4.098372517952285 57.60971076186286, -4.100554889297004 57.60804732983304, -4.1016358533175845 57.607721170688905, -4.1021883703881645 57.607230386862625, -4.102414082675234 57.605979523490426, -4.102731243508912 57.60561664515656, -4.103355827398761 57.60417225358771, -4.1034341931263425 57.60289239313577, -4.103789979003497 57.601808276812505, -4.104619048178778 57.600544662865815, -4.105103893238093 57.59877014522344, -4.106511159071871 57.59706021205681, -4.108461228896212 57.596114806785934, -4.1098715717787595 57.59581991241046, -4.112093102745566 57.59482588851987, -4.112616502541789 57.59432925223649, -4.112665150251798 57.59343712997352, -4.11246395796122 57.59244945305558, -4.1130390754178014 57.591143302230115, -4.112944037005967 57.59011072198597, -4.112383002662976 57.58868790178005, -4.111666674251293 57.58776005218859, -4.111130255772042 57.58664499976051, -4.109849889785805 57.58490264554744, -4.107888430362837 57.582980269024766, -4.1043279348788895 57.58018692066318, -4.1020047059063165 57.57853643959636, -4.100099584972766 57.57734618251294, -4.098530910172085 57.57659416366746, -4.095322165051045 57.575328456485536, -4.091502211649147 57.57416174568239, -4.093258027802938 57.573779543276906, -4.093967640521899 57.5732646209384, -4.09631889665635 57.57393182534992, -4.097850088120819 57.5741516853409, -4.103446928796097 57.57610984589082, -4.107093464532879 57.57715959439419, -4.109702908618586 57.577799783291475, -4.111971357278754 57.57818236244484, -4.115165430603019 57.57851795345464, -4.1187558637988 57.57839127527853, -4.122224986694438 57.57856573427873, -4.125252070979387 57.5784734953212, -4.127007468627382 57.57863082909999, -4.130097334032571 57.579183439928315, -4.1331368773941835 57.579389105242626, -4.134084829145229 57.579152955745435, -4.134415610700763 57.57843215628068, -4.1366332305633 57.57837039470087, -4.13755070878709 57.57795772882543, -4.140539615681539 57.577473121746046, -4.14239654384216 57.57688186218606, -4.14416259507295 57.57611151795147, -4.146333425335787 57.57539985964785, -4.147903271499096 57.574367736448295, -4.149304322376747 57.573707752819956, -4.150759971926044 57.57346373108216, -4.152362716327586 57.57285059340432, -4.153622257665898 57.57259999718151, -4.157781422697522 57.57118751375572, -4.159627346022113 57.57039222017821, -4.162946909528735 57.56881601404484, -4.166577289322455 57.56763512099212, -4.167047662365377 57.56733413311532, -4.168476566682694 57.56712450564009, -4.169291004423828 57.56684545318934, -4.170860953707327 57.56583189993739, -4.171968813797964 57.5654830909799, -4.173856158423343 57.56429871129345, -4.1762936890685785 57.560877396644976, -4.176712894602866 57.558713728975235, -4.176570443247004 57.55795154726855, -4.175833468221007 57.555910242393345, -4.175101989456103 57.55452026365589, -4.175042523532776 57.55380337319404, -4.17555622671931 57.553055939280604, -4.176520209297791 57.552740114635654, -4.177306673787525 57.552166774475516, -4.178383401211075 57.55166838815137, -4.179732420487142 57.55007718426656, -4.180529413389426 57.5496258394683, -4.180637479774598 57.549029152870276, -4.18173157978125 57.548767645325604, -4.182296175441487 57.54837152128984, -4.182461891687901 57.54745396286794, -4.183417174809612 57.54702412154184, -4.183984277523863 57.54647519757869, -4.185524241664826 57.546098117447904, -4.1882393856548195 57.545644041487904, -4.190725512018316 57.545746472941005, -4.19225088925275 57.545990441857775, -4.193504142280621 57.54601628861531, -4.194285547922752 57.54644927148375, -4.195462348040986 57.54678731715199, -4.196500227092731 57.54764891332865, -4.197170787904171 57.54796071022382, -4.1985488339548676 57.54817032954185, -4.201638184995614 57.54829528438317, -4.202779209562437 57.54826010077619, -4.203774069999295 57.54862730266734, -4.205472819999103 57.54891570839001, -4.206777066391194 57.548815638308994, -4.20784221872065 57.548990193624114, -4.210317546429632 57.548854321277716, -4.211110190215491 57.54927532484115, -4.213909811650208 57.54963597747239, -4.216199037263038 57.549297491232586, -4.219778589421637 57.550389197792164, -4.222257446173267 57.550529776824625, -4.224439019394084 57.5511670428894, -4.226245103986972 57.551073201759415, -4.227944626992437 57.551314570669966, -4.2306760320881045 57.55104618800645, -4.231893678797016 57.551087559867206, -4.234627323709022 57.55148396576625, -4.235557765665963 57.551829598322826, -4.237259326660084 57.55199353281989, -4.2378262296250835 57.55180737066608, -4.240721763236879 57.55203454663801, -4.2427757231048755 57.55173120238281, -4.244938972149041 57.5511401463063, -4.247904776465167 57.55006662417999, -4.251575548598492 57.54915297530951, -4.250472324158093 57.547633457101, -4.24959828057793 57.547444172703855, -4.246318581499751 57.54764994773755, -4.246155939221588 57.54709844554569, -4.246756465407687 57.54618472751386, -4.249736900327732 57.54459693292185, -4.246984315869699 57.54499999405411, -4.2461327718291235 57.54533772673661, -4.245259756285253 57.545381115620934, -4.243670377481347 57.545759810096264, -4.242647466277361 57.54575732454749, -4.238534799753956 57.546249054457085, -4.236485297089302 57.54629347609068, -4.235290890882304 57.54649254549485, -4.233840153610098 57.546490376545314, -4.232639148289328 57.54677310096464, -4.231099206998269 57.54676079743715, -4.229140563491106 57.546214056283056, -4.229338302524934 57.54586732441078, -4.2284323726804445 57.545235482023806, -4.227585318484593 57.544335730335945, -4.226496518298367 57.54426598884746, -4.224947131686689 57.54357628557713, -4.222730817684353 57.542909992670985, -4.22215735911898 57.54298748111777, -4.219136658726471 57.54244670467826, -4.2181194401183575 57.54204137995285, -4.216235413413954 57.542110420785214, -4.213103525263018 57.54174487002092, -4.2100887619133305 57.54157396858076, -4.2075751890436655 57.54209148609929, -4.206297825144346 57.54216234423486, -4.205049990775535 57.5424177698237, -4.204578699922433 57.54299746599228, -4.204856768419556 57.54361708382485, -4.203222245039101 57.54375165504011, -4.201939249339531 57.54361949996933, -4.200559063533812 57.54262374714813, -4.199386033447752 57.5410160685204, -4.198579161820169 57.540575474433496, -4.195858082642667 57.53995075436686, -4.194420993603928 57.53950402670636, -4.193962493719629 57.53804740495348, -4.19344960445707 57.53754775637327, -4.193604899018999 57.53682354234866, -4.194126113250609 57.536433506742, -4.194783113644144 57.53474543509915, -4.195293993661894 57.534295372714375, -4.197300100585532 57.53306901156963, -4.198091482103775 57.53209021191176, -4.198463856767527 57.531310992421375, -4.1997891951764545 57.529907757472515, -4.200704341043022 57.52923857052274, -4.2013515370087955 57.5283368406154, -4.201387441803248 57.52785190815805, -4.202339284772941 57.52676784694381, -4.203166371763006 57.52627449198408, -4.203486998513451 57.52571989234988, -4.204212704107036 57.52523548884429, -4.205047408023945 57.524316085330845, -4.2074356317647945 57.522683926157534, -4.208222053250643 57.52201783143663, -4.209801691121432 57.52115813395123, -4.210496549226162 57.52058078536575, -4.210758401553561 57.51991308161637, -4.210534056482783 57.51841377153742, -4.210772155432467 57.51829189167715, -4.213857958145096 57.518288992645395, -4.216518746920568 57.51783164094059, -4.218381666517372 57.51707827188042, -4.219085765038904 57.51654653610784, -4.2194036132036965 57.5159515067618, -4.222143485886385 57.514053191024765, -4.222731534959747 57.51323504831399, -4.223861152730504 57.512783841707574, -4.2246133540579915 57.51203018541842, -4.224613752943455 57.51148836184261, -4.225060106329494 57.510894644110536, -4.225728611565368 57.51071392518735, -4.226338369110836 57.50967883374466, -4.227125688953078 57.50944838747379, -4.2278293247483205 57.5089408681044, -4.229073014194212 57.508693362462125, -4.230297948921369 57.50813887641838, -4.2307028302609275 57.50771569386089, -4.231745565242041 57.50603764947419, -4.232212579014216 57.50471931712747, -4.233765340912882 57.5034410605725, -4.234509920362408 57.500767297984794, -4.238608439847608 57.50067215002994, -4.243455085788591 57.50080077720097, -4.247758196394355 57.50139985923858, -4.247987502623983 57.5012744689043, -4.250191700579274 57.50183179248222, -4.252398982279067 57.50195053400953, -4.254036974162289 57.5021486009081, -4.255102923507728 57.50205676302789, -4.256360723156499 57.50226069906142, -4.257364530065546 57.50218972179395, -4.259846218690886 57.50258826033114, -4.260719191071027 57.50258789013854, -4.262470185370247 57.50312706706786, -4.26619814181429 57.50407818851386, -4.269060980855349 57.50499265324507, -4.270719728840411 57.50530604497622, -4.272183928684618 57.5050406610866, -4.273205643841938 57.504263875073754, -4.275700072800996 57.504138021040305, -4.2779475378637395 57.50441553440915, -4.28008818677917 57.504291490145334, -4.281378254689847 57.504580856340255, -4.283385692525467 57.504194991615705, -4.2848786763109175 57.504151778453405, -4.286351223857737 57.50431558500014, -4.288398085938197 57.504373708506286, -4.290826157140862 57.50476093402832, -4.292437427100821 57.504713685825216, -4.29353707716524 57.504281256896164, -4.295659648380569 57.50365768008392, -4.297074934208661 57.50360046094921, -4.299754814868378 57.50331979437296, -4.300827990934384 57.50337030766709, -4.301663753162906 57.50312323008477, -4.303349558422336 57.503412338440995, -4.304850806958694 57.50326270751016, -4.305206629194343 57.50273773670376, -4.306240649421688 57.50253822201378, -4.307797564272976 57.5026364380079, -4.310656437508019 57.50241695867925, -4.312177994237685 57.50266852350943, -4.313194089249492 57.50300392275662, -4.3162511387388856 57.50354086322894, -4.31832031464211 57.50431873671488, -4.32286035507749 57.50509871679374, -4.323515782081062 57.50503632399598, -4.325077950322359 57.5052932769541, -4.3273637509947225 57.50550990237855, -4.328404489783717 57.50536399532701, -4.328938848125877 57.50505221169208, -4.331085234361018 57.50481038088493, -4.332938439267895 57.504761736809876, -4.335457315308849 57.505124862935304, -4.339294129768573 57.5051867423408, -4.340797763891003 57.50549221152549, -4.34425447240824 57.505743403939114, -4.3458989058415165 57.50634272312408, -4.34940958547529 57.508236253313704, -4.353325858243255 57.50996666982762, -4.3546462531019 57.510429051315576, -4.357088263125146 57.511040293479134, -4.361494231569654 57.51204425116313, -4.363523422840937 57.51243935492073, -4.365346669827561 57.511819307758934, -4.367252392971923 57.511699987811035, -4.368044450503604 57.512276380818484, -4.369898843998512 57.512600981455485, -4.370638015100567 57.5125673223751, -4.372124426792011 57.51205863951668, -4.3729418365726485 57.51109616652511, -4.371959753158149 57.508519556470134, -4.372138901788718 57.508424532555715, -4.373229665832599 57.511697288891924, -4.373809064765349 57.51210871582604, -4.375403209211668 57.51235905904071, -4.3770224235129 57.51212188198762, -4.379570074718843 57.51201453794063, -4.382972263092516 57.51162052866555, -4.386865121551814 57.51081547770447, -4.388836805301089 57.510710756285754, -4.3925502965638525 57.510178498281284, -4.394888245917163 57.50970190945631, -4.396510025738318 57.508980857319045, -4.399230574687816 57.51305452454889, -4.403500392690345 57.512858627892584, -4.4047172084352715 57.516113105769136, -4.404348374478366 57.51698339602385, -4.40492378686458 57.51772776629359, -4.4089655431637205 57.51705715620778, -4.413211885628078 57.51644994873131, -4.416647284211766 57.52017437615817, -4.416487545328602 57.520246384743594, -4.411873913642764 57.520139373337095, -4.4157795049406845 57.52498915421193, -4.422069425613761 57.52364175451715, -4.423125548354366 57.52490453821364, -4.426478343101469 57.524102991949555, -4.427869410190808 57.525491998447464, -4.4282794233988225 57.526260927524824, -4.425461652346012 57.52697447995466, -4.4240879743724015 57.526481085493, -4.421703755310116 57.52637355822059, -4.423297831329898 57.52826604958639, -4.398732028773227 57.53139517957991, -4.391284176986901 57.52251483802691, -4.383770425656252 57.524337387974846, -4.378768058934344 57.52549211948126, -4.376112890766914 57.52344847889374, -4.375427356176495 57.522410057751095, -4.374726648140244 57.522935436980426, -4.368824534793597 57.524897467745916, -4.368254156495896 57.523370486559166, -4.366852577633937 57.52269041413262, -4.364384448480152 57.52385492258648, -4.36295391097437 57.523199593638125, -4.3619139733403385 57.52377249728292, -4.363398167745897 57.524516923489955, -4.36218193621009 57.52532479168768, -4.360753940036049 57.52459046876632, -4.355586130280644 57.52267427381849, -4.355370381636124 57.523990211750224, -4.35421179830767 57.52544955476806, -4.352748904598152 57.5255577067722, -4.35055852231789 57.52656000224536, -4.34791518964768 57.52727416174038, -4.343214411015094 57.527945622612506, -4.339602011740068 57.52683555318144, -4.338828424785429 57.52665883486638, -4.338570485185037 57.52820177671899, -4.327091590047152 57.53090029215983, -4.3274467866278705 57.531837362937566, -4.322814336317284 57.5333735683253, -4.319498048473062 57.534401664580784, -4.31238836365772 57.53678901319569, -4.3055283902254855 57.53882334272214, -4.302299708538456 57.539612408699625, -4.299472834559675 57.54043773002981, -4.301591008264242 57.54204689711567, -4.296004514306732 57.54434053770412, -4.301124597669371 57.54788641435305, -4.296872706318486 57.54857487943254, -4.301034793299234 57.55441152607869, -4.301451167293645 57.5565065503862, -4.3064421257693 57.559868296364805, -4.310646169461584 57.563904361960354, -4.312814263982957 57.56566176339353, -4.316474803574486 57.56379754376931, -4.321736133893143 57.561346678260875, -4.324967137988616 57.56298548162569, -4.3325044924618545 57.56423920318957, -4.327672250249424 57.567644063779056, -4.3188202323495135 57.57333305801859, -4.3108953602586375 57.58497978456818, -4.299744194860236 57.58669290267471, -4.277932447442674 57.59052062973378, -4.272536182151726 57.58876967465834, -4.270884176316927 57.58798748898907, -4.260086126776738 57.58065962728341, -4.25973978341719 57.580447193786725, -4.2565595546326636 57.581943983249374, -4.260374139518142 57.58440769144968, -4.257856039768803 57.584959124165174, -4.259162069305955 57.58633577473151, -4.261916736419358 57.58601838068676, -4.265517241420264 57.59147039683841, -4.269431703857225 57.59769418084492, -4.2709575486472335 57.59753801516054, -4.264770879829226 57.600048179369736, -4.260698899487258 57.601316169301526, -4.235624436782006 57.607279368272145, -4.230166832582126 57.6102780612603, -4.223852632756991 57.614342858490836, -4.220861204834346 57.61580615219759, -4.215050726474431 57.617175058363486, -4.2075327695941365 57.62187109204087, -4.178438637867728 57.62513440881589, -4.1732319708341565 57.62572721676053, -4.161322385584829 57.635563420400906, -4.144971095005157 57.63952580190313, -4.141926852909265 57.63619906765472, -4.129304168038237 57.63890323199545, -4.124980783859382 57.63313420709636, -4.114718396793409 57.63716495913339, -4.108249474389388 57.63287806895241, -4.103864349991807 57.630692646698265, -4.094754738955735 57.62608196695983, -4.0933576909901745 57.62450580635356, -4.084459432799523 57.61877714824962))
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>