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/S02000477 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S02000477 geosparql: asWKT POLYGON ((-3.884130523485015 56.04782194647939, -3.879839384924837 56.04707864272822, -3.876937991162966 56.047176739549776, -3.876787223660487 56.04770923050029, -3.875157143769055 56.047536323325374, -3.87315909180298 56.048384442220524, -3.873198094101973 56.050513635431926, -3.870989418995059 56.05066397487292, -3.868989694091999 56.0498226040491, -3.868122383981777 56.0501502682831, -3.865451678749936 56.0498611854605, -3.862543624616757 56.050674962709195, -3.854512621374679 56.04996021832362, -3.852332867659173 56.05037938052125, -3.850517273388068 56.051026690658645, -3.849991451312286 56.05044147912295, -3.842883063617465 56.05054787690843, -3.83553235844533 56.04217436352423, -3.83370952126562 56.03749721046452, -3.825758860841172 56.041335189961394, -3.822759574425917 56.040943935413964, -3.821643646479491 56.03974726768755, -3.816704469454479 56.03712422698979, -3.814750508452055 56.036191449522946, -3.816652565781145 56.03533671380371, -3.816645825444562 56.03485154543089, -3.818694248012591 56.03438103141858, -3.819329118316375 56.03491084023937, -3.821314579183305 56.03412668693226, -3.82100472234194 56.033681946721195, -3.822858455512825 56.033169303544156, -3.827661823942076 56.03190302876961, -3.825569562940293 56.027701448808386, -3.824597576251484 56.02797064609506, -3.824730934707 56.02727352508893, -3.823689718451242 56.02736743023629, -3.822608874031703 56.02617262624215, -3.824770555040183 56.02611249938093, -3.824695133215326 56.02549476149771, -3.826182661385373 56.025391865513086, -3.82597056804362 56.0242986676048, -3.827135552527234 56.023823113790755, -3.826691486798421 56.02325456595567, -3.829379079808515 56.0220644696062, -3.831394928471 56.029394370676144, -3.833252842357255 56.029654329452555, -3.832312382470088 56.02831201838108, -3.835839911906435 56.027179107627, -3.834202653361964 56.0255449872124, -3.835965154482875 56.027141539347426, -3.838550595489572 56.02631341102292, -3.837680282146438 56.025496064219965, -3.838640925078673 56.024838300483935, -3.837791957863906 56.024194951288706, -3.83846526354394 56.02384342628917, -3.83765823106403 56.02104272031651, -3.836647831617402 56.02045693590982, -3.837539732514001 56.019561740681205, -3.840902932115407 56.01921698979428, -3.837963554951909 56.018351243315166, -3.837739415612406 56.01734810914087, -3.840277398860768 56.01842457072592, -3.845200742491124 56.018377954393486, -3.848063713655534 56.015504351938276, -3.8532485628354 56.014896332452025, -3.851326758640959 56.013289679847965, -3.852325443323304 56.0120345506794, -3.851832210844339 56.01144885814261, -3.853538909874038 56.01091098554674, -3.856641742798478 56.010720515602976, -3.856985600722728 56.01120060048593, -3.859112006845427 56.01071923450958, -3.864500701801419 56.01164618113835, -3.865184565155051 56.009908599372764, -3.868697032898344 56.01030134487367, -3.8771264965744 56.0127108632794, -3.876336828694076 56.01329801715958, -3.877152383965855 56.01390567078629, -3.875837560893399 56.016235196811515, -3.878855259549523 56.01990066427259, -3.876392275929342 56.020063981552816, -3.873916943838452 56.0193108128912, -3.870277476373557 56.01936607017141, -3.868468211133657 56.02475840444275, -3.864922708770762 56.0247761315863, -3.864569410380658 56.02643497180911, -3.865884758945048 56.02908404364002, -3.864879614484363 56.02953958675201, -3.865875623058713 56.03155545124987, -3.868334591993528 56.03062856366853, -3.873788809116399 56.03355627329106, -3.872501996406577 56.033745808577635, -3.875707937103439 56.03572876940286, -3.877293491495056 56.034994720350326, -3.881900929771066 56.03665889012125, -3.883348214943059 56.03808753032458, -3.882934338475224 56.039419920490474, -3.880788513625003 56.03886854765684, -3.880518929689193 56.0425660815129, -3.881650636818552 56.04272854912294, -3.880134087635238 56.04422545611216, -3.882990926684123 56.04486484014067, -3.881674898773516 56.04586445046979, -3.883832948271095 56.0466582558965, -3.884130523485015 56.04782194647939))
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>