Chapter 510m

SQL JOINs

Combine data from multiple tables using INNER, LEFT, RIGHT, and FULL JOINs.

This content is not yet available in hi. Showing English version.

View in English →

SQL JOINs

JOINs combine rows from two or more tables.

INNER JOIN

sql
SELECT users.name, orders.product
FROM users
INNER JOIN orders ON users.id = orders.user_id;

LEFT JOIN

sql
SELECT users.name, orders.product
FROM users
LEFT JOIN orders ON users.id = orders.user_id;

Returns all users, even those without orders.

RIGHT JOIN

sql
SELECT users.name, orders.product
FROM users
RIGHT JOIN orders ON users.id = orders.user_id;

Returns all orders, even those without a matching user.

Multiple JOINs

sql
SELECT u.name, o.product, p.amount
FROM users u
INNER JOIN orders o ON u.id = o.user_id
INNER JOIN payments p ON o.id = p.order_id;

Example

sql
-- Join users with their orders
SELECT u.name, o.product, o.quantity
FROM users u
INNER JOIN orders o ON u.id = o.user_id
WHERE o.quantity > 2;