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/S01004011 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01004011 geosparql: asWKT POLYGON ((-4.631731850316545 55.891713772433626, -4.629300044809976 55.892414055754614, -4.625888037809487 55.88618609116443, -4.623493532970571 55.88719892522683, -4.622125550843177 55.887308982009, -4.619575935666666 55.885657108255174, -4.620155110247943 55.885316934124866, -4.619037506669178 55.884678256879546, -4.619120128149841 55.886141595451505, -4.615116471215725 55.88582228445327, -4.615283610019222 55.88521651451796, -4.613844977651439 55.88547937977143, -4.613650199247468 55.888100123888066, -4.611661669032292 55.8872439142428, -4.609883813727509 55.8850615286293, -4.606140057422775 55.88479933202142, -4.603974969719691 55.88399128733878, -4.604818609406632 55.881546589733404, -4.599593688369629 55.87945497498271, -4.601748295778919 55.876766870356604, -4.60525766357907 55.875452214688515, -4.600304698664244 55.87191675082108, -4.608684069490879 55.87028322800851, -4.610427373631657 55.86910472758488, -4.606379674167516 55.86643114862192, -4.598925913375018 55.86396428343509, -4.59760281504466 55.862581070698724, -4.598703019796825 55.86206347343076, -4.600632518599906 55.86271479009192, -4.608761706504517 55.8630999122916, -4.611432207513483 55.8628814513182, -4.612461446072273 55.86202367886985, -4.617445505592849 55.86222331686075, -4.623255271943218 55.86115084199065, -4.625626675201414 55.85855319697019, -4.624610112394873 55.85760529775968, -4.625079234005947 55.855975495403044, -4.62213496706029 55.854221839871286, -4.622388845016323 55.85217326036059, -4.62366516792191 55.85172711460812, -4.623312922065463 55.84969886294052, -4.625211325515308 55.847923735438066, -4.629017224756452 55.846935419726385, -4.632951176970984 55.84100330759898, -4.638712665587011 55.84069104004688, -4.640628595166692 55.83949938770374, -4.642966090004498 55.839107602743134, -4.648292051944183 55.83944242374713, -4.653164055973354 55.840146350512, -4.678443849140042 55.838187041536486, -4.685820381351895 55.84092043752925, -4.695738950533331 55.841359538462584, -4.699238370710372 55.84206477142959, -4.699860133053005 55.84112529471176, -4.700846174052647 55.841292399205315, -4.701164519871787 55.842426957938436, -4.702781777060193 55.84248130399082, -4.703049083658568 55.84287991492355, -4.702151838294003 55.84376254240491, -4.704046609094306 55.845051200792284, -4.702982453599836 55.845371222414286, -4.704002699983278 55.84694876153735, -4.706373389622918 55.84680671966917, -4.713199474197909 55.84823828619069, -4.716092047653154 55.849163176764165, -4.717191782302078 55.85095458457826, -4.721697404544259 55.85232009123922, -4.729284349120645 55.85318563092538, -4.731801624917929 55.85285108647603, -4.73222486823011 55.85341695834402, -4.736679982040014 55.8542796809757, -4.724425237998473 55.85613379352563, -4.724783958332648 55.85669213069634, -4.722775213409429 55.85744672432853, -4.721852757668055 55.85705366686039, -4.721036565982204 55.857494194697594, -4.718963449607107 55.85755802433384, -4.715205593734901 55.859357876031595, -4.703659639598529 55.857419029109316, -4.693513109459569 55.856518106690125, -4.683220659061702 55.85720151792619, -4.673463837903641 55.860335270465974, -4.67624887744931 55.86248587140559, -4.676881124254091 55.86679558408451, -4.673970816505485 55.86724539391466, -4.673654547690781 55.86869042825502, -4.671368907795191 55.870789464386604, -4.673504214587721 55.870689141166764, -4.673675361064682 55.872015717791314, -4.672770390602147 55.87210729277764, -4.673084577384324 55.87342177318637, -4.675472879607587 55.873046264599886, -4.678661445135797 55.87454087016161, -4.685399715789812 55.87371981499221, -4.686912348576226 55.87686872036583, -4.691922048058166 55.88017483722281, -4.680360360352835 55.88712371423133, -4.678829039465096 55.88695033051467, -4.679311070841374 55.88791059076391, -4.678064248044048 55.891865702780244, -4.664882351193778 55.88857450656358, -4.66425851691042 55.890439636324786, -4.661430389518963 55.8897817683775, -4.66173507643054 55.89002685357717, -4.659379379769935 55.88996094290174, -4.659575941751083 55.88956120325122, -4.656691419934525 55.88924600704785, -4.656712364975529 55.88838266603716, -4.653646905178021 55.887055615329075, -4.655654292008745 55.88577190024804, -4.655089267255434 55.88546051181395, -4.651401837214546 55.88462321514607, -4.649161806301269 55.8850759476748, -4.64834018626028 55.88451837842522, -4.645578731883855 55.88482946551624, -4.643384112917077 55.887110525382965, -4.638893182707 55.88961122305721, -4.63852101019625 55.88938946631517, -4.634015481161286 55.890479577268096, -4.632930224979151 55.88933431848447, -4.631448953050326 55.89060640044913, -4.631731850316545 55.891713772433626))
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>