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/S12000018 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S12000018 geosparql: asWKT POLYGON ((-4.673336004067818 55.93832464319474, -4.661072907314719 55.936865384062244, -4.655884691982016 55.93715002788838, -4.635872699870379 55.939254575917836, -4.630647409419874 55.939493925567454, -4.623347137085831 55.93870052185095, -4.6160884566208775 55.93740521264589, -4.609874266412385 55.93629671037798, -4.611335012718991 55.93147322587571, -4.617405431703361 55.92858489957429, -4.617427322947823 55.92418736983246, -4.622829729489194 55.91965733723775, -4.627983022478181 55.91626484227823, -4.629572937744754 55.91641518441892, -4.635043875307934 55.913732028899716, -4.632805121006378 55.90955806811667, -4.630782768630999 55.90743781223945, -4.626286954742611 55.90633477407005, -4.615991463918112 55.90711224133056, -4.613914545358617 55.90311881692133, -4.612029352050086 55.901589489137194, -4.6120137154499306 55.898336091781424, -4.613378448911995 55.897433434683286, -4.613407759800689 55.8938752725281, -4.617264481389321 55.89157229342192, -4.618754088125755 55.889396869277135, -4.612316843518336 55.88771266027377, -4.609921034531634 55.885092225907215, -4.6039434105643045 55.88407015061535, -4.6047223697556126 55.881687092821124, -4.599116148167754 55.87967901100511, -4.601880557977612 55.876689435995296, -4.6052242183215375 55.8754556481555, -4.600222621883409 55.87191039174839, -4.608645800685527 55.87028579671835, -4.60908196695052 55.86789918670669, -4.603317736782475 55.865104639002574, -4.601409369297998 55.86285922452335, -4.608967439636416 55.86311715273182, -4.612248930002986 55.86209653899845, -4.615461092678093 55.862305125942, -4.622824773485143 55.86159910482549, -4.62571429533542 55.85882742312525, -4.624767783129413 55.855741234633314, -4.6221338228137965 55.85438449529835, -4.622917919129452 55.85025110810178, -4.62481964279621 55.84840833639777, -4.629476398504079 55.84657943591602, -4.632640406348463 55.84110971893489, -4.638898955197537 55.84072925339311, -4.643339980273162 55.83908604188332, -4.654758121965977 55.84021532506719, -4.667588809031621 55.83898484049757, -4.678424144227928 55.838227008661626, -4.681277905643458 55.838402125695865, -4.685136864441625 55.840391604715066, -4.6948639121932025 55.84083494900765, -4.7033883217949155 55.842589337111896, -4.7041957477429115 55.846737775573665, -4.7100532612535755 55.84711221449972, -4.715933248266742 55.849292525995544, -4.716728298567445 55.85060975263533, -4.724855989392559 55.85258720605573, -4.730011998592334 55.85262570053674, -4.738800330984457 55.85034936349751, -4.75094123425606 55.8497682371921, -4.755356458031512 55.85173682726154, -4.761594448846794 55.8554260838417, -4.769390564199441 55.86470693310016, -4.77780555207162 55.867492404406825, -4.781057323235208 55.869931286615966, -4.784096495319835 55.86961990316173, -4.794188260573146 55.87223601171972, -4.7957133668630645 55.874992397022204, -4.800276862804826 55.87963468490224, -4.800679276584449 55.88577383191096, -4.798704966219447 55.88800754292264, -4.799488005320642 55.892326849626315, -4.80270070478951 55.8924020697332, -4.812598136120011 55.88980311042145, -4.814150954656149 55.88772259077081, -4.821754983224002 55.886604583619246, -4.826541614612391 55.88303846665034, -4.8309052401795585 55.88223701103821, -4.837548385281724 55.87969744448309, -4.841442866726508 55.87620521264959, -4.8452754456321765 55.875154804009306, -4.84724112874238 55.876997001141106, -4.858045433870524 55.874319377272954, -4.860529244435113 55.87515605761636, -4.865049452521467 55.87493843063071, -4.86942141972127 55.87375427496521, -4.874417223660882 55.87376703075777, -4.8801940103971 55.87549353471498, -4.886504769732141 55.87546610706837, -4.890564876749465 55.87467502184089, -4.889756003788237 55.881712681562185, -4.895107304392898 55.884656650183636, -4.898185283793765 55.88854921167661, -4.8988323262781 55.89162713199976, -4.8952374038472115 55.89644813134976, -4.8893410260642005 55.899015613987316, -4.885175511395134 55.9017606006355, -4.882668758635454 55.90228413444127, -4.880804325327582 55.90752071840947, -4.873167939611287 55.908517290531144, -4.875122923416149 55.911880276806286, -4.877891710203729 55.908231582174515, -4.880513852713456 55.90854604410806, -4.8839863345340735 55.912765005352995, -4.881416297711779 55.92079044134749, -4.879530116404009 55.92272677008193, -4.877453446988174 55.92814176621098, -4.8811421999348905 55.93165284111275, -4.881379425001635 55.936334164408365, -4.878953075570346 55.94269580505182, -4.875730248800704 55.944464703517696, -4.872402239540149 55.94464483717437, -4.864490253315867 55.94775773867288, -4.858387669303309 55.94948520396724, -4.854259304470558 55.952030468634256, -4.851224943821145 55.95157398976064, -4.844751039780395 55.952622626966, -4.836624742237082 55.95487307769661, -4.830695652574526 55.95595783618802, -4.820953450801628 55.9621140161967, -4.8153200787471615 55.96206131279094, -4.812179912751386 55.95765487264026, -4.806307682880793 55.957968643904785, -4.804492854792986 55.9590841058854, -4.79591319240247 55.97215553940113, -4.733962569401022 55.96011184437531, -4.724698147130708 55.95808980821678, -4.696915416676956 55.95280489890107, -4.704328783428369 55.94059598118067, -4.694545780612181 55.937659114704644, -4.69044484342921 55.93743330298958, -4.685458151547677 55.93595302157003, -4.677340997540775 55.9349343281901, -4.673336004067818 55.93832464319474))
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>