Top-N Sorts: Limit and Pagination
Learn about limits, pagination, and why we should avoid offsets in queries.
We'll cover the following...
It would be pretty interesting to get the list of the top three drivers in terms of races won by decade. It’s possible to do so thanks to advanced PostgreSQL date functions manipulation together with the implementation of lateral joins.
with decades as(select extract('year' from date_trunc('decade', date)) as decadefrom racesgroup by decade)select decade,rank() over(partition by decadeorder by wins desc)as rank,forename, surname, winsfrom decadesleft join lateral(select code, forename, surname, count(*) as winsfrom driversjoin resultson results.driverid = drivers.driveridand results.position = 1join races using(raceid)where extract('year' from date_trunc('decade', races.date))= decades.decadegroup by decades.decade, drivers.driveridorder by wins desclimit 3)as winners on trueorder by decade asc, wins desc;
The above query is a classic top-N implementation. It reports for each decade the top three drivers in terms of race wins. It’s simultaneously a classic top-N implementation because it’s done thanks to a lateral subquery, and a not-so-classic implementation because we’re joining against computed data. The decade information is not part of our data model, and we need to extract it from the races.date
column.
The query extracts the decade first in a common table expression introduced with the with
keyword. This CTE is then reused as a data source in the from
clause. The from
clause is about relations, which might be hosting a dynamically computed dataset, as is the case in this example. ...