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/S37000001 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S37000001 geosparql: asWKT POLYGON ((-2.059110040919489 57.211832870541684, -2.059953362837143 57.20917078328312, -2.062910651939346 57.20477039327519, -2.065958669738057 57.198772725499346, -2.0656298652554286 57.19534679814991, -2.068447575648897 57.19424582075551, -2.070455997150479 57.18966695905334, -2.069410914778901 57.1882204446071, -2.0708117566727253 57.18369130023891, -2.0725566047760373 57.18262675295497, -2.0722569690433756 57.17831687606343, -2.0442131423637924 57.178335331890665, -2.046785379823792 57.172057095051656, -2.0479350022058527 57.15825609838486, -2.0336180190653423 57.15535300744831, -2.0270535799246767 57.152556396314274, -2.0205104666208142 57.14786851599829, -2.0179476130437544 57.14532403721676, -2.016150627802827 57.13992102098213, -2.0163173572092514 57.135910958107466, -2.01810444419527 57.12497205624509, -2.039483621341703 57.10368083681616, -2.0722498072484963 57.10366168054256, -2.0739521435851995 57.10140954091772, -2.0722619364282426 57.097188522318014, -2.073221558995884 57.09588533990414, -2.0778151976638988 57.09542356995416, -2.081558705077157 57.09119468018066, -2.0804417938710733 57.08904843011099, -2.0851948679289696 57.08585360037051, -2.0889561522640383 57.08706092721086, -2.097801718891846 57.08834518140587, -2.118677777152992 57.09043636926756, -2.118738641619411 57.10378881633705, -2.121976201447784 57.10845691166269, -2.1211667291301586 57.113832257566855, -2.1240267929217063 57.1173930045227, -2.1260664466025734 57.118425788241765, -2.145062963070906 57.11701557447374, -2.149651129043652 57.11477160562529, -2.160330828048002 57.112839611662395, -2.167216216207318 57.114344269580734, -2.1710922607265677 57.11416831384366, -2.17540713923292 57.11248792944114, -2.1764263406280553 57.109478925195425, -2.1788784240864434 57.107161437402866, -2.1837944812655676 57.10813076954445, -2.189071470210796 57.107943212858, -2.191903925480464 57.10468346717497, -2.19663890848071 57.1041775229301, -2.202100025856859 57.10487586443651, -2.2096855640670943 57.10260946081994, -2.2108612350668317 57.101232209356816, -2.2156068586818733 57.10040573332699, -2.218810776777217 57.10183212146298, -2.223885233386746 57.100825146248035, -2.231920101018802 57.0950037987032, -2.2375289765107955 57.09440308451521, -2.2454099816982076 57.09602827551313, -2.250738260312282 57.09615871609761, -2.259371354287017 57.09523023807814, -2.2610165347351363 57.09379226676856, -2.2598734580630513 57.087263892325346, -2.2609150628812635 57.08545519655807, -2.2700985807762253 57.07937392990969, -2.2777995761389445 57.07811740312426, -2.2817234767359844 57.07914975525752, -2.287695837298559 57.07967159869953, -2.2938481172018563 57.07915422194477, -2.300268464949781 57.077327966860885, -2.30602140291751 57.076643010068764, -2.3077476950346343 57.0777608153229, -2.3127868849951145 57.07753629918624, -2.3170111932528012 57.080034674865374, -2.3098091387705937 57.082047914976016, -2.301754885861354 57.08506431001016, -2.299232379130513 57.086915511418965, -2.2956869775397757 57.08738562313477, -2.2913529010210008 57.089319148960456, -2.288120020075122 57.09374183247104, -2.2846491350289457 57.094113571630494, -2.2806479103172776 57.095958764720535, -2.2796402028885194 57.09932423641873, -2.284496979140317 57.10183036891427, -2.2899272069105634 57.10220336406801, -2.2986152805630398 57.104348795603286, -2.3039611681606966 57.10506354354577, -2.309278635818436 57.103911436359624, -2.309889744263609 57.10277182080721, -2.3197524946128403 57.10182356577339, -2.321639601431532 57.10262626667613, -2.3309443010748745 57.1015097286566, -2.3374519266462004 57.10253169012989, -2.3408854560525314 57.10397590023236, -2.3446975431634987 57.10671424208609, -2.3487826292340976 57.10698232296482, -2.3513738529501853 57.10830735063412, -2.360870180891912 57.10919027290926, -2.36091833018627 57.111257190501455, -2.351695103424713 57.12077136953472, -2.3505549471359095 57.1310207289709, -2.3538286282534084 57.1328683452554, -2.3511785825030476 57.13564619970125, -2.33934711250128 57.133767193264326, -2.3328679889142276 57.13159089388168, -2.3255718605065994 57.12638545698566, -2.317103666084166 57.12465542775879, -2.308898140961716 57.12482329129422, -2.3041712464631297 57.12343074769984, -2.3043469201245266 57.125721072929394, -2.2998415416076394 57.12658885427485, -2.2962321122606015 57.12839857010657, -2.2895809236092415 57.128889363955466, -2.283757775030094 57.13078643461393, -2.2809641066866173 57.13074324631921, -2.2788063283157105 57.128938942513706, -2.2746285080118134 57.13065670808233, -2.2771845256779186 57.13381850461086, -2.277650114807859 57.13820211241262, -2.2728187928786934 57.14445505930778, -2.2670506417871787 57.14570624813586, -2.267016966074291 57.147234348369814, -2.259240972871221 57.15426370024906, -2.2574558309369888 57.157352168134615, -2.2542858472735046 57.15840962096137, -2.253509321473689 57.160179086654225, -2.2554974234844654 57.16174528380565, -2.2610861972925775 57.17003862035643, -2.267724105122236 57.17325488996031, -2.2710429495657642 57.17275994278063, -2.273312874263492 57.17669500543554, -2.272473593495859 57.179519293101706, -2.2739287889108946 57.18169540432645, -2.2797822610367664 57.18445106109967, -2.2822300846494765 57.18699224656026, -2.285711656954069 57.1877218376163, -2.2901899206058975 57.19315252781936, -2.295054240286336 57.19765339281304, -2.2895879874098686 57.20186114735301, -2.2810835223936694 57.20091033432059, -2.278434685606529 57.20431092219482, -2.2816369086331485 57.207076833299936, -2.277851332991377 57.21083566470536, -2.2779304607017927 57.21298961480611, -2.2818138081352752 57.213110271845885, -2.281939914849517 57.214895828409816, -2.2790057564341724 57.21966692763516, -2.2803117071317236 57.224621723139336, -2.279504977095267 57.227217770967435, -2.276967647189917 57.22942241806611, -2.27404774395285 57.23011779992119, -2.2709483747888894 57.23322634450424, -2.2647073194511287 57.231685610047066, -2.264831649747922 57.22961744083958, -2.2618589172885764 57.22850176116773, -2.253892315321456 57.23093366606671, -2.251623849907996 57.23347141116411, -2.2438497870767677 57.233947536572835, -2.240929991546181 57.23531136330957, -2.2372578768962903 57.23282379811984, -2.236185164125218 57.22929555636483, -2.2315816871143137 57.231048657955114, -2.226288978750144 57.2285179513532, -2.223986199915142 57.230347427849736, -2.2172505229077384 57.230176879088845, -2.211108203000422 57.229203744926714, -2.2067936924403453 57.2313101741677, -2.204438871480234 57.2308047572843, -2.204118411910416 57.226174564526005, -2.201785007449472 57.22287536560156, -2.192263289095892 57.21912647345418, -2.1864988412196187 57.218059868276995, -2.183946974448214 57.21633267201008, -2.179849646137762 57.21578075410411, -2.1793932287210125 57.21315653801319, -2.174662872181645 57.21027881182877, -2.1717829668431508 57.212558233885815, -2.1669089668305435 57.21106015619325, -2.1650895177309484 57.21326697305664, -2.154631119090015 57.21365397765198, -2.1480802606988867 57.2112795756626, -2.143228364812461 57.21135265500712, -2.136595696564578 57.214741160540434, -2.1377531446030784 57.216566138162534, -2.1314348380004247 57.21792580965174, -2.128928647026435 57.22286625369164, -2.1293965174857425 57.22592716800876, -2.1282425285990985 57.22745905210867, -2.1160350794430545 57.22502667820525, -2.0946565146419607 57.22250698574601, -2.087730433309232 57.21938327853254, -2.0808451129846754 57.21930978374361, -2.0759484188023802 57.21829151636357, -2.0676037904793687 57.214138044203885, -2.0638506347080883 57.21427570815503, -2.0630515566706453 57.21262229310088, -2.059110040919489 57.211832870541684))
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>