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
Did you find this snippet useful?

Sign up for free to to add this to your code library

median
SQL

4
SQL RANK
SQL

4
EXISTS
SQL

3