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/S20001082 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S20001082 geosparql: asWKT POLYGON ((-3.041342634063883 55.90275454295724, -3.0433562035878423 55.899773301126494, -3.039793171802389 55.900299752788754, -3.0405040727106565 55.897944559839395, -3.0357103849516682 55.89642761593186, -3.0379466769226955 55.89486537212397, -3.0371851701219352 55.89438124269185, -3.0394852461340847 55.892285589986635, -3.0360719422835114 55.891216909183726, -3.040682983496639 55.88941365134877, -3.043881432786576 55.88862599136013, -3.046356190872543 55.88842632279895, -3.0487439894454127 55.888630636948896, -3.049801278300147 55.88747553652887, -3.046211306279381 55.88659171182009, -3.046755209294675 55.88601760200146, -3.0491763402924486 55.88672967631076, -3.050243535641461 55.88602536560242, -3.0496387208485753 55.88343460520437, -3.046596160736712 55.88393979546438, -3.0473366497072756 55.881956510007434, -3.0457222440310723 55.880248390498075, -3.043964945026883 55.88035078158556, -3.0406060730020426 55.882068481353556, -3.037086278765523 55.88075936545541, -3.038674882806966 55.880448455588606, -3.040716102984961 55.87947568510658, -3.039799718542289 55.87638582017328, -3.037530040971899 55.875822640075896, -3.0380185300837583 55.87436288512089, -3.0356808106242896 55.87420287232127, -3.034706757970243 55.87469204531957, -3.0316680145517214 55.874276399995985, -3.0317113988496356 55.87367290021634, -3.0288851362970046 55.87323118049802, -3.0281887773529554 55.87253151175978, -3.029922348714519 55.869468825406294, -3.0305091654720506 55.86902576635278, -3.0330626021437532 55.86949855460762, -3.0354516327907213 55.8706620432652, -3.036399165928371 55.86937166001089, -3.034546287301032 55.86880319915212, -3.0345142790542954 55.86759942295589, -3.031587546092439 55.86690515982843, -3.032635290701103 55.8648387122023, -3.034641458536558 55.863508695322885, -3.0362879510658427 55.86407928952886, -3.039928390491051 55.86249742586774, -3.0408509159970007 55.86314313135336, -3.038223542210898 55.86527690206051, -3.0481797612756143 55.867823564669465, -3.049172035484788 55.86735245787607, -3.0515141288386616 55.864544053258534, -3.0520749664915305 55.864602157486324, -3.0543565851766323 55.86212061806324, -3.058679896787098 55.86288454036837, -3.0590446665455704 55.861704209040816, -3.0585794128035433 55.86040819789685, -3.062093968439959 55.859933508535, -3.062917521365236 55.85889342005733, -3.0650018189332258 55.85942290769516, -3.0652140105915158 55.858630467360015, -3.0641718150423434 55.85789346797312, -3.062221252026338 55.85757137354381, -3.0640194849772127 55.85616128352725, -3.0608220972892703 55.854236074151544, -3.057240101744256 55.85122347804364, -3.055494827407018 55.85202349616973, -3.054206240288164 55.851851537666505, -3.0534100906420236 55.853125306224086, -3.0501974769356375 55.85181707470535, -3.0479300960047464 55.85023642317448, -3.0437080758430843 55.84814020123213, -3.03966645204671 55.846916460749064, -3.039055234303586 55.84762051279543, -3.035642690807414 55.84595417509374, -3.034593030705053 55.84482218816881, -3.0324664789725997 55.84402088740312, -3.0343513385272134 55.8435236490924, -3.038463664411828 55.84193969477125, -3.0410623459091646 55.84118092186153, -3.0412740923287545 55.84073884217153, -3.0378914322132275 55.83872880930226, -3.041182840023406 55.83744354173687, -3.041710407847371 55.83816263658292, -3.0444844114145395 55.839570471308235, -3.0437910561144 55.83780931129746, -3.0427904456912267 55.83708402711904, -3.0482318422755545 55.83463252680594, -3.05131080739024 55.836313489202176, -3.048565845626695 55.83719338817472, -3.0492034821268152 55.837834635294136, -3.048251118895914 55.83947140723224, -3.0485379988469306 55.84066815364482, -3.0510164705460667 55.84079076967797, -3.053188861008459 55.842760925365354, -3.055304748207772 55.8430898916096, -3.053211439644694 55.844837614924415, -3.0570905437439038 55.84719361471124, -3.0601711247215326 55.84682233301303, -3.060444955142058 55.84749388313043, -3.0639846623036573 55.847274625073496, -3.0647206497770574 55.84965839321692, -3.061126510883643 55.8497340067955, -3.057629169846814 55.85041538704298, -3.0584858923993097 55.85116253283858, -3.062441335940324 55.851307924646704, -3.0625874432167066 55.85179404168002, -3.0650528455269357 55.85230593566248, -3.065415428468186 55.85489892749908, -3.065342806290114 55.85663471484453, -3.068507759040796 55.857865234829944, -3.0715382462739833 55.85768612705448, -3.0720212969723666 55.86067350416178, -3.0722556960141585 55.86414061347142, -3.073183831731904 55.863475298740354, -3.0739051803215545 55.86132829069463, -3.0768009518099007 55.86313027660799, -3.076375100156054 55.865168068728295, -3.0738201603560014 55.864964442667834, -3.071762242897984 55.86530819665312, -3.070717695747733 55.86495093501732, -3.0687987122834293 55.86521583196325, -3.0723358609342384 55.86731223926672, -3.0744177898620113 55.868149755262124, -3.0750866598576496 55.87105249597049, -3.076945925095052 55.8704699848412, -3.079190090572393 55.871492808715196, -3.079070951067562 55.87306118626125, -3.0796927139498305 55.87411872103707, -3.0816905376061943 55.875702744851196, -3.0798790188091765 55.87653190840744, -3.0793762871113284 55.8782033077564, -3.07829610420126 55.87967289301536, -3.0756014369938636 55.88062751611416, -3.073891879428292 55.881606967944016, -3.0716964907783177 55.88145961226338, -3.0714477794522446 55.88273863448867, -3.069513195908023 55.88398617618965, -3.0709136319062904 55.88498080775254, -3.07270613656125 55.884446209576105, -3.0741425833749827 55.8850700575908, -3.075242488665614 55.88396399408009, -3.0744007968409734 55.88353544990136, -3.0764047127240484 55.881371460640224, -3.077680411415919 55.88192281102518, -3.0840314005669915 55.88214820425653, -3.085313301842817 55.88088784916795, -3.0939440126957884 55.877224660511175, -3.0915963876487353 55.87555852052358, -3.088098700201891 55.87418018764956, -3.0853152577895395 55.873588821612515, -3.0880636398808923 55.87030978420999, -3.0893647216228985 55.86913587736145, -3.0922138626989293 55.867678050576586, -3.099925914698226 55.86259371373105, -3.101713292740761 55.86379399061767, -3.105771921835209 55.86436667601534, -3.107428915788789 55.86497756773549, -3.109333007949415 55.86423484133183, -3.111777442996432 55.86485287823623, -3.10851292615473 55.86602846512005, -3.1120375713327064 55.86721081184485, -3.115714064687582 55.86557811334561, -3.118972890395668 55.866341269037505, -3.1161144762424375 55.86818150915335, -3.1167890025186096 55.869022767173064, -3.1193305871273105 55.870405430415445, -3.120526331391655 55.86972809364052, -3.118997880628172 55.86910434526685, -3.1220507816550596 55.867172688347424, -3.1264646433450705 55.86822075089592, -3.1293103370810185 55.86845014479376, -3.130695251518395 55.866977891776685, -3.1334940867723957 55.867608085962736, -3.1348635900583557 55.86859259607505, -3.1321520642451692 55.86871667365029, -3.1323362920147373 55.871752083343964, -3.1358798103768355 55.87050976995792, -3.1366567570917927 55.8711077620082, -3.1390463586321458 55.87142346021448, -3.138009597479276 55.872895661062856, -3.136420817952895 55.87343377589394, -3.1354684725525575 55.87202381611057, -3.1319868463672758 55.873038168932865, -3.130455599144662 55.87247922911896, -3.126458299538604 55.8724518610551, -3.1228350765707216 55.87313305179263, -3.1240463969646504 55.87467648583958, -3.1232789905426053 55.875222622544726, -3.120295480986 55.874748516562974, -3.1177660801857194 55.87693513243457, -3.116515070272467 55.87957025035197, -3.118990272100572 55.87948487282686, -3.118893975491519 55.88203268453284, -3.120953782632505 55.88105743703722, -3.1227757750002723 55.87883039854838, -3.1257055037238404 55.87843523143249, -3.128229757081238 55.878946215630435, -3.1274018759656794 55.88122669253863, -3.128460392956996 55.88341471846586, -3.1248731349054344 55.88311680809171, -3.118180622346741 55.883014532581015, -3.1147232961070683 55.88398039171976, -3.112525974756694 55.88373070799611, -3.1111156002020213 55.88416577414688, -3.10843364808724 55.88261748902543, -3.105410804279563 55.881440621533116, -3.1007969210855246 55.88233632925678, -3.1003031524591207 55.882914529066746, -3.098040144427412 55.882842727363254, -3.0928394717073 55.88063636856773, -3.091155368993613 55.88155739479904, -3.092271038867807 55.882025174714265, -3.0892495726119624 55.88373223051955, -3.0918597566824446 55.88559604841929, -3.0903881177792627 55.88624856127467, -3.0916090610894287 55.88871654203318, -3.092261023365912 55.89114068232699, -3.089323049163961 55.89091993578346, -3.083776058775844 55.89096889788153, -3.0828893439512153 55.890707138578236, -3.082875480107238 55.89307941427827, -3.08142720097742 55.89275069954283, -3.078529231804059 55.89607377399252, -3.0812987729832675 55.89697517508756, -3.077790846037061 55.89758040128093, -3.0752919422834046 55.89925341290069, -3.0729761366676986 55.899518066148744, -3.0724859906973396 55.8967106223257, -3.069938207669545 55.89652612738715, -3.0686884983555665 55.89702219452036, -3.067997667031357 55.89807049681476, -3.064911362690157 55.89749521046265, -3.0630003056616357 55.897540444012066, -3.060083598164531 55.8967949975272, -3.05471162754019 55.897025923376845, -3.053278282991936 55.89901498089938, -3.047878578866653 55.8986387468079, -3.0457280146102765 55.899546585686096, -3.0469775831359405 55.90085631304757, -3.0453185219765126 55.90301484261367, -3.041342634063883 55.90275454295724))
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>