2 Upvotes

Left join LATERAL

SQL
Querying

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

By changjulian17 - Last Updated Dec. 22, 2021, 12:59 p.m.

Did you find this snippet useful?

Sign up to bookmark this in your snippet library

COMMENTS
RELATED SNIPPETS
median
SQL
Querying

2
SQL creating a view
SQL
Querying

1
SQL window function
SQL
Querying

1
SQL RANK
SQL
Querying

1
EXISTS
SQL
Querying

1
Top Contributors
103
100