Why outer joins are needed in SQL - Performance Analysis
We want to understand how the time to run a query changes when using outer joins.
Specifically, we ask: How does adding an outer join affect the work the database does?
Analyze the time complexity of this SQL query using a LEFT OUTER JOIN.
SELECT a.id, a.name, b.order_date
FROM customers a
LEFT OUTER JOIN orders b ON a.id = b.customer_id;
This query lists all customers and their orders, including customers with no orders.
Look for repeated steps the database does to combine data.
- Primary operation: For each customer, find matching orders.
- How many times: Once for every customer row.
As the number of customers grows, the database checks orders for each one.
| Input Size (customers) | Approx. Operations |
|---|---|
| 10 | Checks orders for 10 customers |
| 100 | Checks orders for 100 customers |
| 1000 | Checks orders for 1000 customers |
Pattern observation: The work grows roughly proportional to the number of customers times the number of orders.
Time Complexity: O(n * m)
This means the time grows with the number of customers times the number of orders, since each customer may match many orders.
[X] Wrong: "Outer joins are just like inner joins, so they take the same time."
[OK] Correct: Outer joins must keep all rows from one table even if no match exists, so the database does extra work to include unmatched rows.
Understanding how outer joins affect query time helps you explain real database behavior clearly and confidently.
What if we changed the LEFT OUTER JOIN to a FULL OUTER JOIN? How would the time complexity change?