Introduction to JOIN

Order only stores a user_id, not the user's name or email. That keeps user data in one place.

JOIN reunites split tables by matching a foreign key back to its row.

Order rows alone

Order only carries user_id — no name, no email.

SELECT * FROM "Order";
Loading plan...

Combine with JOIN

JOIN matches Order.user_id to User.id, pulling both tables into one result.

SELECT "Order".id, "User".name, "Order".amount
FROM "Order"
JOIN "User" ON "User".id = "Order".user_id
ORDER BY "Order".id;
Loading plan...

Try Yourself

Loaded Database:introduction-to-join.database-init

The database for this lesson is already loaded. Write any query you want and run it directly in your browser.

Exercises

0 OF 1 DONE
1.List each order's amount with the user's email, ordered by order id.

What We Learned

  • JOINcombines rows from two relations by matching column values.
  • Foreign keya column referencing another table's primary key.
  • ON clausetells PostgreSQL which columns from each side must match.