query-f37b5e1c9078b2a6be67ef31ce51803c
Pagination (ORDER and LIMIT)It’s quite common to care only about a few results: a first, first to, pioneer in; oldest, earliest; youngest, latest. In order to get an answer our entities should be ordered and limited: only returns a single result.LIMIT 1 limits the query to ten results. LIMIT 10 is any natural number. For example, countwhere results,count cuts off the result list at LIMIT count .)something is equivalent to just ASC(something)ending). (If you don’t specify either, the default is ascending sort, so descending or asc to specify the sorting order (DESC() or ASC()), but we’ll see some other kinds later. This expression can also be wrapped in either ?something can be any expression – for now, the only kind of expression we know are simple variables (something .something sorts the results by ORDER BY something can significantly speed up the query, since WDQS can stop searching for results as soon as it’s found enough to fill the limit.) LIMIT result, but don’t care about which one. In either case, adding the some. In this case, the results aren’t sorted, so you don’t have any guarantee which results you’ll get. Which is fine if you happen to know that there’s only a certain number of results, or you’re just interested in ORDER BY without LIMIT(You can also use The query that returns the ten most populous countries:
Use at
- https://query.wikidata.org/sparql
PREFIX wikibase: <http://wikiba.se/ontology#>
PREFIX wdt: <http://www.wikidata.org/prop/direct/>
PREFIX wd: <http://www.wikidata.org/entity/>
PREFIX bd: <http://www.bigdata.com/rdf#>
SELECT DISTINCT ?country ?countryLabel ?population ?ended
# ideally we don't need a "DISTINCT" above
# we get multiple records because some items have multiple P31 statements that lead to a Q3624078
# we can trim duplicates as workaround (or inspect classification and P31 links)
#SELECT ?country ?countryLabel ?population ?ended
WHERE
{
?country wdt:P31/wdt:P279* wd:Q3624078; #countries
wdt:P1082 ?population; #with their population
MINUS
{
?country wdt:P576 ?ended.
} # exclude "former" countries
SERVICE wikibase:label { bd:serviceParam wikibase:language "en". }
}
ORDER BY DESC(?population) # most populous countries - descending population
LIMIT 10