A quick review of SQL Joins
DEV Community

A quick review of SQL Joins

SQL Joins

Simply Join

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

Example Tables

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

Inner Join - only matching rows on both sides.

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

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

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

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

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

Full Join - keeps all rows from both tables.

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.
Read on DEV Community ↗ ← Back to News

Comments

No comments yet. Start the discussion.