Skip to content
Ameen Abozaid
Planned lessonMID-011-L04

Advanced joins and LATERAL

  • Lesson 4 of 7
  • Mid-level
On This Lesson 4 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

use joins beyond the basic inner/left, including LATERAL.

Planned topics

  • LEFT JOIN keeps unmatched left rows; conditions on the right table go in ON, not WHERE, or it turns into an inner join.
  • Self-joins compare rows of one table (for example, product pairs bought together).
  • LATERAL lets a subquery on the right refer to columns of the left row: a "for each row" query.
  • Typical use: top N per parent, with ORDER BY ... LIMIT n inside.
Draft outline details

Demonstration

sql
SELECT c.name, top.*
FROM category c
CROSS JOIN LATERAL (
  SELECT p.name, p.price_cents FROM product p
  WHERE p.category_id = c.id ORDER BY p.price_cents DESC LIMIT 3
) top;

Check your understanding

What does LATERAL allow that a normal subquery in FROM does not?

Show answer

Referring to columns of earlier FROM items.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Continue to next lesson

Type to search levels, phases, modules and lessons.