🗄️ SQL Q3 / 104

What is the difference between INNER JOIN and LEFT JOIN?

AI-Powered Answer ✓ Answered

SQL JOIN clauses are used to combine rows from two or more tables, based on a related column between them. This document will focus on explaining the fundamental differences and use cases for INNER JOIN and LEFT JOIN.

SQL JOINs Overview

JOINs are a core concept in relational databases, enabling you to retrieve data from multiple tables simultaneously. They establish a relationship between tables based on common columns, typically primary and foreign keys. For our examples, consider two tables: 'Customers' and 'Orders'.

CustomerIDName
1Alice
2Bob
3Charlie
OrderIDCustomerIDAmount
1011150.00
102225.00
103175.00
104450.00

INNER JOIN

An INNER JOIN returns only the rows that have matching values in both tables. If a record in one table does not have a matching record in the other table, it is excluded from the result set. It's the most common type of JOIN and is often implied if you simply use the JOIN keyword without specifying a type.

sql
SELECT C.CustomerID, C.Name, O.OrderID, O.Amount
FROM Customers C
INNER JOIN Orders O ON C.CustomerID = O.CustomerID;

Result of the INNER JOIN on 'Customers' and 'Orders':

CustomerIDNameOrderIDAmount
1Alice101150.00
1Alice10375.00
2Bob10225.00

LEFT JOIN (or LEFT OUTER JOIN)

A LEFT JOIN (also known as LEFT OUTER JOIN) returns all rows from the left table, and the matching rows from the right table. If there is no match for a row in the left table, the columns from the right table will contain NULLs in the result set. It preserves all records from the 'left' table (the first table mentioned in the FROM clause).

sql
SELECT C.CustomerID, C.Name, O.OrderID, O.Amount
FROM Customers C
LEFT JOIN Orders O ON C.CustomerID = O.CustomerID;

Result of the LEFT JOIN on 'Customers' and 'Orders':

CustomerIDNameOrderIDAmount
1Alice101150.00
1Alice10375.00
2Bob10225.00
3CharlieNULLNULL

Key Differences Summarized

  • Matching Rows: INNER JOIN returns only rows where a match exists in both tables. LEFT JOIN returns all rows from the left table, and matched rows from the right table.
  • Unmatched Rows: INNER JOIN excludes unmatched rows from either table. LEFT JOIN includes all rows from the left table, padding columns from the right table with 'NULL's where no match exists.
  • Result Size: The result of an INNER JOIN can be smaller than or equal to the smallest of the two tables. The result of a LEFT JOIN will always have at least as many rows as the left table.

When to Use Which?

Use INNER JOIN when:

  • You only care about records that have a direct relationship in both tables.
  • You want to find all customers who have placed at least one order.
  • You need to combine information only where there's a common data point across both datasets.

Use LEFT JOIN when:

  • You want to retrieve all records from one table (the 'left' table) regardless of whether they have a match in the second table.
  • You want to find all customers, and if they have orders, include order details (otherwise, show 'NULL's for order details).
  • You need to identify records in the left table that *do not* have a match in the right table (often achieved by adding WHERE right_table.id IS NULL).

Conclusion

Choosing between INNER JOIN and LEFT JOIN depends entirely on the specific data you need to retrieve. INNER JOIN is for intersecting data, while LEFT JOIN is for preserving all data from one table while optionally adding related data from another. Understanding their distinct behaviors is crucial for writing accurate and efficient SQL queries.