Skip to content

JOIN Operations

JOINs are the most powerful—and most misunderstood—feature of SQL. They allow you to reconstruct normalized data into meaningful information.

A JOIN clause is used to combine rows from two or more tables based on a related column between them (usually a Primary Key and a Foreign Key).

We will use two simple tables for our examples:

Table: Customers (C)

IDName
1Alice
2Bob

Table: Orders (O)

OrderIDCustIDAmount
101150.00
102320.00

Returns rows when there is a match in both tables. If a customer hasn’t placed an order, they are excluded. If an order has a non-existent customer, it is excluded.

  • Result: Alice (Order 101).

Returns all rows from the left table, and the matched rows from the right table. If there is no match, the right side results in NULL.

  • Result: Alice (Order 101), Bob (NULL).

The opposite of LEFT JOIN. Returns all rows from the right table.

  • Result: Alice (Order 101), NULL (Order 102).

Returns all rows when there is a match in either left or right table.

  • Result: Alice (Order 101), Bob (NULL), NULL (Order 102).

Returns the Cartesian Product of both tables (every row in A joined with every row in B). No ON clause is used.

  • Result: 2 customers $ imes$ 2 orders = 4 rows.

Task: List every customer and their total order amount. Include customers with zero orders.

Section titled “Task: List every customer and their total order amount. Include customers with zero orders.”
LEFT JOIN with Aggregation
SELECT
C.Name,
SUM(O.Amount) AS TotalSpent
FROM Customers C
LEFT JOIN Orders O ON C.ID = O.CustID
GROUP BY C.Name;
NameTotalSpent
Alice50.00
BobNULL

When you join two tables, the database engine chooses an algorithm to execute the search:

The engine takes one row from Table A and scans Table B for a match. Then it takes the next row from Table A and scans Table B again.

  • Efficiency: $O(N imes M)$.
  • Best for: Small tables or when the join column is indexed.

The engine builds a hash table in memory for the smaller table. Then it scans the larger table and looks up each row in the hash table.

  • Efficiency: $O(N + M)$.
  • Best for: Large, unindexed tables.

Both tables are sorted by the join column, and then the engine “merges” them like a zipper.

  • Best for: Very large tables that are already sorted or indexed.
Join TypeLeft Table (A)Right Table (B)
INNEROnly matchesOnly matches
LEFTAll rowsOnly matches
RIGHTOnly matchesAll rows
FULLAll rowsAll rows
CROSSAll rowsAll rows (Cartesian)