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/K01000010 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/K01000010 geosparql: asWKT POLYGON ((-3.11255183210869 55.35678112209596, -3.0817629065309133 55.31555352301723, -3.069512467109196 55.3167441900268, -3.046640662047247 55.29551474048094, -3.0546551095000667 55.280724655417245, -3.0429562556435963 55.27153248687429, -3.019936155280101 55.2791250925304, -3.010169297880845 55.26743452056736, -2.9959286506735796 55.26908811646043, -2.9620838122602793 55.290265562636215, -2.917058875993218 55.277411577438095, -2.906932996868239 55.28551628993781, -2.8930537142630053 55.279894512589294, -2.884925621897502 55.262691500660985, -2.9050128638286767 55.257183972896705, -2.9214450361995867 55.23672193141284, -2.863842468750223 55.23635163626042, -2.861916487815796 55.223285991492894, -2.8881870900239623 55.207866225011216, -2.9047518920922704 55.17366924084906, -2.8969156716208464 55.1466833327941, -2.8651172527802746 55.13532552831769, -2.8585400960381184 55.10834877944119, -2.827607960317233 55.12483683060465, -2.8255025508611036 55.138314113562565, -2.7849188436939896 55.14177579896977, -2.7035074350202453 55.173167590060146, -2.689790278724994 55.18898422753518, -2.674555558664382 55.175891258936645, -2.6773242422814647 55.15548761180606, -2.6569394687185506 55.13613207687522, -2.5989028449415446 55.12457550344997, -2.5935395419329645 55.10509631368394, -2.56842591966619 55.096261622695, -2.56213622088229 55.08346216606928, -2.502574307436441 55.09075870959018, -2.4860862561720403 55.08244363859537, -2.503984992011089 55.06243014024255, -2.4830501548866444 55.04001884516253, -2.572851664705704 55.01639837609492, -2.5733107539721716 54.98763765984321, -2.6013616298771507 54.97131740111518, -2.595568789868167 54.963480732931636, -2.5684914165329134 54.958361885565616, -2.567649436073177 54.9408561089225, -2.545272506324298 54.929171866889675, -2.5662213019369036 54.91923782863644, -2.5764140730941945 54.896717353623785, -2.605344354412104 54.88487805439479, -2.5733522792533536 54.85354617450032, -2.582671044420364 54.84586691945165, -2.603768736047267 54.85334367535764, -2.6553301197120045 54.85476865246118, -2.6801366288603834 54.83531723840797, -2.7012245495508838 54.833604856166886, -2.6897166238565857 54.81935621078386, -2.705662418045632 54.816362490511466, -2.751014358733519 54.82905545236043, -2.773770687935343 54.800563104854646, -2.8093729680787867 54.78825048616735, -2.811349766919381 54.77754845362357, -2.7815298581532866 54.76088549621202, -2.8104459240698985 54.761057627691486, -2.8274277502141785 54.77159648083189, -2.8714549004911913 54.769914858121524, -2.882541788490155 54.76279843105852, -2.9121617388071326 54.78014826612268, -2.89305205859552 54.79285344305881, -2.9044142438111487 54.81301922920505, -2.9209258290984668 54.8092706199492, -2.9447835016662003 54.78404376649067, -2.9844372822017435 54.776157196309406, -2.9938313827165914 54.759634325896926, -3.0260894134896343 54.74808139786607, -2.9837478839056253 54.72095033131336, -2.9922745229634384 54.69624294208215, -3.063798691049797 54.700082021302656, -3.0819202309047125 54.69525391388421, -3.087190948170531 54.68609212756952, -3.054604506437979 54.67975694449653, -3.0799602614419936 54.66505263256178, -3.136407766533674 54.7289346271816, -3.101648095492348 54.740311280719474, -3.105789597129675 54.74631679912049, -3.1578542260246762 54.79126646146386, -3.1973467101486808 54.79321072233534, -3.224173521969346 54.78425517814697, -3.2356685547171966 54.789473085703975, -3.231281428638678 54.796795586915486, -3.246456343033588 54.805223278703686, -3.228170435373813 54.80889119720817, -3.2259078883175634 54.82161659509476, -3.237495345337514 54.82570390008264, -3.2960606113390964 54.82316923692711, -3.3490701801592118 54.80519162191508, -3.3651971774197804 54.78676032207652, -3.393327994532331 54.78988934942194, -3.3925568456045596 54.773377200216416, -3.430868671468906 54.762933649785836, -3.438775765573388 54.76577125426774, -3.4378562062791125 54.8016272316063, -3.3984140525432793 54.869171162006296, -3.3711148168728937 54.88855093759873, -3.366989346064502 54.89146985142937, -3.3388459888113196 54.900233976671714, -3.3397576241182967 54.89626956945539, -3.2962313841461848 54.880175666706286, -3.2818556761638242 54.874976998741936, -3.2880473488285356 54.89260610127357, -3.2637424650259117 54.89129414632629, -3.2496349049247235 54.902096907807795, -3.254345843582577 54.91086274951701, -3.27457986554986 54.90309135216832, -3.309271704291654 54.91204269035096, -3.3134758397653337 54.918893044918654, -3.30251923123429 54.927698438381185, -3.285430542266704 54.94142235169907, -3.250411775972854 54.94420821131304, -3.247867194366575 54.94441023301615, -3.214233085852193 54.955290389159636, -3.1860027853921835 54.94993395398417, -3.1551560096363738 54.9323582342625, -3.1320476230295133 54.9294066382811, -3.1188142379119883 54.92771417406226, -3.118337820860026 54.92781939020409, -3.1102349419014694 54.929608523452906, -3.0736191487139695 54.93768465161781, -3.129758260105189 54.93439798376845, -3.131446516476097 54.94722167144731, -3.1087322501647408 54.94911504675208, -3.116270408310105 54.960662282889935, -3.103649069750093 54.971467167117325, -3.071878203641781 54.96893408278823, -3.0570770684561936 54.98296324784692, -3.0562077269607433 54.987272408277725, -3.0709620244566804 54.982358494593655, -3.0916147719180844 54.975476019695975, -3.1170279371857843 54.97614249252505, -3.11899965143025 54.9761940508321, -3.149055961497655 54.9637420833342, -3.208083506194898 54.9762691728007, -3.197832808693127 54.98410828198092, -3.1785411852452867 54.98226532574676, -3.183296610959955 54.98973152942954, -3.1591433388015533 54.9880415420532, -3.1650381121406945 55.02589109221385, -3.129694594916305 55.01680635340174, -3.0944444895347107 55.026636494841476, -3.0973038395021035 55.03624438619147, -3.1150817703648035 55.04084567014144, -3.1751224510836757 55.03906755792346, -3.1929549368489307 55.049406766967174, -3.1721268385331487 55.06295617180117, -3.1771853869775115 55.08487077320776, -3.147169301941589 55.09474592993751, -3.128530041001543 55.081125625390335, -3.105029682404432 55.077602737403524, -3.0916661541170383 55.08555958884306, -3.0942670015066724 55.10278142329965, -3.110512638463728 55.1104256896827, -3.134210685148052 55.16449447495005, -3.1268437265092155 55.17545429802707, -3.146471791322467 55.17603518114324, -3.147390016660825 55.191599976374405, -3.1684416796747534 55.19008811922576, -3.1706778717971917 55.197246830967885, -3.209960322314072 55.18917105421143, -3.2253263391061964 55.19698866288879, -3.2037600284997203 55.250223208940376, -3.2616985804293868 55.30382622281167, -3.303999207879889 55.31308739733963, -3.309612254436761 55.32944551046383, -3.296676380630172 55.34240214207788, -3.2233894931599343 55.37623175852308, -3.1792537021546003 55.360467766933176, -3.1745026878402527 55.34679158440441, -3.1336265615725494 55.359147046819956, -3.11255183210869 55.35678112209596))
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>