The above was based on just discovered https://extendsclass.com/postgresql-online.html With this sql: ==== create table T1(id integer primary key, val text); insert into T1(id, val) values(1, '1one'), (2, '1two'), (3, '1three'); create table T2(id integer primary key, val text); insert into T2(id, val) values(1, '2one'), (2, '2two'), (3, '2three'), (4, '2four'); select id, t2_cnt.cnt, max_t1.cnt from T2, (select count(*) as cnt from T2) t2_cnt, (select count(*) as cnt from T1) max_t1 order by id limit case when t2_cnt.cnt > max_t1.cnt then t2_cnt.cnt - max_t1.cnt else 0 end; ====