INNER JOIN
SQL JOIN
A JOIN combines rows from two or more tables, based on a related column between them. Relational databases split data across tables to avoid duplication; joins put it back together at query time.
Look at a row from the Orders table: it only has CustomerID 4. The customer's name lives in Customers. A join connects them through the shared CustomerID column.
Example
Show each order with the name of the customer who placed it:
SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;Result: 8 rows
| OrderID | CustomerName | OrderDate |
|---|---|---|
| 101 | Aarav Traders | 2024-01-05 |
| 102 | Delta Mart | 2024-01-12 |
| 103 | Blue Harbor Foods | 2024-02-03 |
| 104 | Aarav Traders | 2024-02-18 |
| 105 | Evergreen Grocers | 2024-03-01 |
| 106 | Cedar & Co | 2024-03-09 |
| 107 | Delta Mart | 2024-03-22 |
| 108 | Golden Spoon | 2024-04-02 |
Types of SQL JOINs
| Join | Returns |
|---|---|
INNER JOIN | rows with matching values in both tables |
LEFT (OUTER) JOIN | all rows from the left table, plus matches from the right |
RIGHT (OUTER) JOIN | all rows from the right table, plus matches from the left |
FULL (OUTER) JOIN | all rows from both tables, matched where possible |
CROSS JOIN | every combination of rows (Cartesian product) |
SELF JOIN | a table joined with itself |
NATURAL JOIN | an inner join on all columns with the same name |
Each type has its own module in this level.
The SQL INNER JOIN
INNER JOIN returns only rows that have matching values in both tables. Rows without a match on either side are left out.
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name = table2.column_name;Note: JOIN on its own means INNER JOIN. The INNER keyword is optional.
Example
Example
Every order with the name of the employee who handled it. Order 105 has no employee, so it is not returned:
SELECT o.OrderID, e.FirstName AS HandledBy, o.Amount
FROM Orders o
INNER JOIN Employees e ON o.EmployeeID = e.EmployeeID
ORDER BY o.OrderID;Result: 7 rows
| OrderID | HandledBy | Amount |
|---|---|---|
| 101 | Sara | 245.5 |
| 102 | David | 89.9 |
| 103 | Sara | 412 |
| 104 | David | 56.25 |
| 106 | Sara | 320.75 |
| 107 | David | 27 |
| 108 | Sara | 198.4 |
Unmatched Rows Disappear
Example
There are 8 customers, but only 6 appear: Fjord Supplies and Harbor Lights have no orders.
SELECT DISTINCT c.CustomerName
FROM Customers c
JOIN Orders o ON o.CustomerID = c.CustomerID;Result: 6 rows
| CustomerName |
|---|
| Aarav Traders |
| Delta Mart |
| Blue Harbor Foods |
| Evergreen Grocers |
| Cedar & Co |
| Golden Spoon |
Tip: If you need the customers without orders too, use a LEFT JOIN.
Naming Columns
When both tables have a column with the same name (here CustomerID), you must prefix it with the table name or alias, otherwise the query fails with "ambiguous column name".
SELECT CustomerID FROM Orders JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
-- Error: ambiguous column name: CustomerID
SELECT Orders.CustomerID FROM Orders JOIN Customers ON Orders.CustomerID = Customers.CustomerID;
-- OKChaining Joins
Add one JOIN ... ON per table. Each join connects the new table to one already in the query.
Example
Customer, product and quantity for every order line:
SELECT o.OrderID, c.CustomerName, p.ProductName, od.Quantity
FROM Orders o
JOIN Customers c ON c.CustomerID = o.CustomerID
JOIN OrderDetails od ON od.OrderID = o.OrderID
JOIN Products p ON p.ProductID = od.ProductID
ORDER BY o.OrderID;Result: 12 rows
| OrderID | CustomerName | ProductName | Quantity |
|---|---|---|---|
| 101 | Aarav Traders | Basmati Rice 5kg | 10 |
| 101 | Aarav Traders | Green Tea 100 bags | 5 |
| 102 | Delta Mart | Dark Chocolate | 12 |
| 103 | Blue Harbor Foods | Espresso Beans 1kg | 8 |
| 103 | Blue Harbor Foods | Olive Oil 1L | 20 |
| 104 | Aarav Traders | Green Tea 100 bags | 3 |
| 105 | Evergreen Grocers | Basmati Rice 5kg | 6 |
| 106 | Cedar & Co | Espresso Beans 1kg | 10 |
| 106 | Cedar & Co | Almonds 500g | 4 |
| 107 | Delta Mart | Dark Chocolate | 6 |
| 108 | Golden Spoon | Olive Oil 1L | 9 |
| 108 | Golden Spoon | Green Tea 100 bags | 7 |
Joins with Aggregates
Example
Total quantity each customer bought of each category:
SELECT c.CustomerName, p.Category, SUM(od.Quantity) AS Units
FROM Customers c
JOIN Orders o ON o.CustomerID = c.CustomerID
JOIN OrderDetails od ON od.OrderID = o.OrderID
JOIN Products p ON p.ProductID = od.ProductID
GROUP BY c.CustomerName, p.Category
ORDER BY c.CustomerName, Units DESC;Result: 10 rows
| CustomerName | Category | Units |
|---|---|---|
| Aarav Traders | Grocery | 10 |
| Aarav Traders | Beverages | 8 |
| Blue Harbor Foods | Grocery | 20 |
| Blue Harbor Foods | Beverages | 8 |
| Cedar & Co | Beverages | 10 |
| Cedar & Co | Snacks | 4 |
| Delta Mart | Snacks | 18 |
| Evergreen Grocers | Grocery | 6 |
| Golden Spoon | Grocery | 9 |
| Golden Spoon | Beverages | 7 |
Exercise
Exercise: List each order ID with the full name of the employee who handled it and the customer's country.
Show answer
SELECT o.OrderID, e.FirstName || ' ' || e.LastName AS Employee, c.Country
FROM Orders o
JOIN Employees e ON e.EmployeeID = o.EmployeeID
JOIN Customers c ON c.CustomerID = o.CustomerID;Comma Joins (Old Style)
Before SQL-92, joins were written by listing tables with commas and putting the join condition in WHERE. You will still see this in older code.
Example
Same result as an INNER JOIN:
SELECT o.OrderID, c.CustomerName
FROM Orders o, Customers c
WHERE o.CustomerID = c.CustomerID
AND c.Country = 'India';Result: 4 rows
| OrderID | CustomerName |
|---|---|
| 101 | Aarav Traders |
| 102 | Delta Mart |
| 104 | Aarav Traders |
| 107 | Delta Mart |
Why Explicit JOIN is Better
- Join conditions (
ON) are kept apart from filters (WHERE), so queries are easier to read. - Forgetting the condition in a comma join silently produces a Cartesian product (every row × every row). With
JOINthe missingONis a syntax error. - Outer joins can only be written with the explicit syntax (Oracle's old
(+)notation is deprecated).