DEV Community

Cover image for A quick review of SQL Joins
Gumathi Geo
Gumathi Geo

Posted on

A quick review of SQL Joins

SQL Joins, Simply

Join combines rows from two tables, matched on shared column, usually an id.

Example tables:

customers orders


customer_id | name order_id | customer_id | item
1 | Amina 101 | 1 | Bag
2 | Brian 102 | 1 | Shoes
3 | Carla 103 | 5 | Hat

Note: customer_id 5 has no match in customers. Carla has no orders. These gaps show what each join does differently.

_Inner Join _— only matching rows on both sides.

sql
SELECT customers.name, orders.item
FROM customers
INNER JOIN orders ON customers.customer_id = orders.customer_id;

Result: Amina's two orders only. Carla and order 103 drop out, no match.

Left Join — keeps all rows from left table, matched or not.

sql
SELECT customers.name, orders.item
FROM customers
LEFT JOIN orders ON customers.customer_id = orders.customer_id;

Result: Amina's orders plus Carla with NULL item.

Right Join — keeps all rows from right table, matched or not.

sql
SELECT customers.name, orders.item
FROM customers
RIGHT JOIN orders ON customers.customer_id = orders.customer_id;

Result: Amina's orders plus order 103 with NULL name.

Full Join — keeps all rows from both tables.

sql
SELECT customers.name, orders.item
FROM customers
FULL JOIN orders ON customers.customer_id = orders.customer_id;

Result: Amina's orders, Carla with NULL item, order 103 with NULL name. Nothing dropped.

Quick rule

: Inner join = strict match only. Left/right = pick which side to keep fully. Full join = keep everything. Default to inner join unless missing rows matter to your question.

Top comments (0)