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/S02000816 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000816 geosparql: asWKT POLYGON ((-3.49925458727211 57.668640671879494, -3.49805428758751 57.669211875136995, -3.496020960630405 57.668283677176404, -3.484661191300904 57.66722270423794, -3.474452091216405 57.66764756794434, -3.470846061801804 57.66346728554162, -3.459735096453363 57.665204056181246, -3.45991781778278 57.66557925843212, -3.455935148837623 57.665337691456564, -3.442866637805998 57.66720328215506, -3.445522429999465 57.66737958111141, -3.447264289651658 57.67154596662366, -3.451498757706941 57.67094047187887, -3.451892812947568 57.67240924028067, -3.45108550466062 57.67234663995212, -3.451428319816265 57.672953581585844, -3.447765473467885 57.67276198977906, -3.446372697541995 57.67315522158749, -3.447625114298897 57.67597069090452, -3.444536114552355 57.67588022958197, -3.443390435688317 57.679118372026615, -3.43828054878578 57.677972786324574, -3.438009742989209 57.67917964437695, -3.432790361142502 57.6786549423186, -3.42572498971707 57.67937258425773, -3.416182238255315 57.682794638063825, -3.410264839758358 57.68243851618706, -3.408702949347838 57.678925438238984, -3.404562349577696 57.680265083666946, -3.400539769109342 57.68075884434089, -3.389104150218477 57.679492494370486, -3.369191440940154 57.68331187260352, -3.367806581732896 57.68305734922222, -3.361228793751819 57.68520343579678, -3.35736337122291 57.68679009325926, -3.357509957657578 57.68757006990579, -3.355521760805981 57.68822920006032, -3.358829537136466 57.691454663072854, -3.347737370492532 57.69180685357322, -3.343556897687922 57.690809248240235, -3.339348013009609 57.684035552506394, -3.336335359570094 57.68329483774302, -3.335767409024732 57.681989276157516, -3.342615533528374 57.6812340600442, -3.34134407015878 57.67632469960705, -3.335111831940001 57.677387773983945, -3.328705869370247 57.67078202115112, -3.331634742116915 57.66930943435293, -3.329467890752919 57.66424682824726, -3.331176526661132 57.66363649421946, -3.327957219962281 57.66048048700114, -3.330020809282149 57.65981488923806, -3.329353425694385 57.65912094891176, -3.333512490080336 57.65793298626134, -3.330541680468581 57.65795595906143, -3.329922975831875 57.65783788595116, -3.330300173059988 57.65748241013602, -3.331432708065975 57.65753751614553, -3.336461244386096 57.655732737243156, -3.342214051654777 57.65449499433953, -3.341057414528217 57.6531238278096, -3.339136879609983 57.65284773459994, -3.344349525825539 57.652424112703606, -3.346617204747518 57.65121417727523, -3.347555218579455 57.65120418526063, -3.350284897057342 57.64845311687006, -3.345237836035691 57.64835418773361, -3.344702296942138 57.64701238532969, -3.343135928280577 57.64724462848947, -3.340295253954582 57.646088971188966, -3.338520991321096 57.646134717748616, -3.337906821742681 57.646849286907425, -3.334943287129638 57.64680136849071, -3.335222239986887 57.64572940562554, -3.33910364023454 57.645115156977916, -3.338917470921093 57.64465838599631, -3.341954136170136 57.64348421900133, -3.341784963480216 57.6430043757055, -3.339490023361289 57.642423316241, -3.341364130350593 57.64100204411991, -3.337412344481298 57.640136595585126, -3.332083260217574 57.64190866652636, -3.329190469547269 57.6420558894775, -3.328666208091761 57.64254354387771, -3.327574641485742 57.642306435441746, -3.325438932936021 57.643002588840666, -3.324302143542575 57.642412900571905, -3.322272219494377 57.642990825952026, -3.321856739813989 57.642413633251756, -3.32059506759019 57.64247833976415, -3.318194896897489 57.639433763091056, -3.323370811919337 57.63773307710247, -3.323093065573352 57.63741116036702, -3.326433535305742 57.63607027818876, -3.322655082883217 57.63444488449213, -3.318667500623267 57.6317376436557, -3.311636963445797 57.632851649362074, -3.312643428661184 57.63428751169755, -3.311996298744562 57.63445592782997, -3.311260641065475 57.633098100891736, -3.307960947553723 57.63402162382191, -3.308591203173074 57.6352458141172, -3.306298378674313 57.63529647802932, -3.305115384945867 57.634069006161695, -3.302519959896538 57.634086785075155, -3.299686192636455 57.634933427010935, -3.294016274450482 57.63798301859217, -3.291661493718459 57.63807320089459, -3.291476329044948 57.638989801174226, -3.288280728126601 57.640207694770076, -3.286293482618023 57.642221082287584, -3.283825173883724 57.64167045809859, -3.279796636660194 57.642802621547695, -3.282502708643309 57.64493660324924, -3.281767749753177 57.6454740656644, -3.281001641111927 57.6452970795757, -3.281658197865313 57.64796864373569, -3.279635751844879 57.64868470578699, -3.278321716519396 57.64848240268515, -3.277731410049353 57.64789547549082, -3.278086039726623 57.64606829460198, -3.273299499620131 57.644301965468514, -3.269311782676093 57.64524936706917, -3.26697082420479 57.6463418497657, -3.265357351519653 57.646196308460404, -3.263519712270608 57.64490312484085, -3.2650073300774 57.64432231217785, -3.264569459420349 57.64329361697219, -3.261902715979941 57.6436885712692, -3.262088939339221 57.64277941389915, -3.264082409217967 57.64133116117491, -3.248102311751127 57.63795053287993, -3.246563527059168 57.633617830848124, -3.248341012315563 57.63368114010654, -3.247078118312354 57.63006438548869, -3.249287928712698 57.630042566761006, -3.255360902661009 57.62792524279768, -3.246556455673114 57.62119409824725, -3.237652309154969 57.62324893185448, -3.23674321682156 57.622611033669706, -3.234898144327058 57.62300637153804, -3.232342456201024 57.62170179119021, -3.229372511330078 57.62299735069186, -3.22856060236645 57.622753712162094, -3.226777763700343 57.62390536928233, -3.223861338893965 57.62242200135782, -3.225326814847863 57.62019793905843, -3.219816054420247 57.62061955314266, -3.219050566270824 57.6202586272661, -3.216353996678774 57.62122784113072, -3.21475586185886 57.62100066585909, -3.213418137162794 57.62155250807372, -3.211354964060253 57.621437544338626, -3.209241335203185 57.62007733192423, -3.20835517745413 57.620343363545466, -3.207503737290387 57.619911320162124, -3.205075434197149 57.61988957760655, -3.203895065560909 57.62064643209934, -3.201313260451761 57.62003319148802, -3.195791113349089 57.620624546899165, -3.192715109121403 57.62178548990631, -3.189332685388299 57.61972428657815, -3.180873100498796 57.6159588166053, -3.178853631348238 57.61924753494116, -3.175786499718294 57.619141351726924, -3.175024203284702 57.62145711078833, -3.172457574825151 57.62130128366206, -3.172557380521302 57.61764422297737, -3.167827822643309 57.61685257427705, -3.168684236316782 57.615892451664315, -3.166440633169165 57.615356223048394, -3.162270691161631 57.61739787113542, -3.161031331279026 57.61633129299058, -3.156421331204402 57.615601003121014, -3.14932542744569 57.61671675540668, -3.151488986153427 57.614199761707994, -3.143378065574294 57.61017710078209, -3.143396378174174 57.609161849461714, -3.140393879436245 57.60841642643115, -3.137508230217998 57.61140685987403, -3.13240708768088 57.61045555194198, -3.131663875043196 57.61077661122819, -3.132948789170727 57.61226527565572, -3.13211637392492 57.61402443979153, -3.12991858248773 57.6154814010235, -3.127006959643719 57.61551638541081, -3.126551717080087 57.61595163167898, -3.123561596703642 57.61940977292717, -3.129426717112931 57.62066903697518, -3.127259857932415 57.62314075260129, -3.111940140827403 57.62260271841052, -3.109841651059771 57.62571132322124, -3.107207195080654 57.625527815043334, -3.105231167174096 57.62662307616883, -3.105040281013377 57.62750508124107, -3.105810259119989 57.62804631035097, -3.104520823408328 57.628596566802514, -3.10058326447093 57.627966171560686, -3.100504799086682 57.62703261550189, -3.103494983603992 57.622380282464626, -3.106560049593609 57.62021552208041, -3.105078060828315 57.615352749543, -3.110038226673025 57.61136051349147, -3.106828045889962 57.610270931674044, -3.107328858991275 57.60689178823558, -3.106053816481859 57.60571698106503, -3.105603852492565 57.6033200412177, -3.106540448764807 57.602501110354034, -3.114161177613469 57.601086687094885, -3.116487893376057 57.59947613932491, -3.118067031349177 57.59368605455557, -3.116258555194908 57.59144728200786, -3.117057498341527 57.58967953493063, -3.123580261837958 57.58747473251569, -3.12426386018501 57.58469286675496, -3.126382613576456 57.58289533812228, -3.125530954966887 57.5813129126111, -3.126712465352278 57.58139152419345, -3.127053292796468 57.58420900329575, -3.130144139291823 57.584431732134384, -3.130993783518931 57.58523294325399, -3.132742995625399 57.584976549369856, -3.132895884858301 57.58355013989307, -3.131405161994081 57.58346720773622, -3.131798857330291 57.581711260342864, -3.132738630573299 57.5813754624067, -3.139341221065956 57.581728092626406, -3.14233282359787 57.580596148762986, -3.148177621340675 57.58085755956637, -3.14891355238637 57.58032985040516, -3.150382543904431 57.58077461495643, -3.150473796093942 57.580486322877384, -3.15762955361826 57.580950949983624, -3.157278283644654 57.577262088921394, -3.158522793412599 57.577484246100816, -3.159600712019222 57.57667485307387, -3.16432114296725 57.57735008137782, -3.169302040407063 57.5772771152065, -3.176280172199642 57.57639485244608, -3.17854685533979 57.575646079655606, -3.184797057537895 57.575533708217215, -3.201147039365885 57.56933355481843, -3.205727251694392 57.57386238670388, -3.240569435648048 57.57472871335488, -3.240936165681084 57.57273081918641, -3.253637024147969 57.56670335490733, -3.256884081835146 57.56827010977847, -3.260673871435934 57.57150226682041, -3.263927599391805 57.571793144001454, -3.264430992271598 57.572327106602344, -3.270108867690342 57.57212640055999, -3.273749420132625 57.57391335246853, -3.294471990675643 57.56511455168069, -3.301615481209908 57.55917496804388, -3.301835779630999 57.55691786476188, -3.307157728862186 57.552919160725104, -3.314462569958519 57.5446234750045, -3.334562551681027 57.532761004341644, -3.335600322397197 57.532821899155294, -3.336373456949276 57.53204112984643, -3.340424306682444 57.532285644659154, -3.342811294163873 57.53179313183492, -3.342929672580834 57.531378629444724, -3.344076176963873 57.53167186308683, -3.344913748660247 57.53128563878084, -3.346065229584799 57.53171355040724, -3.346612063116021 57.53204909573465, -3.345962444024178 57.53255011887042, -3.346827650000443 57.532460037528026, -3.346911845125097 57.533384441012444, -3.347629124379006 57.533358809299, -3.347723415726513 57.53410343615293, -3.348868384925378 57.534351723994085, -3.34851720113368 57.53524484635739, -3.349225335078206 57.535875108409954, -3.348318929043695 57.53620819768586, -3.35048748985644 57.53838597433707, -3.351567817052864 57.538688836520855, -3.350494040350663 57.5390147557762, -3.352645363138413 57.539818195605434, -3.351579478014796 57.54080882121428, -3.351760700558495 57.541642343652875, -3.353621320962508 57.54226023396937, -3.352031821689444 57.54445127458759, -3.353355483835594 57.54500304770416, -3.352890125890955 57.545520094309225, -3.354438840127055 57.54763258288848, -3.356273417457197 57.54843937138634, -3.356430731837307 57.54907550067804, -3.358436098159739 57.548631718292185, -3.357612999371691 57.55031150609429, -3.359160081786805 57.5501241708524, -3.359307849566687 57.55229658610338, -3.361303067629424 57.552472722131085, -3.360197357163068 57.55419150510929, -3.358756662399215 57.554099223131296, -3.358414561390883 57.55478564271395, -3.360540950888856 57.555786864602645, -3.357776081512208 57.55649039679023, -3.357438279487591 57.55774273307854, -3.354682517295556 57.55914680164969, -3.354474617079232 57.55984974443363, -3.351721635435763 57.56088539479358, -3.351861895967882 57.561512732894606, -3.349791237910973 57.562019998004295, -3.349116382434987 57.56411137018125, -3.345772339380995 57.566356980382096, -3.345720263680068 57.567210969224895, -3.348575838556489 57.56757578114954, -3.35057484989759 57.56963857709562, -3.3495486384333 57.56991905225885, -3.350738242820162 57.57179286011644, -3.350489453695884 57.57229859888427, -3.349293378481466 57.57250901550398, -3.349272430745765 57.57510546198436, -3.346327583460136 57.577319858692086, -3.345768214821735 57.58073952947869, -3.344233863025566 57.58266934114328, -3.344562295982709 57.5834114643322, -3.342576872132048 57.58399854363543, -3.341483918008815 57.58520495294027, -3.341244062096004 57.58777676011427, -3.339802124554522 57.589049732820534, -3.336225426283782 57.58959069173713, -3.33399546678832 57.595390590764325, -3.332828768993041 57.5964270096124, -3.333172688577707 57.59942383722031, -3.335127686560869 57.601631071931045, -3.338485148550857 57.603742578633415, -3.337822012611244 57.605734937640435, -3.338103692406217 57.60975651415309, -3.336589671797442 57.61137157787095, -3.338288369746671 57.61250347385714, -3.342334444239113 57.61282888118511, -3.343250770302208 57.614525991127614, -3.341482670316342 57.61768016164493, -3.344784336086152 57.61745100415957, -3.344502723554332 57.616804008986385, -3.348883217227803 57.61471057446768, -3.348530239461719 57.6137960143279, -3.350599517061308 57.613172026811846, -3.356603809910558 57.608993309501855, -3.360324330429633 57.61181004454116, -3.363338586893394 57.61273878759146, -3.364483920199229 57.61203470575642, -3.367838751150581 57.61222302425394, -3.370610158545116 57.6111419338186, -3.370356647280027 57.6106326280139, -3.371993711215515 57.609213456021514, -3.38025845543306 57.6073537136855, -3.380309614874601 57.60782029198232, -3.381378284285187 57.60775471729583, -3.381757505331102 57.60936759206918, -3.37385509854631 57.613298665730426, -3.375177999809824 57.61464077818692, -3.379189353227776 57.61315072709157, -3.379953496464746 57.61387902753173, -3.381808233397857 57.61687720105766, -3.379682569551471 57.61777182099917, -3.377830429637817 57.62014568384005, -3.382013551963349 57.6218428000258, -3.384863814654913 57.62018559775479, -3.390841285879674 57.62282399019073, -3.392911586351529 57.623106628308264, -3.396181867115344 57.622369836844804, -3.398165044816202 57.6225635139985, -3.399460753706459 57.619683451314536, -3.413437007866913 57.618728398111784, -3.415725438633328 57.61816377284759, -3.424729086380194 57.62192551175006, -3.425731072559478 57.619722237849366, -3.427779791818174 57.62243907857633, -3.435146400904346 57.623271977143645, -3.442521818185627 57.63023111176232, -3.436860797295756 57.631068162607, -3.430657734278102 57.63303403812087, -3.434430232415162 57.63908210449424, -3.441876269927863 57.63802718494988, -3.448945094760717 57.63760500000412, -3.452648883191095 57.640167721704074, -3.451791445188121 57.64046040682113, -3.452267219075419 57.64109741091228, -3.447716404881268 57.64250611319435, -3.450979441688236 57.64325027650412, -3.452471619896002 57.642020367584855, -3.453150101420692 57.64237551439919, -3.455956298322792 57.640327635125175, -3.45542628243488 57.640081883018254, -3.460367345755683 57.63881212921378, -3.464807478400575 57.642669808801415, -3.470219630844096 57.64053059344149, -3.47770194618816 57.645743664994875, -3.476712099667165 57.64612356416314, -3.480914160889042 57.65015295797791, -3.484324758488782 57.649142741535364, -3.485561364667292 57.650700351296265, -3.485179195425356 57.65118994977113, -3.487323705917795 57.652659401382635, -3.48624020450147 57.65286162430388, -3.487300347272795 57.6535855647379, -3.486142004175767 57.65465528502954, -3.485066290651067 57.65457808241344, -3.4849277633972 57.65528940653236, -3.492944573252676 57.660737897939185, -3.488114301343965 57.6631215598644, -3.49925458727211 57.668640671879494))
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>