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/S01000913 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01000913 geosparql: asWKT MULTIPOLYGON (((-4.139015189424912 54.90530237939646, -4.137136347788355 54.904877043483275, -4.137374153373921 54.90436052019978, -4.135575560835376 54.903682072866154, -4.136016445348856 54.9031709688167, -4.133081891516532 54.89988784333296, -4.132089793651594 54.896462701486975, -4.130709816521282 54.895138659735586, -4.130122839279194 54.89258725999217, -4.128850276875895 54.89152197520985, -4.130032088022315 54.88998221019829, -4.129097600282846 54.88882111452257, -4.130359720360437 54.88791811475723, -4.128245116926245 54.88655300110453, -4.128043354564058 54.8853880417555, -4.13250620613277 54.884572697099905, -4.132269860440372 54.8833454335874, -4.133146700379148 54.88282668046163, -4.132109117205869 54.8823685154137, -4.132955822795959 54.8821648886496, -4.132866386268723 54.881357501526885, -4.135119397790556 54.880625792965475, -4.13558966607116 54.87979058598522, -4.135038748940708 54.879395795650844, -4.135418306995718 54.8783194952522, -4.136082294407214 54.87819995419793, -4.135798054102777 54.87783642918086, -4.132044939494675 54.877021525240856, -4.134610136263505 54.87335410860237, -4.13629585952779 54.87369298505586, -4.137394151969346 54.87176810238111, -4.14069196007818 54.87252794875507, -4.140826254707785 54.87212110087199, -4.141284979233142 54.87224783623874, -4.143686193628398 54.869329151405, -4.143651393158317 54.86867360835566, -4.146444325944705 54.86580187815562, -4.146148927137348 54.86493521631315, -4.149081939209233 54.8626541828712, -4.146671906324248 54.86070138207559, -4.143824181337254 54.859304563477, -4.139387736248443 54.855266110652416, -4.139452945646299 54.85443802098605, -4.141573924933843 54.854184894159914, -4.14376698778316 54.85117997892349, -4.146709417399046 54.85026507672573, -4.148558850133953 54.84696053516321, -4.15262002335575 54.8474638379065, -4.15321743074333 54.846104961896, -4.155619183609776 54.845576948039096, -4.157231755062333 54.84427192371065, -4.163638826502895 54.846701626160545, -4.167544644002987 54.84140059541939, -4.169664136357509 54.84142564215754, -4.171219607416664 54.84254835909723, -4.179647825348921 54.837965909033535, -4.180601916006385 54.83948581742409, -4.183400152649866 54.8388152897528, -4.187777031362725 54.83594999661405, -4.192815502507747 54.83409728621052, -4.196365889662959 54.83180389746635, -4.197563618886722 54.83378667651413, -4.198792162696614 54.83404306703957, -4.202500587262531 54.83294212222484, -4.203711761950131 54.83487965427867, -4.20951110900744 54.833003433029276, -4.215306395740058 54.829687001968914, -4.217084577155783 54.82925630275486, -4.216509536771349 54.83028706285846, -4.217149950450055 54.8306079434515, -4.218116458759511 54.830405983697396, -4.218153562832751 54.83102105722101, -4.216633189768 54.83339488158389, -4.214402078845979 54.83529307882205, -4.216333473017122 54.83588122693032, -4.216673365300736 54.83668399890343, -4.218761503917563 54.836708758592856, -4.217003337380769 54.83787346265072, -4.216665303033087 54.83909310664483, -4.220016079843789 54.8382857948749, -4.221669662637087 54.83833642985713, -4.220780808755643 54.83944931761619, -4.221667120165704 54.83999039114786, -4.223446972454761 54.83922072364188, -4.224851684675151 54.83984216254023, -4.225401958391069 54.840784871235925, -4.224861867516398 54.84144195852622, -4.218881185308369 54.84399637162259, -4.219284235357057 54.84564264557322, -4.22045601615836 54.846861916399256, -4.223243453011126 54.84797941090736, -4.222932951910749 54.84941429588246, -4.21390208284086 54.85517928459417, -4.213724213838338 54.85619824393576, -4.212488213729827 54.8569578481905, -4.212767119190587 54.858067353625586, -4.207445155748923 54.86132824645981, -4.202121158728714 54.8676907359045, -4.199782424125284 54.86889918626641, -4.197459878234187 54.871351388988614, -4.193702746692919 54.87357768163526, -4.192217347214254 54.87479159130443, -4.191153390621198 54.87640062413295, -4.188797489052623 54.87636222035405, -4.187400301646854 54.876755933109315, -4.186380206525861 54.878356291402106, -4.184665570797853 54.8792230892013, -4.186322689257101 54.88103591222537, -4.185436289561067 54.88280463504986, -4.185050612839666 54.88575981568988, -4.185799240031927 54.886339584240936, -4.189099582674075 54.88653180902365, -4.192543783398295 54.8885100684405, -4.189942391569526 54.88970754447819, -4.183786518568528 54.890636361324866, -4.183064198701354 54.889390955622005, -4.17205946890315 54.8914938588386, -4.173140987107604 54.892175611575404, -4.173279731736615 54.89359331258037, -4.172156803471709 54.89417068939095, -4.174662164528596 54.89754148047892, -4.173849034378251 54.89750210811984, -4.173155904327913 54.898242581139684, -4.173563734310081 54.897417328794724, -4.172459191656991 54.8980483099573, -4.172346356662747 54.89769078865358, -4.169303413529317 54.89708003412621, -4.166621377127896 54.89737959008982, -4.163189803290219 54.89708122330549, -4.160431832484336 54.89800219897297, -4.158406441895075 54.90183134396832, -4.157453470558674 54.90180333208512, -4.156349033980314 54.90244315129937, -4.155797819359026 54.90176082393969, -4.153440213580041 54.90200938433295, -4.153274555923909 54.901535933663084, -4.151361450084337 54.90134512675018, -4.148829483649887 54.902127002205596, -4.148629704250588 54.90276871583995, -4.146982146441631 54.90287874742737, -4.14587530723188 54.90377018538092, -4.144142791444608 54.9040434715991, -4.144347945282177 54.90438141086624, -4.14256529804855 54.904592639429794, -4.141178265075063 54.9054979638715, -4.139015189424912 54.90530237939646)), ((-4.223683827136322 54.816625399167414, -4.224541002763857 54.81531971596808, -4.225323813164827 54.81591078354662, -4.227000832273962 54.81608640088028, -4.228764665723315 54.81718918392203, -4.228148988328999 54.81876909528057, -4.226922680776497 54.81982195015893, -4.22304008130678 54.82208969525402, -4.222180599119403 54.82170391374461, -4.222042574690521 54.82013665759918, -4.220824416872973 54.81920954965003, -4.223683827136322 54.816625399167414)))
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>