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/S13002939 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S13002939 geosparql: asWKT POLYGON ((-3.8365926679588984 56.0518512173266, -3.833147591170118 56.05041888102872, -3.829283278930298 56.05128047040853, -3.824820160101841 56.051061697206066, -3.8207045584698527 56.054400762199236, -3.8138017449609287 56.05346446463627, -3.810702625822698 56.05559304394344, -3.815278319126105 56.05826574652993, -3.811908400991584 56.06215959188817, -3.8021751978491354 56.06334092735942, -3.7998207367687518 56.06279575775564, -3.7982597983149877 56.06604545440202, -3.8003069296109877 56.06809137134, -3.8068212880044356 56.06974132503549, -3.807785796220378 56.07291101671977, -3.8128077039989114 56.07485031991467, -3.817856965498082 56.0757196010889, -3.823651253248444 56.078786493579734, -3.829498993549187 56.08095728025138, -3.835897139360829 56.08465357830423, -3.833335883917196 56.08750526044411, -3.8285676953476115 56.08660368701332, -3.826021950900883 56.09030951058032, -3.8231477447360254 56.09108623460134, -3.821865945465951 56.0967224778011, -3.820383839859795 56.099280254396874, -3.8215534065197145 56.10072500484623, -3.8143856568481347 56.10144073055906, -3.8121198635093343 56.10225765439047, -3.803070690396266 56.10865443928086, -3.797236553163383 56.107209143465866, -3.7947241539432777 56.10523003717078, -3.7936603754202336 56.10275461006195, -3.789809199985967 56.09869744592786, -3.7818544453911143 56.09237037880577, -3.774948676702764 56.087060218511944, -3.7718670700183257 56.08365470289173, -3.7655510706025956 56.07922512020908, -3.7562228856067588 56.074668336904644, -3.745083194659554 56.07242442960466, -3.746373249802618 56.071108592071866, -3.7401248520696657 56.06953077087725, -3.732350733999107 56.065443668324676, -3.727743538004636 56.06145282169819, -3.7245055297652985 56.055806323040855, -3.719768900761432 56.049728590326765, -3.71637809909028 56.0466666068628, -3.7078355197076958 56.04173767913721, -3.7003953873831037 56.03915996102149, -3.6929735132031065 56.037674216038546, -3.697548802046095 56.03523716318196, -3.698543322336914 56.03065857532628, -3.700526544054024 56.02860572508443, -3.705733882288268 56.026416656907784, -3.7136297530496276 56.026830215291426, -3.732109154226487 56.02348528156974, -3.7345725490240542 56.02159679621588, -3.7413488649514637 56.02199916355633, -3.7477588566930247 56.02153467058699, -3.751107087917291 56.020462673513414, -3.7554618407372846 56.0204664259009, -3.774246048502145 56.02425757761866, -3.778023852079663 56.023918291859815, -3.780562881421378 56.02654514334396, -3.783381329618699 56.02377256272985, -3.7915612501874523 56.02178942208958, -3.7995168588900405 56.020655362858896, -3.8050334035585838 56.01612472273157, -3.8078435857711037 56.01583820504498, -3.813276599689338 56.018286278319756, -3.823611098775491 56.02071212494603, -3.8258438100208783 56.023688562546745, -3.8259044933097313 56.02901747025623, -3.8281068781182954 56.03459592923295, -3.8264353087098013 56.03536383198674, -3.8273458648234735 56.04002325094278, -3.8226863959258126 56.04293276687872, -3.822962581191098 56.04432880292653, -3.8268799230005137 56.046095987136, -3.8365926679588984 56.0518512173266))
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>