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/S01005152 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01005152 geosparql: asWKT POLYGON ((-4.468704102899936 55.83184205926265, -4.469025802899534 55.82977457803166, -4.471752732953459 55.82775852135737, -4.473422585465072 55.82544090008816, -4.474670847769334 55.82372603077818, -4.476354389907425 55.82266307025221, -4.478470545518067 55.82045873092474, -4.477007498174471 55.815275156319814, -4.472769658822824 55.81641208050353, -4.47043573592891 55.815856810652555, -4.470213033621506 55.817631927075226, -4.467659196310977 55.817889944805934, -4.467933501780236 55.81944835289755, -4.462999945953566 55.81918778034918, -4.458536505962325 55.819798432369915, -4.457353499396003 55.82053213732058, -4.456886099432632 55.82020892408134, -4.449430985585698 55.81996222460339, -4.451025130026611 55.81840249482657, -4.450258824160391 55.81814813240608, -4.447758534407686 55.81900686111559, -4.445631391516573 55.81894104523677, -4.445028238879718 55.81886936986081, -4.445747624883471 55.818507608107076, -4.444788117091484 55.81771777807011, -4.443814956715019 55.81696416412886, -4.444189513375261 55.81631857204259, -4.440101477692648 55.81604027096387, -4.43762858781832 55.81606237013209, -4.437486339387275 55.81659547957363, -4.434465162242069 55.816529518121904, -4.433774999892137 55.81774756756897, -4.433112887605133 55.81762585559375, -4.432820349042715 55.81805407822629, -4.427459788718815 55.81732351544244, -4.427440460915252 55.81762692026927, -4.425134160547353 55.81767566479396, -4.4235318052045 55.81860605648726, -4.424247507606227 55.818880201095624, -4.423657002286919 55.81959225792989, -4.423077089757453 55.81976547212311, -4.419713482474885 55.81881609293115, -4.418503754966465 55.81732993237508, -4.414273756837923 55.81836579706107, -4.412169850981781 55.81790377731308, -4.405339775718153 55.813804325180726, -4.40784544173592 55.812926339618315, -4.407724072167497 55.81113696767888, -4.417352112915453 55.80808655053513, -4.434566439516089 55.808310336206, -4.436683335597055 55.80733994016992, -4.437863787118048 55.805801242531416, -4.441067743064661 55.805182782533606, -4.442739045571951 55.803992045759856, -4.448682550734818 55.802735515570156, -4.451636539835459 55.80317844514274, -4.454600655589916 55.80143851841523, -4.455123420756858 55.80187034641351, -4.457239338424992 55.8017369530149, -4.457213050924678 55.800878031523006, -4.458300685949562 55.80074513472981, -4.460021351497883 55.801153717474804, -4.460164751625209 55.80154997520256, -4.459165179713453 55.80204042482609, -4.465802730717408 55.80466234531132, -4.468989393053299 55.80295747004312, -4.47325236518428 55.797811097663995, -4.4760090470914 55.79756668040969, -4.480599616581372 55.798255209267985, -4.485272282507235 55.79772969678856, -4.485817178801756 55.79827048954486, -4.495407111118504 55.80147478355886, -4.498622164016273 55.79611545866137, -4.499796129608997 55.796010690051084, -4.500752609399596 55.7950025353992, -4.501972994615762 55.79462715695162, -4.501276366018766 55.792969546429475, -4.499941969974697 55.79380563252234, -4.496629795716813 55.79340558883293, -4.496792263201378 55.7929528818619, -4.49506343717117 55.79186448262994, -4.49545901396441 55.79106549544413, -4.497594200416302 55.790527767907676, -4.498017552040491 55.78966529018021, -4.499914451455561 55.7886470059152, -4.501354991495884 55.788473879002645, -4.502364226285475 55.78754552762442, -4.503818340313375 55.7873361422083, -4.504081843565305 55.786719578509356, -4.507573175461506 55.78569563160643, -4.511237322286267 55.78587246546795, -4.513888477844944 55.78468574179544, -4.516647244849311 55.78220245655293, -4.517560388264839 55.78078159176873, -4.516980929525317 55.77946321275777, -4.521845222515822 55.7745278086912, -4.532139823104004 55.77663501175624, -4.530902204787924 55.77746945475533, -4.536714625638064 55.77832030496287, -4.541995549556275 55.778597644749844, -4.546726616045121 55.77533579292746, -4.546610034610257 55.77232713953742, -4.547803964074606 55.771808027860025, -4.549000496261635 55.7676665731947, -4.55048873709111 55.766530121856455, -4.558369303984363 55.766599934006734, -4.558180616607351 55.77004638477829, -4.561398706916871 55.7714623955207, -4.562746303620528 55.77158709028812, -4.564511456858399 55.7751545670988, -4.56436812430264 55.77806977208903, -4.567128129913014 55.778812089957164, -4.568685890156757 55.77800653373623, -4.572208662653474 55.77870582253596, -4.573174263134402 55.781256264127386, -4.57218163343461 55.78216689442037, -4.572607799854751 55.7833084771008, -4.569485165947223 55.786996113652535, -4.569255436314905 55.79416459891146, -4.571246569360562 55.79484201531542, -4.572950747642211 55.79455467118752, -4.575731278887397 55.79631205924028, -4.582719786180429 55.79293856149847, -4.585491170776904 55.793590358359516, -4.586901522314157 55.794648265031235, -4.592252899788947 55.796665730704255, -4.593315163427564 55.79512428369888, -4.596424832626906 55.794141802112826, -4.597884455262296 55.79449745165042, -4.600711194565329 55.79214561780563, -4.612000204779413 55.796813833440766, -4.589929501266093 55.80540645534428, -4.585047328364729 55.80639010101356, -4.584116588604987 55.806165877803856, -4.581097455263008 55.80478326287252, -4.576780045335656 55.80580382008353, -4.576506673562561 55.80654124559388, -4.573720724156628 55.80791314682949, -4.569831890854452 55.80872257304052, -4.56632640133061 55.806629644474896, -4.565272762546753 55.80735272693011, -4.562857235769317 55.80775366369709, -4.563197257352615 55.80734210063095, -4.561425757691251 55.807343080371666, -4.560454154522686 55.806886947756446, -4.560774967195108 55.80647499357665, -4.558290096836483 55.80641979921925, -4.558018485903735 55.806763766330825, -4.55226600612245 55.80736286121689, -4.55398692873234 55.80902589038246, -4.551758556349558 55.810582200069575, -4.549648015771266 55.810217955516684, -4.547309157924051 55.81095054077631, -4.547059842524308 55.8115377842191, -4.547557339431677 55.813145439705345, -4.547098355932582 55.813276461052176, -4.550772686807512 55.816004592263255, -4.549364169153401 55.81692364784015, -4.547809156186759 55.816335701807695, -4.546675303495286 55.816817597757314, -4.545292402868816 55.81568676140152, -4.53947550040486 55.81853050016944, -4.53740662715641 55.81740475862764, -4.53509260849789 55.81741655130637, -4.531936741686422 55.81503681107647, -4.524225902615957 55.815518975722384, -4.524181416366708 55.81631084751353, -4.523420842763019 55.81812409531422, -4.524621086629552 55.81890838470746, -4.525222765877152 55.81851871642972, -4.525509131293934 55.8207866447258, -4.525875382650668 55.821264480308066, -4.520042102511335 55.82512326841, -4.518896525454918 55.8239513237754, -4.520879464704993 55.82301186304854, -4.520610933378281 55.822316294060386, -4.519519408962295 55.82197914449861, -4.515523331397874 55.8233912247344, -4.515609127428858 55.82422536524183, -4.514146946173753 55.826061890423404, -4.516450480413038 55.827362979470124, -4.516312755878437 55.82795002678812, -4.519755555778104 55.82735820026485, -4.519437237112329 55.8278680596982, -4.522189151864447 55.82992384652579, -4.521874913823675 55.83049654547829, -4.519311087522148 55.831349052370314, -4.517878919510026 55.8316749990162, -4.517807261235237 55.830813606287315, -4.515159013423629 55.82912411570461, -4.515923054835956 55.828838838809155, -4.515160202743943 55.828153374010405, -4.513328270840909 55.82846947022561, -4.512369636683894 55.82895644616494, -4.51283902339648 55.829549057040026, -4.511538500792353 55.82993515603812, -4.510826115427375 55.82953625365646, -4.509620435985818 55.82965974003172, -4.50854508243161 55.82907948313104, -4.508489545372266 55.82697739914799, -4.510151819179789 55.82675471998238, -4.509673740856782 55.825700929619046, -4.51141761015269 55.82565656381791, -4.511457969710648 55.824723693914905, -4.508441021681394 55.82324832322434, -4.504370539391946 55.82351107580987, -4.504575371574645 55.82396529608853, -4.503128179476321 55.824552033704045, -4.502493401377142 55.824115552151, -4.503108942469251 55.82400415139469, -4.502945994260793 55.8232075283412, -4.502072075421895 55.823521925607885, -4.50136970236278 55.823275564255276, -4.49880375798533 55.82434340158429, -4.496543137685616 55.82394891449788, -4.493704919517119 55.82625353823175, -4.492883591517627 55.82614436240228, -4.490034575068852 55.82728168234498, -4.488737892885452 55.82732210357527, -4.487951674493453 55.82643536057119, -4.486217843318102 55.82656816241313, -4.486845210469459 55.82710254583983, -4.485712088382383 55.827862481554014, -4.484958583535188 55.82956746690685, -4.484011828392935 55.83099772176528, -4.482355148744574 55.83206480614806, -4.483289100978113 55.833187426940555, -4.480848542418249 55.83223003583043, -4.480557809034855 55.8336829748358, -4.478920250937499 55.834551887938936, -4.478843699214525 55.83636002409435, -4.478123177464807 55.837084601486765, -4.473653940148091 55.83738131805248, -4.473795256595184 55.83784585145796, -4.472036577918622 55.83806996928458, -4.470564477580261 55.836266003834304, -4.469278695286629 55.83639070011657, -4.468661719606794 55.83648398085911, -4.468928307998937 55.83691005207698, -4.470123201210668 55.83746004523749, -4.46695983588819 55.83826942151563, -4.466624821146 55.83878049718193, -4.463849819813306 55.838180357624616, -4.463758759814064 55.834721808025975, -4.464118853633694 55.834355072460234, -4.465844341367789 55.83436541996012, -4.468372561084682 55.833563791779945, -4.468704102899936 55.83184205926265))
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 lang='en'>
<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>