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

OrderIDCustomerNameOrderDate
101Aarav Traders2024-01-05
102Delta Mart2024-01-12
103Blue Harbor Foods2024-02-03
104Aarav Traders2024-02-18
105Evergreen Grocers2024-03-01
106Cedar & Co2024-03-09
107Delta Mart2024-03-22
108Golden Spoon2024-04-02

Types of SQL JOINs

JoinReturns
INNER JOINrows with matching values in both tables
LEFT (OUTER) JOINall rows from the left table, plus matches from the right
RIGHT (OUTER) JOINall rows from the right table, plus matches from the left
FULL (OUTER) JOINall rows from both tables, matched where possible
CROSS JOINevery combination of rows (Cartesian product)
SELF JOINa table joined with itself
NATURAL JOINan 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

OrderIDHandledByAmount
101Sara245.5
102David89.9
103Sara412
104David56.25
106Sara320.75
107David27
108Sara198.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;
-- OK

Chaining 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

OrderIDCustomerNameProductNameQuantity
101Aarav TradersBasmati Rice 5kg10
101Aarav TradersGreen Tea 100 bags5
102Delta MartDark Chocolate12
103Blue Harbor FoodsEspresso Beans 1kg8
103Blue Harbor FoodsOlive Oil 1L20
104Aarav TradersGreen Tea 100 bags3
105Evergreen GrocersBasmati Rice 5kg6
106Cedar & CoEspresso Beans 1kg10
106Cedar & CoAlmonds 500g4
107Delta MartDark Chocolate6
108Golden SpoonOlive Oil 1L9
108Golden SpoonGreen Tea 100 bags7

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

CustomerNameCategoryUnits
Aarav TradersGrocery10
Aarav TradersBeverages8
Blue Harbor FoodsGrocery20
Blue Harbor FoodsBeverages8
Cedar & CoBeverages10
Cedar & CoSnacks4
Delta MartSnacks18
Evergreen GrocersGrocery6
Golden SpoonGrocery9
Golden SpoonBeverages7

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

OrderIDCustomerName
101Aarav Traders
102Delta Mart
104Aarav Traders
107Delta 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 JOIN the missing ON is a syntax error.
  • Outer joins can only be written with the explicit syntax (Oracle's old (+) notation is deprecated).