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/S01000904 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01000904 geosparql: asWKT POLYGON ((-4.419439779373008 54.84061294877737, -4.419160695143054 54.839333073292494, -4.417798644343804 54.83817366077716, -4.418060347669099 54.837341449683194, -4.417211736267991 54.83669316623711, -4.417078103743404 54.83523959494276, -4.414809279770545 54.834017308603585, -4.412928404785624 54.83198723713644, -4.413029962196322 54.828470486134755, -4.412273386967533 54.82701132058407, -4.407047577905901 54.82422961560266, -4.398758156301912 54.82411513856376, -4.394894030251709 54.8222228746143, -4.392118483367779 54.82162143409748, -4.390192812388115 54.82196501567821, -4.385984280547468 54.821877066134405, -4.385720320020356 54.821360893948146, -4.383869255854465 54.820857939261856, -4.382505660323049 54.820965627547885, -4.379234028753427 54.819600578258516, -4.375048949537308 54.81937695841255, -4.368013587098707 54.815136844717344, -4.361423710504607 54.81557093035595, -4.358278329964966 54.813681506055346, -4.352931641139548 54.81247294129758, -4.352086757247635 54.81079041074693, -4.350116493471162 54.810630840267095, -4.348574483522171 54.80792805796246, -4.346400202908275 54.807736424437365, -4.34649317525717 54.80665594956383, -4.34270097904726 54.801632506219626, -4.342983000620453 54.799784320632845, -4.34188256447684 54.79932016835664, -4.343088740594473 54.794352936990656, -4.345551570785635 54.791213119090486, -4.348266561493995 54.79081899523793, -4.351848716062571 54.79089340305295, -4.354538659607851 54.79140751428305, -4.354729799918658 54.79255439594105, -4.355857483724891 54.79268530818326, -4.357339096520708 54.79370779146067, -4.360322844970403 54.794297406118744, -4.360766469419401 54.795502289443725, -4.362458250581946 54.795627561836454, -4.364383644619153 54.79559004078157, -4.366884071180776 54.7947628092079, -4.368468460856235 54.79392286357804, -4.36945104790191 54.79263621682054, -4.368471670126534 54.79211600576327, -4.368881711358418 54.79061581755673, -4.367808000815729 54.78927944086251, -4.363306113511132 54.78916949801265, -4.362796503100748 54.788433340756605, -4.36370879760667 54.787273954268045, -4.361503831530224 54.785204491481274, -4.362315023349614 54.783912249801894, -4.361467240991646 54.78217590047655, -4.359698406394412 54.78144626829573, -4.358754522140486 54.778857813226296, -4.359299680481811 54.77780447915068, -4.362697788596415 54.77767538759926, -4.366659415240647 54.775746383345115, -4.370197714125428 54.77509297935635, -4.371905875025548 54.77345952085695, -4.371623037379906 54.77208073824966, -4.36595627496571 54.769296975439055, -4.362902112767646 54.768646402271806, -4.360723926971611 54.7688865949542, -4.355750340122214 54.768111401289225, -4.355129061583995 54.76678410322903, -4.356553021614033 54.76453614110522, -4.37474620924291 54.76753405528103, -4.378630261289344 54.770667016802044, -4.380683712075834 54.77095031967537, -4.381819542925273 54.7728157210088, -4.383922689953144 54.77076981747244, -4.386673515994268 54.77022129662263, -4.392797158639776 54.77097253717915, -4.401153615357892 54.76767910617714, -4.402322442230275 54.76798855092049, -4.408895766106362 54.76375007265415, -4.41048741488391 54.76149811307809, -4.412122996019114 54.76181615520822, -4.416635828405878 54.762679129134455, -4.417493570591561 54.76559249827527, -4.420102963930976 54.7678956017696, -4.411129360971715 54.77229031698562, -4.419449593997686 54.77518088408428, -4.421410205178177 54.774161924152736, -4.422722535771438 54.77192436893158, -4.426902947039216 54.770069890389735, -4.438039288340872 54.77323538537361, -4.437228740074346 54.77560717131073, -4.440834566916163 54.77652880640526, -4.434985257621177 54.779598192854465, -4.433681341549765 54.779120976398296, -4.42945948103757 54.78055401904632, -4.425828093581424 54.78406058983829, -4.428075096946493 54.78522016535105, -4.429365392147394 54.78961699655743, -4.428481302596444 54.7912695380072, -4.4264067544936 54.79112348647135, -4.426219131749524 54.79292507305508, -4.425204901405255 54.79313413415755, -4.424452332272266 54.79433575052103, -4.430390846351056 54.79581695592013, -4.428285107529061 54.79624567106176, -4.428641678920746 54.797002609878334, -4.426804283110111 54.7972371530363, -4.427080045674571 54.79846314841842, -4.425379980147466 54.79969271588816, -4.426147663357118 54.800818983875544, -4.427712023372516 54.80095847528781, -4.425286672585552 54.801771075605146, -4.42591571270659 54.80343946722463, -4.429528267984185 54.802701958795645, -4.43076445645103 54.803594085164505, -4.432558081548249 54.803144622807054, -4.433166938132831 54.804471794504344, -4.434535647212999 54.80420162346103, -4.435465747752084 54.80465937567346, -4.437064207269671 54.804069938662145, -4.436706197236968 54.803034387669776, -4.438790156937121 54.8024980988875, -4.440551408518934 54.80280425911783, -4.441029520738781 54.80376546918545, -4.448037803585292 54.80746260409385, -4.446751881048614 54.80807284830946, -4.446404928995846 54.80928439870847, -4.448140871705934 54.80968085522571, -4.447006590443805 54.81099818636912, -4.447725737102382 54.8120803565151, -4.456587724648233 54.80986964430783, -4.456461526383672 54.814969078332254, -4.457215089181021 54.816356131770064, -4.459992467689574 54.815185178105025, -4.460396192465883 54.816192772714295, -4.465758052268798 54.816821016296515, -4.471718724487597 54.81807506022945, -4.472090757996448 54.8188135865866, -4.476041426406939 54.81953930783751, -4.475019209279716 54.81884376687347, -4.476300405758786 54.81764901754299, -4.479734671394054 54.81653610971062, -4.482079987570578 54.81627787573964, -4.483349261310134 54.817409332575195, -4.484093389090956 54.81938641821936, -4.482649956616974 54.822948744776646, -4.483512322685686 54.82304795311439, -4.483204972010296 54.823620568620115, -4.488855538328657 54.82335195756198, -4.488097952151783 54.82040103866267, -4.489545353581065 54.82115341096769, -4.491858899831678 54.82118599392531, -4.49307300511099 54.82180909578993, -4.495120493979228 54.81790160877636, -4.49726775700287 54.81687759090938, -4.495894189296913 54.81631256642441, -4.499259516285413 54.81484993342888, -4.499971922265243 54.81328010442682, -4.49859355455274 54.811375803920846, -4.500986876965821 54.81031068859989, -4.502843856588679 54.811396048223585, -4.502522991218367 54.81249936559671, -4.512149201198386 54.814871329355086, -4.511693543172754 54.815554965856634, -4.514105623578634 54.81629605808827, -4.512282387430694 54.81676532048365, -4.511402810298673 54.816388003643674, -4.508909964370737 54.817104799600656, -4.507316591033385 54.81827039759223, -4.508843852749746 54.820054663475666, -4.508539750276816 54.82117562752685, -4.513810706170171 54.82156991828249, -4.514445953863944 54.81976787552469, -4.516374405298021 54.81973687681249, -4.516410517963305 54.82131825044399, -4.517837794340439 54.821738102763675, -4.518759641361222 54.820541361750614, -4.521870687521564 54.82124980915639, -4.524528677542343 54.818938197803305, -4.526282593709793 54.81960285286625, -4.526824194752332 54.81905221091838, -4.528629509558251 54.81929326263644, -4.529051644812961 54.82031824023394, -4.534385670254128 54.821717132550766, -4.534243017345218 54.82316740091521, -4.537925551767956 54.82404328881789, -4.538437811336395 54.82674736442967, -4.535769215580777 54.82863700437921, -4.534945425721949 54.83114427434001, -4.532440885304033 54.83242811956982, -4.529277605894971 54.83239522454862, -4.528109171214145 54.831889204862016, -4.522247319857441 54.83128307393645, -4.521796215915589 54.833288082045584, -4.52361361924244 54.83620778348547, -4.523874116760758 54.83813507214558, -4.521318448536517 54.837612888567364, -4.518823131735512 54.840307575506834, -4.520116822399428 54.841071773347046, -4.517098641117465 54.8443795601813, -4.512649480328438 54.84497516948383, -4.512442646047236 54.846408750803796, -4.510190720866351 54.84676997789497, -4.507455027179074 54.848624392845814, -4.506019664627591 54.84808772015883, -4.503886139715375 54.84357419776784, -4.502382492268818 54.8376543131684, -4.496663879550812 54.83836540944782, -4.492783474447654 54.83580234855139, -4.487087167594908 54.833330341140844, -4.484850525036932 54.834185201433215, -4.483744137976489 54.83518768239006, -4.480768941348378 54.835176635782624, -4.481799424284096 54.83623427046408, -4.480589791077223 54.83682532811343, -4.481560416780969 54.83766844708571, -4.479560586766969 54.83756550316285, -4.477132083558495 54.83809153499994, -4.4740733593332 54.837228050768985, -4.474024073501542 54.8361683290315, -4.470823378878184 54.83527170356686, -4.468210913650942 54.83661033154853, -4.463390702731471 54.83724763981383, -4.457482288405381 54.83612694101914, -4.454385220202693 54.84103479155169, -4.447242027199525 54.842419568300485, -4.446217500287234 54.8432492483625, -4.444481747211003 54.84287971529923, -4.443559690307605 54.84154092599253, -4.4403938620575 54.84199116875787, -4.44023259405006 54.840610084317476, -4.431222873050885 54.84256179310767, -4.431440352999562 54.84177538184742, -4.424956840634791 54.84196804499635, -4.424706882952357 54.83987857771166, -4.419439779373008 54.84061294877737))
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>