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/S01004997 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S01004997 geosparql: asWKT POLYGON ((-3.49652095844562 56.35807532752519, -3.498041269968736 56.35722132278054, -3.503691004705789 56.35683840752569, -3.505127743641112 56.3559224299918, -3.504519421755636 56.35321623968268, -3.505666374298472 56.35275304137157, -3.505739599135513 56.351736806979744, -3.507185062275061 56.35022766147701, -3.508222789088442 56.34946026368886, -3.510326687136256 56.349057252727434, -3.510763055968535 56.348225278722644, -3.509780779761721 56.347527406591084, -3.507125522457928 56.34748785018717, -3.504336528742234 56.34528438884687, -3.50093886251735 56.33705007193647, -3.503320161680792 56.334667029975485, -3.501622408117175 56.33386096662071, -3.499792550035768 56.329273636756376, -3.502492111613326 56.32924963451101, -3.503525598481834 56.327953591095124, -3.506111875871115 56.328677715038154, -3.516857439998449 56.32850874406245, -3.517185096809666 56.32724919180447, -3.511744165426157 56.324844194081635, -3.514459806947269 56.32301820441208, -3.513129921320474 56.3214337289534, -3.510624113580807 56.32332116119184, -3.51008715017747 56.32255936165993, -3.506791547645919 56.32253546758523, -3.506214165151972 56.3219150067092, -3.503406736390898 56.32247281014037, -3.502957415525054 56.32091480267313, -3.505084259491778 56.31990058304781, -3.506570329136971 56.31822021329839, -3.505950346166335 56.31725732975951, -3.506771942637914 56.31677111000957, -3.50738555096799 56.31511032752638, -3.509374744352503 56.31348669742627, -3.510673112380164 56.31074828259521, -3.511453147016475 56.30512287299324, -3.51515983502859 56.30525728108131, -3.516619672620982 56.307018524514795, -3.518014318240285 56.307540561731294, -3.523494817998312 56.30796745585208, -3.525283034966689 56.309032680731185, -3.529432092582055 56.31658319815543, -3.531185632644022 56.31636384870579, -3.536687489070727 56.314067309302644, -3.536571628941141 56.31279281330094, -3.540903169852705 56.30915378845818, -3.545646964090619 56.307737859303074, -3.542673893386832 56.305762186370636, -3.544068235704193 56.30466654910185, -3.547616664167562 56.303283412308836, -3.548600351576758 56.301231405128206, -3.551457867160344 56.29956027308487, -3.550465930657126 56.29661646602781, -3.545069353007489 56.296234656125776, -3.534302501829591 56.29369986585395, -3.530857099252465 56.29441642582497, -3.52490284395497 56.29462468920339, -3.518226107787587 56.29576703133064, -3.517501109112918 56.294598824214766, -3.518421370043695 56.2921345117411, -3.51581570422272 56.291618328092774, -3.511019899342795 56.289646244134715, -3.509514535001825 56.28752607064612, -3.51209610317186 56.28662297515388, -3.514178741093092 56.28495317891642, -3.519465007553023 56.2830104405777, -3.519536225103028 56.28236261216102, -3.522752309525384 56.28118193441054, -3.524013059412351 56.28120236025258, -3.52437153269313 56.28087446870775, -3.523613500539087 56.28049742758998, -3.52510501332159 56.279823117162714, -3.526729171315814 56.27964135699848, -3.530874686658921 56.277882909666424, -3.533119588250585 56.27586034282885, -3.537193342865606 56.27394082854915, -3.536981624166649 56.27349418125041, -3.538798703990421 56.272896529613575, -3.538415550884604 56.27260477069028, -3.54383045714677 56.27152198064865, -3.548258295797317 56.269597676526566, -3.547549581054555 56.26804305598556, -3.544916782505734 56.26642261165172, -3.542861599830557 56.265909118867945, -3.542650040758958 56.26265899272339, -3.54022188094079 56.25968806822272, -3.540231987488842 56.25712706320353, -3.541325819828627 56.25542416020421, -3.541126711682133 56.25368344580192, -3.5463186497231 56.25234271792858, -3.552044487940838 56.25183971856451, -3.556691035814227 56.25179033040941, -3.56141445501298 56.249268762266986, -3.564238862610295 56.251245857695025, -3.571034285543051 56.24928176827538, -3.572375473864766 56.24971400903063, -3.575396499436355 56.24978340584649, -3.579158652933643 56.248252818337676, -3.585622046465763 56.24687622046637, -3.589742840896689 56.25012106713126, -3.591784738592323 56.2503194536584, -3.594924247965154 56.24702621801425, -3.595372049802524 56.24537607249466, -3.601535756099244 56.246895879921404, -3.605701243680824 56.24847727965032, -3.608382403997858 56.2489097078506, -3.613903925757436 56.25201873099052, -3.618768588627323 56.25675353305054, -3.61892303553449 56.258117334638676, -3.615907979902055 56.2608883876061, -3.609804046973179 56.264319592719794, -3.616224182117127 56.26838728100023, -3.623409939969245 56.27181560786406, -3.63235720502235 56.27523827755515, -3.633061079723244 56.28114156146696, -3.641778457748636 56.29081164246612, -3.642738806916326 56.2939888123823, -3.644801778162321 56.296872777090776, -3.646387873280499 56.30406718179206, -3.64515442974511 56.30658158974488, -3.647264871195798 56.307919351433895, -3.645009134796575 56.309593710809146, -3.644675431783276 56.31085613384631, -3.64635030924076 56.31258607873009, -3.642329864216725 56.317599534728394, -3.644138967585719 56.31755756105347, -3.642106081575925 56.31916600366949, -3.64482633077872 56.32113369993768, -3.638129054530317 56.32333408378377, -3.637464508995451 56.32254315245847, -3.626781346883691 56.32457989529192, -3.627785113985144 56.32270668758283, -3.624893735199508 56.31935708527741, -3.621821876483922 56.318984011012354, -3.61723321276352 56.320212144008, -3.616670073396069 56.31989601517161, -3.616740516803541 56.31850232890504, -3.61431458202872 56.31427480576487, -3.607962257076828 56.31546267497764, -3.605752897853739 56.31482641110651, -3.603647400190694 56.31573083445069, -3.603996105374329 56.31620590707676, -3.601759838907242 56.316782955742596, -3.602027331900823 56.3172819235835, -3.598489850333375 56.31770445228265, -3.598253273004925 56.31603763582928, -3.596820160550203 56.31424103814648, -3.597489851067793 56.3136303686121, -3.593141565361701 56.31174572996618, -3.591240038427092 56.311850824799535, -3.591421567699591 56.3121924788073, -3.589137450761703 56.31260565300333, -3.588160425238913 56.31245220224035, -3.588196441916922 56.31174612776871, -3.58677271214763 56.31171944951196, -3.585558223702409 56.31089036527781, -3.58504799796337 56.31182354921898, -3.586612854766272 56.31215280390901, -3.586276717952056 56.31270762471646, -3.584969869018479 56.31346776118693, -3.58333505903454 56.31224867046555, -3.582169091070529 56.31298241563659, -3.582465142213908 56.31388617111407, -3.584011960518987 56.314658588434625, -3.582803570136855 56.31581373692129, -3.581726657292803 56.31557590111505, -3.577834777297532 56.31647018584872, -3.577463202872282 56.31569318054241, -3.575500387562082 56.316320194724234, -3.57493162266093 56.31546481913144, -3.572079549499572 56.31569873901161, -3.57207056764141 56.316660304027366, -3.567079759733338 56.31678645299633, -3.567517748171859 56.314849027696795, -3.565653957272111 56.31513318331097, -3.563285510590383 56.31612455293085, -3.55692678811946 56.31759740503663, -3.561607886034443 56.32097092793059, -3.561594345571455 56.325787320191466, -3.559205838290953 56.328288418124785, -3.560299410328079 56.33071022056919, -3.559963986055664 56.33095128255384, -3.558590566592133 56.33025499330031, -3.554424122437425 56.335186455107035, -3.555673535646164 56.33647366221847, -3.561155919464642 56.33802207842491, -3.563429828221413 56.34022179869833, -3.56414937744545 56.33922431616873, -3.565648691164001 56.33870219062199, -3.567009509857352 56.339143242662736, -3.567461039514358 56.34186911474589, -3.57182127668666 56.34403328545779, -3.57702949259391 56.34908882701289, -3.582294571296884 56.3480602535408, -3.582483509731549 56.351058992370234, -3.585770746729894 56.35072044438271, -3.587111149330357 56.35454907250487, -3.580325101242886 56.35638797271017, -3.58212260628704 56.35953690771577, -3.58626136330906 56.364093548539415, -3.58612876406362 56.36557785048132, -3.580443303537734 56.36589308964334, -3.577488860464453 56.366874205061265, -3.573698438609564 56.36682355175423, -3.567224632882001 56.367660361732504, -3.567862442981108 56.36860475031174, -3.563452901256878 56.369253492881306, -3.564560682347697 56.370605299123696, -3.550294300483983 56.37135064681789, -3.541812747481961 56.371016157369205, -3.539669454789511 56.372471477815836, -3.533379312574818 56.37469695859545, -3.533191458138033 56.37082658587561, -3.529198929912136 56.37099271809971, -3.529394540904621 56.369813222641405, -3.519732070951287 56.36989613497054, -3.517574757529224 56.36735275486056, -3.515610576818208 56.36762837980945, -3.510628199286781 56.3673208093705, -3.509278331408193 56.36511787381042, -3.506691279402045 56.36314562037636, -3.505291910694585 56.36255162198258, -3.502974542344572 56.36318952892147, -3.49652095844562 56.35807532752519))
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>