TExcelTAD
Home › SQL › SQL INNER JOIN

SQL INNER JOIN

INNER JOIN returns the rows where the join condition finds a match in both tables. Rows without a partner are dropped. It is the most common join — so common that plain JOIN means INNER JOIN.

sql
SELECT columns
FROM table1
INNER JOIN table2 ON table1.column = table2.column;

Products with their category

sql · try it yourself
SELECT p.name AS product, c.name AS category, p.price
FROM products AS p
INNER JOIN categories AS c ON c.category_id = p.category_id
WHERE p.price > 30
ORDER BY p.price DESC;
product                     | category    | price
----------------------------+-------------+------
Noise-Cancelling Headphones | Electronics | 149
Mechanical Keyboard         | Electronics | 89
Coffee Grinder              | Home        | 59.9
Chef Knife                  | Home        | 45
Learn Rust in Practice      | Books       | 39
Cast Iron Pan               | Home        | 38
Python Recipes              | Books       | 34.5
(7 rows)

Who sold what?

Orders with the customer and the employee who handled them — two joins:

sql · try it yourself
SELECT o.order_id,
       c.name AS customer,
       e.first_name || ' ' || e.last_name AS employee,
       o.status
FROM orders AS o
JOIN customers AS c ON c.customer_id = o.customer_id
JOIN employees AS e ON e.employee_id = o.employee_id
WHERE o.order_date >= '2025-10-01'
ORDER BY o.order_id;
order_id | customer      | employee     | status
---------+---------------+--------------+-----------
37       | Felix Reyes   | Aisha Bello  | processing
38       | Kai Moreau    | Owen Price   | processing
39       | Lena Andersen | Rosa Delgado | processing
40       | Arjun Sharma  | Aisha Bello  | processing
(4 rows)

Rows without a match disappear

Products 14 and 27 were never ordered. Joining products with order_items silently drops them:

sql · try it yourself
SELECT COUNT(*) AS products FROM products;

SELECT COUNT(DISTINCT p.product_id) AS products_with_sales
FROM products AS p
JOIN order_items AS oi ON oi.product_id = p.product_id;
products
--------
30
(1 row)

products_with_sales
-------------------
28
(1 row)

That is often exactly what you want ("products that sold"), but if you need all products, use a LEFT JOIN.

Rows can also multiply

If one row matches several rows on the other side, it appears once per match. Order 19 has four lines, so joining orders to items gives four rows for it:

sql · try it yourself
SELECT o.order_id, o.order_date, oi.product_id, oi.quantity
FROM orders AS o
JOIN order_items AS oi ON oi.order_id = o.order_id
WHERE o.order_id = 19
ORDER BY oi.product_id;
order_id | order_date | product_id | quantity
---------+------------+------------+---------
19       | 2025-05-09 | 1          | 3
19       | 2025-05-09 | 5          | 5
19       | 2025-05-09 | 19         | 5
19       | 2025-05-09 | 24         | 4
(4 rows)

Keep this in mind when you aggregate after a join: COUNT(*) then counts order lines, not orders.

Joining with USING

When both tables name the column the same, USING (column) is a short form of the ON condition, and the shared column appears only once in SELECT *:

sql · try it yourself
SELECT categories.name AS category, COUNT(*) AS products
FROM categories
JOIN products USING (category_id)
GROUP BY category_id
ORDER BY products DESC, category;
category    | products
------------+---------
Beverages   | 5
Electronics | 5
Bakery      | 4
Produce     | 4
Books       | 3
Dairy       | 3
Home        | 3
Snacks      | 3
(8 rows)

Both tables also have a name column, so we had to write categories.name. With an unqualified name, the database cannot know which one you mean:

sql · try it yourself
SELECT name FROM categories JOIN products USING (category_id);
Error: ambiguous column name: name

Tip: Always prefix columns with a table alias in multi-table queries (p.name, c.name). It avoids "ambiguous column" errors and tells readers where each value comes from.

Test yourself

What does plain JOIN mean?

  1. LEFT JOIN
  2. INNER JOIN
  3. CROSS JOIN
  4. FULL JOIN

An order with 4 items is inner-joined with order_items. How many rows does that order produce?

  1. 1
  2. 4
  3. 5
  4. 0