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/S13002848 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S13002848 geosparql: asWKT POLYGON ((-2.8014522995600126 57.696062007459744, -2.7977711678057773 57.6973383390923, -2.7949425461794704 57.70016589552372, -2.7921206321271685 57.7010971255518, -2.7885274007806675 57.698506560502565, -2.779742538803061 57.69498048247808, -2.7768539594428376 57.69310741770416, -2.7743278510017215 57.69413611403972, -2.7709672670708883 57.69269073658043, -2.76493695302718 57.693648949296346, -2.75801863952203 57.692191370052406, -2.755682506933461 57.6906028631229, -2.7494092086186095 57.69149704165562, -2.7471417809170684 57.68964485713362, -2.746920680397384 57.68509734883161, -2.741558694068697 57.68310785167355, -2.73258265308778 57.68490450385629, -2.732305976125089 57.686481643026355, -2.7271635177533162 57.68713738445522, -2.7234701639369097 57.689404177279926, -2.721428660065758 57.68930627676163, -2.7167656213294653 57.692271828168906, -2.7101997934905206 57.690274350035715, -2.7060151802515593 57.68710269979972, -2.7018824050965873 57.68631632941298, -2.6983435457340716 57.68718119386464, -2.694316954255475 57.68491822595326, -2.6853995831440947 57.6868549687348, -2.685078610569545 57.683887041102345, -2.6761533551343706 57.68613307194009, -2.670474325463996 57.69002663681745, -2.666897926296053 57.69073726034832, -2.6607988689343993 57.68925030536589, -2.660647776073425 57.686845553377985, -2.6548362058502195 57.68683355934907, -2.6476950986082612 57.68816468013114, -2.644883888394947 57.682774301736856, -2.6400823036698657 57.68265850824628, -2.6371479292313995 57.68114996875309, -2.6308629549937614 57.68114814907655, -2.6264858778068247 57.683070676068056, -2.6204032058981537 57.681778360041086, -2.615241888964817 57.682091909307225, -2.6122632414793534 57.6798778195699, -2.609700260471379 57.68173071640041, -2.60289030825085 57.68142651641351, -2.597425713833844 57.68288419214458, -2.592861177123147 57.67930015362274, -2.5891389660750233 57.67890796995864, -2.586791941281511 57.68039283582028, -2.581792165106903 57.67896352340518, -2.5792784222009435 57.68146499694503, -2.5753997636261206 57.68358452435937, -2.5696968760141594 57.681808525011995, -2.5662372062396477 57.678463702541045, -2.5669246861105828 57.67610628287568, -2.5615238585822695 57.67358475997286, -2.55968163096208 57.67404119937261, -2.5495600079402774 57.67137195488661, -2.5438239712644153 57.67175603064104, -2.539691897288067 57.67020360143599, -2.5309112120401194 57.670557841888844, -2.525274643448515 57.67144909815538, -2.5208080330333424 57.669536362605925, -2.5194332994381585 57.66764942394836, -2.5126612941935913 57.66695031586408, -2.5141673900948254 57.66487560379554, -2.512752854588733 57.661443770241625, -2.514123921876661 57.65789915222635, -2.5250540963927444 57.65248544626722, -2.5305809883073964 57.65173922531491, -2.5345727293192017 57.653604235509505, -2.537716528003479 57.65261003357316, -2.5370125384720317 57.648620299068895, -2.5388765841810708 57.6456410123828, -2.5412646882850867 57.644234886675456, -2.5384153596033587 57.642217057993676, -2.536154049022235 57.6376752959673, -2.5273595465601644 57.63711853980143, -2.5210370459148277 57.6354856316686, -2.5175992037120793 57.63339702292699, -2.5090610421252073 57.63048180186003, -2.508730298833445 57.62934151944725, -2.5114508335116206 57.62573840022064, -2.5129958675784887 57.62167474675848, -2.5194580574316263 57.61769072084673, -2.5145700261118114 57.61389757556467, -2.5168363391628494 57.61118644627978, -2.5153831937070215 57.60867728375345, -2.516109446827561 57.60465103947078, -2.5206569866151773 57.60133674217626, -2.519287261033761 57.59856407408036, -2.5159125236202757 57.59938088847997, -2.5130830703753793 57.59817886153245, -2.512808715192683 57.59591729862372, -2.516569069640049 57.593880052542524, -2.516668996029829 57.58666038622226, -2.511254598998346 57.58365527385983, -2.5082910145530875 57.57968794387506, -2.509762468517544 57.57740496236882, -2.5088620567861435 57.574235070940055, -2.4995935229614563 57.568560098036116, -2.49802307157382 57.56325401483088, -2.496812902734428 57.56189615953415, -2.5026388504344834 57.55737636819518, -2.5031327081162957 57.55501023746715, -2.4985979119872663 57.553096003029346, -2.493818480594837 57.553429180278755, -2.4882251724292046 57.55127544339272, -2.491010906696931 57.54956149165283, -2.486807162121432 57.548190903121174, -2.4830333886483373 57.54406355573102, -2.4784122941167945 57.54213725522792, -2.4790482604670063 57.54044166330218, -2.4835094669515025 57.53939976248217, -2.4868783615098855 57.54000301847051, -2.493628813797623 57.53756943273249, -2.5046440994114163 57.5376650570449, -2.512120815728895 57.53556180892229, -2.5230737322291614 57.53437965366558, -2.5333976003458236 57.53694675962128, -2.536637191427645 57.53690069844859, -2.5433775626971884 57.534826560377034, -2.5473867890354946 57.530872107854655, -2.5537918716724612 57.53003487855775, -2.555276723828705 57.52193317668414, -2.552276555098688 57.520053703944484, -2.5509130279535275 57.51753733946915, -2.552257137101087 57.515127674132025, -2.5605589275623757 57.517009747060364, -2.564068379173865 57.5166132595513, -2.5679415547825575 57.517493264259585, -2.5733102204572194 57.51993024756379, -2.575421379699632 57.52179262685043, -2.5873564427733267 57.52206100660879, -2.5931760142126414 57.5198698468709, -2.604703018910442 57.516796013641624, -2.60733145988389 57.51483955187706, -2.60927774352214 57.511631446270364, -2.622162238339203 57.51431639464774, -2.6286031549462665 57.513988033822265, -2.6298352984569626 57.51477591275589, -2.6327129010077543 57.52025987349824, -2.6353151184402743 57.522913717968976, -2.641842316749607 57.52352204635567, -2.6441482512687915 57.524571209530606, -2.6498224144833813 57.52957063489507, -2.6515511179189657 57.53351585329687, -2.654962911222779 57.53490223638058, -2.66286688648976 57.53401387494463, -2.6682603208845777 57.53423068014548, -2.6688674185369625 57.53764808894727, -2.6773212880781196 57.53713136796802, -2.6857679886107646 57.539001762046695, -2.6946407487658646 57.537934623580156, -2.6963442714257018 57.53722370713066, -2.6980941926044424 57.53378622231977, -2.6963475687526914 57.53170735464905, -2.703794738088347 57.52982277351457, -2.7083932164802977 57.53056511953536, -2.7106698064271058 57.53187366380632, -2.7128759910417326 57.536578059260094, -2.7157467676269307 57.539456930927244, -2.7114563231427997 57.54369503973146, -2.7164231557765124 57.547281553619314, -2.7172861657287264 57.550637118303015, -2.717278605528991 57.55578153230773, -2.719953762704743 57.56422982437495, -2.7293945059332314 57.56589767738361, -2.7366989913404125 57.564302156988035, -2.7402003286117482 57.564853897120635, -2.742626690549836 57.566620864844445, -2.7495151036659307 57.56757463037921, -2.752045166084452 57.5689922839525, -2.748099374410845 57.57149496160621, -2.7489391949001747 57.574530663613274, -2.7472641625822027 57.57968584925431, -2.754473125403621 57.58000677908934, -2.7610069149693017 57.57905492891008, -2.775648458809619 57.58414625057019, -2.7806850976149966 57.58800920064476, -2.7828234184304024 57.59684213932893, -2.792256373048657 57.60508522252992, -2.81064181240732 57.60995224232929, -2.805177143565141 57.61153041721704, -2.8028857905812545 57.613125051670394, -2.797410934236497 57.61893645752987, -2.78757531021265 57.625640811755034, -2.7824013206301936 57.63528533380177, -2.779998771192299 57.642582401739226, -2.7755012150704585 57.64604323987723, -2.7758808196899727 57.64813479489447, -2.7724061483577778 57.650268807596525, -2.7785341395614753 57.65373803439795, -2.783028222755676 57.65413154318452, -2.7887491075555437 57.65830432257442, -2.7908038914771436 57.66154685511151, -2.797550309824573 57.66202093815281, -2.8031813210175986 57.665715807512036, -2.7992915812601846 57.66826097202985, -2.801334340388683 57.669449041409415, -2.802916175588503 57.672452720643996, -2.8017617786164237 57.67931918849519, -2.80192088981134 57.6913790963968, -2.8014522995600126 57.696062007459744))
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>