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/S02001399 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02001399 geosparql: asWKT POLYGON ((-3.571414571332135 56.1929478847554, -3.573384542556561 56.19285978598959, -3.579993274703832 56.19304416910984, -3.584975013852475 56.19168196335112, -3.585358743705852 56.18953754602874, -3.5841016113675703 56.18777359373265, -3.583162290202446 56.1868376355474, -3.5830361001526176 56.185416813978186, -3.5823606058455337 56.18439030942822, -3.5875386479608515 56.18359696865016, -3.587995665827737 56.183661186614444, -3.5882076247049803 56.18204462767442, -3.5889370809148784 56.1817620864107, -3.588030194507793 56.181181582279955, -3.589311619547956 56.18119566316373, -3.5923955562610357 56.180968168035704, -3.596572882636019 56.18096100457894, -3.5980006325800455 56.181084528312766, -3.597476350634602 56.179401087744004, -3.5971805771075807 56.177400191048406, -3.59583679684423 56.17789918503548, -3.594941273271573 56.177709464167215, -3.5914258378770487 56.17416048530483, -3.592560205055232 56.173701977311616, -3.5931000643974147 56.17312442473485, -3.595410985646855 56.172528511684014, -3.5973363433193493 56.17230775190101, -3.597850048419551 56.17203333571385, -3.598062455525408 56.17136923781269, -3.5985721854111463 56.17130962940115, -3.602017157504234 56.17188144268717, -3.603747690251582 56.171895839130194, -3.605243459978648 56.171456784084434, -3.6057406186629715 56.17109628614259, -3.605901360419603 56.17027828975769, -3.6087663530278924 56.17053845549589, -3.610517535147077 56.170585735276916, -3.6114355380731733 56.17004540460567, -3.6135420563403775 56.1691184415466, -3.6151675397680996 56.16866678793518, -3.616615837070479 56.16863706703414, -3.617668276219206 56.168271051684805, -3.618904544544022 56.16829350107402, -3.6214569975363573 56.16855297333331, -3.6225630464026786 56.169191723337725, -3.623804927459784 56.16907926323932, -3.6258884495933303 56.16982644440868, -3.6268864663677585 56.169203173626464, -3.6267852104060827 56.168294244529044, -3.6270313280823947 56.16771591269156, -3.6284740165851037 56.16702207495686, -3.630103003139975 56.16639315612454, -3.632477392773181 56.1650004599669, -3.6331281963541215 56.164094176907575, -3.634478279794979 56.16384179623202, -3.6369734978536354 56.16359310587769, -3.6399387129582323 56.16281065959033, -3.64143547184343 56.16252570359956, -3.6425246059468757 56.16250044775957, -3.6446825814173716 56.161749281460686, -3.6468323895803327 56.159868664197646, -3.647863061040123 56.15968329312492, -3.647452807844 56.158977084236994, -3.648012196763124 56.15854099914908, -3.6470059867259867 56.15749138931514, -3.6458600647703583 56.15682553149885, -3.6447938353354288 56.15662946108143, -3.6435302427677883 56.156034339514456, -3.6418709536503084 56.155522641130105, -3.642804807342708 56.15439148929851, -3.6392791427711737 56.15322612767301, -3.6376200382485564 56.15229832617554, -3.6366661786471437 56.15129556546321, -3.6366727655275812 56.149898179066064, -3.637863046805483 56.14964787400949, -3.6404134307248754 56.14776923978725, -3.6419294839028225 56.14715782995755, -3.642996345672056 56.146998074808195, -3.644723927981857 56.14651680744707, -3.6439126036423084 56.14568652953181, -3.641900989635494 56.144829073335046, -3.641484454215528 56.14449763900954, -3.6397589275021223 56.142115938889596, -3.6392421058193483 56.14059969442307, -3.640605678421541 56.13962639402255, -3.640894958759096 56.138974670416815, -3.6436993585495663 56.138278719996585, -3.6447915532559136 56.13762528549718, -3.640044374563015 56.13366453365557, -3.636868288158373 56.13424132483612, -3.634256133711572 56.13362354072788, -3.633962798347718 56.13319520149622, -3.631944172616767 56.13291097869805, -3.6296917559709834 56.13291915275515, -3.6283533543664794 56.132775958247834, -3.628382858983098 56.131228204597534, -3.630077556634846 56.131275040782114, -3.6319071066373017 56.13120056004349, -3.6347751378429582 56.130648640250335, -3.635047376157697 56.130201134543725, -3.636445015044532 56.13037671896194, -3.637272094770594 56.1296585692413, -3.6387706438209935 56.12989928502351, -3.641053584213234 56.12993187357034, -3.641570846626835 56.13017660426656, -3.6436502465868124 56.129661913854996, -3.6456345158141814 56.12993472096503, -3.6464766907420585 56.129573945589975, -3.648700879671353 56.12958740691758, -3.650733041724234 56.129174767516815, -3.651887769061212 56.12807473003129, -3.6520080441926908 56.12739019260318, -3.6529060960687314 56.12668446474874, -3.6541603426585874 56.12509513766625, -3.6546219998984912 56.12486879842961, -3.6570816706372575 56.12430385299091, -3.659316539062761 56.124174703543794, -3.6588493550777836 56.13012456116438, -3.6611843495094365 56.130542473011026, -3.662044583887606 56.13332551938247, -3.6649588184688113 56.13187548028846, -3.6675683228910056 56.13082486602778, -3.6703796076810167 56.12997814299561, -3.6705401726380336 56.13071281771607, -3.67471390519008 56.13196824093141, -3.67471219864191 56.13266916711224, -3.6751810992782747 56.13383097997191, -3.675418751378344 56.136334832088465, -3.6752077209254685 56.137653953549574, -3.6746461163095705 56.13796746789539, -3.6729873995670697 56.138155986994036, -3.6708062979465947 56.13796086091531, -3.672102100325318 56.13926426610581, -3.6732959160250256 56.14081164161788, -3.6740963517490233 56.14219361426296, -3.6766206644201005 56.14179500766955, -3.676271960897032 56.1432489182657, -3.684575810225676 56.14158315106423, -3.6847635014876468 56.14247092732097, -3.6859506037487644 56.145321246092585, -3.6871397777149424 56.147872769820886, -3.6872464926912483 56.14896879178998, -3.6880176236869584 56.15061269429447, -3.685147501148906 56.150876524990544, -3.682216123520178 56.15158143306037, -3.6799129449465693 56.15192727951008, -3.6795217428799654 56.15107893597556, -3.6779499606634807 56.15232237826536, -3.67660182832248 56.153158389738564, -3.674851439853045 56.15363143542129, -3.674446692065666 56.15431985055013, -3.678250693486785 56.155157842255036, -3.6787515087045435 56.15587217586609, -3.6744645909164215 56.15516788647625, -3.6745225409177897 56.15791318424491, -3.678264622991066 56.15732325748914, -3.6798774322522942 56.15770207561073, -3.680988817338102 56.15888338937608, -3.681766385325051 56.15879114419725, -3.682539938491746 56.16032430285662, -3.681929125540249 56.16075525326747, -3.682074665615618 56.16106138098125, -3.6862212457798123 56.161154720928906, -3.68783541039219 56.162054107368974, -3.6885688214322143 56.16172222927315, -3.6890202971193333 56.162856102535045, -3.690955914125163 56.162809860058985, -3.69306732754754 56.16219445407801, -3.696178767627579 56.16161508780306, -3.6964968334890203 56.16255568208041, -3.6923346209924386 56.16348439293267, -3.691589222187137 56.16374620544521, -3.6896375170764486 56.16404248605859, -3.6880453231615564 56.16409120509168, -3.6870388371741405 56.164275683013884, -3.6871897910646574 56.16490413666957, -3.6853643847408053 56.16511363575704, -3.6834393626715887 56.165621880433434, -3.683971681591832 56.16665643539249, -3.681021015806628 56.167432474871504, -3.6767436038717256 56.16743249476997, -3.6772036999223583 56.16843322807649, -3.678902287463 56.17077267976521, -3.678783844776909 56.17147809081773, -3.680063613126219 56.17352971864256, -3.678660665132117 56.17507127994144, -3.6811966929669664 56.17702775685996, -3.683316474844461 56.177467355680584, -3.685315137069772 56.178337997015696, -3.688419850833829 56.17888119680929, -3.6905552544542286 56.17967180804598, -3.6934674016730455 56.18060789975041, -3.6941212991474943 56.18114547029507, -3.69559203649013 56.18185188485746, -3.696924695745591 56.182112158615496, -3.700207718428884 56.18239053415705, -3.7040795199179666 56.18395468809526, -3.709598298874592 56.18522635821574, -3.7131679495878442 56.185419553912105, -3.7139630243802513 56.18554332050973, -3.717507231144111 56.18660838024507, -3.719011642368456 56.18745910355122, -3.7202668872519284 56.18850198163434, -3.7184065510643127 56.19050476406164, -3.7177941652419535 56.19163441467175, -3.714319922755313 56.1933651159184, -3.7140003909866297 56.193427955240665, -3.7113616575433994 56.19226040056841, -3.710809444836438 56.19216470617897, -3.7062528766714133 56.19335092261158, -3.6988990670316295 56.193793669485274, -3.6925371185777505 56.19443806276225, -3.6864589102980765 56.19586002735233, -3.6847301767377814 56.19521415122888, -3.6815814806491773 56.19434495641943, -3.676582979166393 56.19337048917563, -3.675115529552796 56.19298152628843, -3.671124750249943 56.19071269727806, -3.6669413075448847 56.188635043255495, -3.6662707104973538 56.189335994820176, -3.6651520455701845 56.19011037287674, -3.6640927363754447 56.19114003778029, -3.6632608699822424 56.191654440322665, -3.6623567247678532 56.19292012622138, -3.660855861906036 56.19399164203151, -3.659618688941678 56.19408014398297, -3.6592239346856847 56.19464256418899, -3.6592860992837615 56.19552234072066, -3.6589341168357423 56.19614259349818, -3.6576254321092665 56.196443201323156, -3.656776326964155 56.19749693857224, -3.656244331365055 56.198429606001845, -3.6543141164214115 56.20020768303109, -3.652965292564149 56.20089067394309, -3.6529241684434015 56.20124616312015, -3.6518009752179563 56.201741915055536, -3.651904312814726 56.202270696798585, -3.650557496160206 56.20300305546175, -3.649556494882873 56.202216678695734, -3.6491019169438466 56.20214187067776, -3.647998875857467 56.20104233967638, -3.6470723391262605 56.199922476185556, -3.64564092749418 56.199056436811404, -3.6421331865621647 56.19788101085603, -3.638966796515331 56.197145758281316, -3.63806816847564 56.196861137480155, -3.635567910660443 56.197761368861514, -3.6324479929636837 56.19906510650486, -3.6290376412087992 56.200138960889376, -3.6283304111079615 56.200110537415114, -3.6259183815510236 56.20104894529909, -3.6238716429726803 56.20277595002404, -3.622798772659049 56.204014794569815, -3.619596667489388 56.2053031041792, -3.618199433450505 56.2056592625998, -3.613576020724781 56.20547347675996, -3.6132021238382026 56.20490057300997, -3.61098304327916 56.20499689927504, -3.605873641311859 56.20448198999554, -3.6042788187617174 56.20443260493281, -3.60285696413283 56.204548990035114, -3.6028893369513515 56.2041657788056, -3.6019860841511995 56.20392229102045, -3.601030486881157 56.20222917997927, -3.59999166151842 56.20139976765632, -3.5984609635687277 56.20071868148311, -3.596331654423879 56.19932375455942, -3.5941752950553547 56.19878817452515, -3.5917411328053994 56.198470892675836, -3.5896802795539724 56.19844888608937, -3.587894060126719 56.198675816289324, -3.584317781486755 56.198021711912496, -3.582608110214322 56.1972115343456, -3.5803109990726285 56.19598921234144, -3.579358091817734 56.195705760425945, -3.5775466944584156 56.19574955532305, -3.575419125527005 56.19614060623916, -3.5737147623779424 56.19525655712917, -3.572264052760255 56.19422818404083, -3.571414571332135 56.1929478847554))
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>