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/S12000033 http://www.w3.org/1999/02/22-rdf-syntax-ns#type geosparql: Geometry
http://statistics.gov.scot/id/statistical-geography/S12000033 geosparql: asWKT POLYGON ((-2.059110040919489 57.21183287054167, -2.0599533628371582 57.20917078328312, -2.0629106519393305 57.20477039327519, -2.065958669738088 57.198772725499325, -2.0656298652554437 57.19534679814989, -2.068447575648897 57.194245820755505, -2.0704559971504946 57.18966695905334, -2.0694109147789166 57.18822044460709, -2.0708117566727253 57.18369130023891, -2.0725566047760524 57.18262675295497, -2.072256969043406 57.17831687606343, -2.044213142363808 57.178335331890665, -2.046785379823792 57.17205709505167, -2.0479350022058527 57.15825609838486, -2.0336180190653423 57.155353007448305, -2.0270535799246923 57.15255639631425, -2.02051046662083 57.14786851599829, -2.01794761304377 57.14532403721679, -2.0161506278028423 57.1399210209821, -2.016317357209236 57.135910958107466, -2.0181044441953007 57.12497205624507, -2.0394836213417338 57.103680836816146, -2.0722498072484807 57.10366168054255, -2.0739521435851995 57.10140954091772, -2.0722619364282426 57.097188522317985, -2.073221558995914 57.09588533990411, -2.0778151976638988 57.09542356995416, -2.0815587050771875 57.09119468018066, -2.0804417938711035 57.08904843011102, -2.0851948679289847 57.08585360037051, -2.088956152264054 57.08706092721082, -2.097801718891846 57.08834518140587, -2.118677777153007 57.09043636926756, -2.1187386416194114 57.10378881633707, -2.121976201447784 57.10845691166267, -2.121166729130143 57.113832257566834, -2.1240267929217365 57.1173930045227, -2.126066446602574 57.11842578824179, -2.1450629630709215 57.11701557447374, -2.1496511290436677 57.11477160562531, -2.1603308280480173 57.11283961166238, -2.167216216207334 57.114344269580734, -2.171092260726583 57.11416831384366, -2.175407139232935 57.112487929441116, -2.1764263406280713 57.109478925195425, -2.1788784240864434 57.107161437402866, -2.1837944812655983 57.10813076954444, -2.189071470210796 57.107943212857975, -2.1919039254804953 57.10468346717494, -2.196638908480725 57.104177522930094, -2.202100025856859 57.10487586443651, -2.2096855640670943 57.10260946081994, -2.2108612350668317 57.10123220935679, -2.2156068586819044 57.10040573332697, -2.218810776777202 57.10183212146298, -2.223885233386746 57.10082514624806, -2.2319201010187864 57.09500379870317, -2.2375289765107955 57.09440308451519, -2.2454099816982236 57.09602827551314, -2.2507382603122665 57.0961587160976, -2.259371354287002 57.09523023807814, -2.261016534735151 57.09379226676854, -2.259873458063051 57.08726389232533, -2.260915062881279 57.08545519655805, -2.2700985807762253 57.07937392990969, -2.2777995761389596 57.078117403124246, -2.2817234767359844 57.07914975525749, -2.287695837298575 57.07967159869952, -2.293848117201872 57.07915422194477, -2.3002684649497964 57.0773279668609, -2.30602140291751 57.07664301006874, -2.30774769503465 57.07776081532287, -2.312786884995115 57.07753629918623, -2.317011193252817 57.080034674865374, -2.309809138770609 57.082047914976, -2.301754885861354 57.08506431001016, -2.2992323791305282 57.086915511418965, -2.2956869775397757 57.08738562313474, -2.291352901021016 57.089319148960456, -2.2881200200751524 57.09374183247103, -2.284649135028976 57.09411357163048, -2.280647910317277 57.0959587647205, -2.2796402028885194 57.0993242364187, -2.284496979140317 57.10183036891427, -2.289927206910594 57.10220336406801, -2.2986152805630398 57.104348795603286, -2.3039611681607117 57.10506354354573, -2.309278635818452 57.103911436359624, -2.309889744263609 57.102771820807206, -2.3197524946128554 57.10182356577339, -2.3216396014315475 57.10262626667613, -2.3309443010748896 57.10150972865657, -2.3374519266462004 57.10253169012987, -2.340885456052532 57.10397590023235, -2.3446975431635138 57.10671424208608, -2.3487826292341287 57.10698232296482, -2.3513738529501853 57.10830735063412, -2.360870180891912 57.10919027290923, -2.36091833018627 57.11125719050143, -2.351695103424713 57.12077136953472, -2.3505549471359095 57.1310207289709, -2.353828628253393 57.1328683452554, -2.3511785825030476 57.13564619970125, -2.3393471125012795 57.133767193264305, -2.3328679889142427 57.13159089388167, -2.3255718605065994 57.12638545698566, -2.3171036660841966 57.12465542775879, -2.308898140961747 57.1248232912942, -2.304171246463129 57.12343074769982, -2.3043469201245266 57.125721072929394, -2.299841541607639 57.126588854274836, -2.2962321122606166 57.12839857010653, -2.2895809236092255 57.128889363955466, -2.283757775030094 57.130786434613945, -2.2809641066866324 57.13074324631919, -2.2788063283157105 57.128938942513706, -2.274628508011813 57.130656708082306, -2.2746456062047713 57.13230234820252, -2.2771845256779186 57.13381850461086, -2.2776501148078587 57.13820211241259, -2.2728187928787085 57.14445505930776, -2.2670506417871783 57.14570624813587, -2.2670169660743067 57.1472343483698, -2.2592409728712366 57.15426370024906, -2.2574558309370047 57.157352168134594, -2.2542858472735356 57.158409620961365, -2.253509321473689 57.160179086654225, -2.255497423484497 57.16174528380565, -2.2610861972926086 57.17003862035639, -2.2677241051222206 57.17325488996029, -2.27104294956578 57.1727599427806, -2.2733128742635076 57.17669500543554, -2.272473593495859 57.179519293101684, -2.273928788910879 57.18169540432645, -2.279782261036781 57.18445106109965, -2.282230084649491 57.18699224656023, -2.285711656954069 57.1877218376163, -2.290189920605913 57.19315252781935, -2.2950542402863516 57.19765339281304, -2.2932027519492224 57.199781331155805, -2.289587987409899 57.20186114735301, -2.281083522393685 57.20091033432057, -2.278434685606545 57.20431092219482, -2.2816369086331485 57.207076833299936, -2.2778513329913928 57.21083566470536, -2.2779304607018083 57.21298961480609, -2.2818138081352752 57.213110271845885, -2.2819399148495316 57.214895828409794, -2.2790057564341724 57.21966692763516, -2.2803117071317236 57.224621723139336, -2.2795049770952676 57.22721777096746, -2.2769676471899474 57.22942241806609, -2.2740477439528655 57.23011779992119, -2.270948374788905 57.23322634450426, -2.2647073194511287 57.23168561004708, -2.264831649747922 57.22961744083956, -2.261858917288576 57.22850176116771, -2.258801198269523 57.22895092485461, -2.2538923153214867 57.23093366606669, -2.251623849908011 57.23347141116411, -2.2438497870767677 57.233947536572835, -2.2409299915461958 57.23531136330956, -2.237257876896305 57.23282379811983, -2.236185164125218 57.22929555636483, -2.2315816871143292 57.231048657955114, -2.226288978750175 57.22851795135318, -2.223986199915158 57.230347427849736, -2.217250522907754 57.230176879088866, -2.211108203000422 57.229203744926714, -2.2067936924403613 57.23131017416769, -2.204438871480234 57.23080475728428, -2.204118411910431 57.226174564526005, -2.2017850074494874 57.22287536560156, -2.192263289095923 57.219126473454146, -2.1864988412196182 57.218059868276974, -2.1839469744482294 57.21633267201004, -2.179849646137762 57.215780754104074, -2.1793932287210125 57.21315653801319, -2.17466287218166 57.21027881182875, -2.1717829668431508 57.212558233885815, -2.166908966830559 57.21106015619324, -2.1650895177309635 57.21326697305667, -2.154631119090015 57.21365397765198, -2.1480802606988867 57.2112795756626, -2.143228364812477 57.211352655007126, -2.1365956965646093 57.21474116054041, -2.1377531446030935 57.216566138162534, -2.1314348380004247 57.21792580965173, -2.1289286470264503 57.22286625369164, -2.1293965174857425 57.22592716800872, -2.1282425285990985 57.22745905210867, -2.11603507944307 57.22502667820524, -2.0946565146419758 57.22250698574601, -2.0877304333092472 57.219383278532504, -2.0808451129846905 57.2193097837436, -2.075948418802396 57.21829151636357, -2.0676037904793843 57.21413804420388, -2.063850634708088 57.214275708155, -2.06305155667063 57.212622293100864, -2.059110040919489 57.21183287054167))
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>