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/S01004123 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01004123 geosparql: asWKT POLYGON ((-3.044530497920115 55.86688582823281, -3.039510970240084 55.86561646174566, -3.038223589372447 55.865276898984014, -3.040822378230095 55.86326916603487, -3.039500612273127 55.86223802986806, -3.041895632403996 55.85859664460737, -3.039974849470704 55.858460142229596, -3.036533520564512 55.860016703856836, -3.035678773252429 55.85911637336211, -3.03214423194013 55.860781420632875, -3.028418888794818 55.8582607549403, -3.025306502544452 55.855339512632895, -3.026321185201213 55.85444149577853, -3.019403245022501 55.85013209542402, -3.016846524916582 55.8509957565724, -3.012295988727776 55.8470818268837, -3.019093516456052 55.843790950166486, -3.017398259001149 55.842475138768194, -3.017722042193645 55.84203217427172, -3.015568797031163 55.84151982955024, -3.016671402038982 55.84032464447713, -3.014853342700448 55.83918952191187, -3.018967462304642 55.836531761821114, -3.016866868570649 55.83557871789094, -3.017757290051421 55.83482556173685, -3.012967487826823 55.831145129180555, -3.016057530058255 55.82903501708495, -3.019612442918565 55.82762183757859, -3.017948392463046 55.825029839981305, -3.016893699901356 55.82498465256728, -3.015625495953063 55.82409658675382, -3.011028287784182 55.8253205456724, -3.011745447194435 55.827120723130044, -3.008871381123657 55.82771041594407, -3.00839505069311 55.82693255911452, -3.006825410996818 55.826991167449975, -3.006546308914091 55.826673212619795, -3.00845815001677 55.826230566017415, -3.006914633951483 55.82415403129205, -3.0088099525289 55.823487759893, -3.006380995024121 55.82276186852809, -3.005566834515485 55.821519551080634, -3.003367011189585 55.822247369350244, -3.000492019303518 55.81968298441198, -2.999968157138836 55.82041506643929, -2.995027493597062 55.818190810762026, -2.992990554511261 55.82030088335382, -2.989841916601568 55.81635470395566, -2.977373012440219 55.804054538163335, -2.976380465670043 55.80328069070954, -2.980088189855642 55.800366859586155, -3.000663991223801 55.7896970767473, -3.01024933332768 55.78787548616096, -3.013302957755124 55.79185788507884, -3.009871416239658 55.79299133989891, -3.01059454876827 55.794432061661325, -3.021531661309556 55.80339904438723, -3.023873741726657 55.80510477679678, -3.024744892434684 55.80486389628095, -3.0273738531221 55.809577310681306, -3.033222232333905 55.80809062564616, -3.039255380803456 55.80816557617576, -3.04009216832203 55.80903009429273, -3.039466771696723 55.80951161456671, -3.040544562772942 55.81043699044007, -3.047490387973149 55.808239463162124, -3.049627708934026 55.80939833820358, -3.058943731998295 55.80696425095935, -3.064098333163614 55.810513959523504, -3.060748837941193 55.81408319568773, -3.052082350195177 55.81462489784997, -3.053115573323059 55.81742851681721, -3.048828827392618 55.818848920577736, -3.053116138239005 55.822765899318675, -3.059607520429005 55.822584342867714, -3.062341991663147 55.82511263524986, -3.061859021815619 55.82554811035992, -3.062848928156785 55.82614158809499, -3.059072340522836 55.83049619092921, -3.060490036026545 55.83155324883126, -3.055238604597187 55.832182472528665, -3.048231889347865 55.83463252372014, -3.045834022740395 55.83571322115279, -3.044501200723089 55.83483498488122, -3.043015613537157 55.8354046903858, -3.044345701165197 55.8371728299332, -3.043909725925301 55.83778723962366, -3.04508347534591 55.838684804834294, -3.046512989477737 55.83899612796101, -3.046648429895654 55.83928251052741, -3.044918712641539 55.83909055050691, -3.04245782289002 55.840216642340934, -3.040447561465065 55.839685549139, -3.041062393000905 55.8411809187823, -3.037943786550689 55.84220465792609, -3.040920150959729 55.84362616654331, -3.039591076837766 55.84426140395896, -3.042085154259532 55.845386437567655, -3.041808081273853 55.845775158790154, -3.040871538996436 55.84718481672936, -3.045812703227573 55.84919159401313, -3.047479016656513 55.848791043169456, -3.049423684112407 55.85133747559853, -3.053553140272304 55.853097122283934, -3.052182726609708 55.85440275494311, -3.04958556556226 55.85524261413754, -3.054989387968973 55.85540306204876, -3.055892899428895 55.856320813039765, -3.055662063646783 55.85744338830165, -3.053892731725402 55.857380274147786, -3.053627443685872 55.85879326139458, -3.055964062689165 55.85894395378313, -3.056582970483491 55.859378927640684, -3.054356632327867 55.862120614971985, -3.052075013650105 55.86460215439718, -3.049656794549016 55.863773878489795, -3.047782131381866 55.86536249765641, -3.050509112679054 55.86696971612701, -3.049870098802161 55.867568208039685, -3.049506992166939 55.86714000477077, -3.048906693597886 55.86798975337649, -3.044530497920115 55.86688582823281))
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>