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/S22000066 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S22000066 geosparql: asWKT POLYGON ((-3.8350496955975077 58.28740952461282, -3.8502189785912226 58.27703947165271, -3.827624647435301 58.26945017603032, -3.8207179794547055 58.245167633837184, -3.7766885708757476 58.23044407212384, -3.7641364823413586 58.20401821488275, -3.7784157870935404 58.19553726138946, -3.7553863014753732 58.19257719653495, -3.7256381898598714 58.19972051237552, -3.7123408179893764 58.186469528697266, -3.6893828100885084 58.18333498188978, -3.682719442493792 58.17153813645936, -3.634640742151048 58.16565955554933, -3.6546176968183763 58.15218754767811, -3.64168766499698 58.145940675961974, -3.653441226404288 58.13634734568864, -3.6264991731171454 58.12300714939914, -3.745361584897441 58.06819909699183, -3.749186279453704 58.06747659740834, -3.804270467121034 58.05705546272859, -3.8333054891012055 58.03889316905301, -3.8333792189637483 58.038725364767714, -3.847757993939091 58.00653503202489, -3.8912603716360468 57.988085184745955, -3.9607523286892525 57.97410402097446, -3.9662987736211464 57.97298603459866, -3.9832754253825033 57.96956215346799, -4.007582912733522 57.93348820666418, -4.029906629791477 57.93443612834452, -4.029377441858985 57.93491454480651, -4.008665909945223 57.95362689029785, -4.02903893607757 57.94854293511382, -4.045989403599599 57.94430929354803, -4.046841203377703 57.944096466366126, -4.0469565914704 57.94425507075738, -4.054516297396293 57.95464229264223, -4.081968671245361 57.95128640925681, -4.068985038356922 57.93679556819423, -4.035231271925996 57.92746681844715, -4.033273588713073 57.92692536712809, -4.027814065943051 57.92683401560199, -4.00137056257821 57.92638813106847, -3.992209696593464 57.90307907475615, -4.013100988305929 57.89074639171024, -4.0130980722528795 57.890668428112335, -4.01191991711627 57.859137661422395, -4.02423761766257 57.86330648176525, -4.036740712900642 57.86753591497192, -4.055015041442343 57.867356477387546, -4.072850462262749 57.86717239743775, -4.111688394126811 57.848498817046845, -4.1208793292000525 57.85554463722089, -4.121087185094705 57.855703941796634, -4.146908250088024 57.855305449948894, -4.1461615992559295 57.86079856852, -4.160957611097461 57.860393434645026, -4.164801681809176 57.8602878963532, -4.173521153949579 57.86004807209862, -4.193861332859575 57.862715945206254, -4.23249446939925 57.875132867446034, -4.29075511133895 57.861999483335936, -4.361259181703229 57.88118895274956, -4.372448046393637 57.86457478108433, -4.381878706838027 57.865165938953766, -4.372865334425487 57.87401617662742, -4.390291786078878 57.87455359610111, -4.440666411329396 57.84865966976131, -4.52860123880937 57.84369805172444, -4.550754286568576 57.81630025976679, -4.675849427635452 57.78996085757621, -4.68633990812222 57.80160070310106, -4.708408153779641 57.808796182908395, -4.7430063273554754 57.80791709145164, -4.749480299582666 57.81621506366441, -4.819380068688917 57.809727461645124, -4.872026525922877 57.81730724417799, -4.8920973440293105 57.826089313870845, -4.886686425173298 57.833411191365826, -4.9485590081255735 57.85859760545651, -4.924271114336451 57.860984474090095, -4.899701736016329 57.88064588239374, -4.9273432168965545 57.89027405614899, -4.871508937284256 57.90683661987024, -4.902910274735387 57.9189554221546, -4.860163991835027 57.94397423499295, -4.912770183704143 57.94516890352215, -4.927531576115645 57.95913657857275, -4.906475227705607 57.97700966723449, -4.9504542885744005 57.97923125739376, -4.938889712597304 57.99355485000608, -4.947432458195964 58.033368500706985, -4.931249384609264 58.05757443536366, -4.90974114745018 58.06203135829371, -4.8906644054690345 58.085912797965605, -4.9020056432932915 58.10352959297107, -4.88184202871785 58.14937956711867, -4.906078663055032 58.17538513933121, -4.883171247446407 58.176291652366594, -4.87926904970408 58.18440491524508, -4.847417822073203 58.17589993089101, -4.834572676330979 58.21642741469927, -4.802726225382985 58.24231069341313, -4.780816685811931 58.23890787259096, -4.76340062065111 58.21349572063596, -4.7110572122957945 58.20634262979651, -4.702508930117516 58.210343065165986, -4.726257837464809 58.23960868551165, -4.710846186626453 58.25736080845983, -4.687645944318142 58.261670279561315, -4.667041613658462 58.27948088815442, -4.639372670687669 58.27788095249004, -4.627423359009091 58.25865516778111, -4.602615209248648 58.2435100844243, -4.618859284920745 58.22924104951549, -4.605292454074685 58.20730606254171, -4.500659271557123 58.197521114244985, -4.4314001667094365 58.21401764586444, -4.432887642651965 58.196450482959186, -4.414652445641927 58.180767636162614, -4.317096703240355 58.17388013073904, -4.312327065722486 58.19339178889902, -4.282410199334155 58.201549661010354, -4.259155572407255 58.22769546454984, -4.157614765079762 58.22852713874, -4.125596078006438 58.238776355463585, -4.1162905493626285 58.23499588872581, -4.121377776066038 58.21350205600463, -4.033080019514671 58.207329840649294, -4.010265370839387 58.216836957808596, -4.02744863177724 58.22647446866652, -4.023863990074438 58.2612992107792, -3.969070437768986 58.25318985440473, -3.944177872445333 58.25827870797583, -3.927487023500011 58.283928534426884, -3.942816409330659 58.31993653173124, -4.007522599465092 58.3412077847897, -3.9792559424234337 58.34288803532966, -3.9677905848872568 58.36497604656654, -3.9085555417769595 58.344761903446866, -3.9037050070410415 58.334323496003435, -3.89108095975758 58.33845451798914, -3.854991689554484 58.32294759955467, -3.837762904919236 58.31176951461267, -3.849681661533155 58.30171586852194, -3.8350496955975077 58.28740952461282))
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>