INNER JOIN combines rows from two tables where join keys match. Unmatched rows on either side are excluded—ideal when every result must have related data on both sides.
Basic join
SELECT c.name,
o.id AS order_id,
o.total
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;Practice: Seed customers and orders sample data, then run each query. Verify row counts manually.
Table aliases
Aliases (c, o) shorten queries and disambiguate columns. Always qualify column names when names overlap.
Multiple joins
SELECT c.name,
o.id AS order_id,
p.name AS product_name,
oi.qty
FROM customers c
JOIN orders o ON o.customer_id = c.id
JOIN order_items oi ON oi.order_id = o.id
JOIN products p ON p.id = oi.product_id;Chain joins along foreign keys—sketch ER diagrams when queries grow complex.
Join vs WHERE filter
SELECT c.name, o.total
FROM customers c
JOIN orders o ON o.customer_id = c.id
WHERE o.total > 50;Put join conditions in ON; reserve WHERE for filtering result rows. Mixing them can confuse readers and optimizers.
Important interview questions and answers
- Q: INNER JOIN drops what?
A: Rows with no match on the other side of the join condition. - Q: JOIN vs subquery?
A: Joins often clearer for related columns; subqueries help existence checks and staged logic.
Self-check
- Which customers have no orders in an INNER JOIN result?
- Why use table aliases in multi-table queries?
Challenge
INNER JOIN hands-on
- Seed customers and orders sample data.
- Run the basic INNER JOIN from the lesson.
- Count rows—customers without orders should be absent.
Done when: your join returns only customers who have at least one order.
Interview prep
- INNER JOIN drops?
Rows with no match on the other side.
- ON vs WHERE in joins?
ON defines join match; WHERE filters result rows.