Left join LATERAL
SQL
lateral goes through each line in the 'from' table and queries the code in the lateral code
select
c.id category_id,
c.category category,
p.title title,
p.views as views,
p.id post_id
from categories c
left join lateral (
select id, category_id, title,views
from posts
where c.id=category_id
order by views desc, id asc
limit 2) p on c.id=p.category_id
order by 2, 4 desc, 5
11
10
10
10
10
