LEFT JOIN in SQL
SQL LEFT JOIN tutorial with examples for beginners
The LEFT JOIN is one of the most commonly used SQL joins. It returns all records from the left table, and the matching records from the right table. If no match exists, SQL returns NULL for the right table.
In this beginner-friendly SQL tutorial, you will learn:
What LEFT JOIN is
How LEFT JOIN works
Syntax and examples
LEFT JOIN vs INNER JOIN
Real-world use cases
Best practices
A LEFT JOIN returns:
All rows from the left table
Matching rows from the right table
NULL values when no match exists
LEFT JOIN never removes rows from the left table.
SELECT columns
FROM table1
LEFT JOIN table2
ON table1.column = table2.column;
Here, table1 is the left table, table2 is the right table.
| id | name | city |
|---|---|---|
| 1 | Amit | Delhi |
| 2 | Neha | Mumbai |
| 3 | Rahul | Bengaluru |
| 4 | Sara | Jaipur |
| id | user_id | product |
|---|---|---|
| 1 | 1 | Mobile |
| 2 | 3 | Laptop |
Fetch all users, including those without orders:
SELECT users.name, orders.product
FROM users
LEFT JOIN orders
ON users.id = orders.user_id;
| name | product |
|---|---|
| Amit | Mobile |
| Neha | NULL |
| Rahul | Laptop |
| Sara | NULL |
The NULL values indicate users who have not placed any orders.
Get all users and their orders from Delhi only:
SELECT u.name, o.product
FROM users u
LEFT JOIN orders o
ON u.id = o.user_id
WHERE u.city = 'Delhi';
SELECT u.name, o.product
FROM users u
LEFT JOIN orders o
ON u.id = o.user_id AND o.product = 'Laptop';
This keeps unmatched rows.
WHERE o.product = 'Laptop'
This can convert LEFT JOIN into INNER JOIN.
SELECT u.name, o.product, p.amount
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
LEFT JOIN payments p ON o.id = p.order_id;
| Feature | INNER JOIN | LEFT JOIN |
|---|---|---|
| Returns matching rows only | β | β |
| Keeps unmatched left table rows | β | β |
| Shows NULLs for missing values | β | β |
SELECT u.name, o.product
FROM users u
LEFT JOIN orders o
ON u.id = o.user_id;
SELECT e.name, d.department_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.id;
SELECT s.name, c.course_name
FROM students s
LEFT JOIN courses c
ON s.course_id = c.id;
β Use table aliases to simplify long queries
β Avoid putting right-table conditions in the WHERE clause
β Index joined columns for faster performance
β Use LEFT JOIN when you want all records from the left table
In this tutorial, you learned:
What LEFT JOIN does
How it returns all rows from the left table
How NULL values indicate missing matches
LEFT JOIN vs INNER JOIN differences
Real-world examples and best practices
LEFT JOIN is essential in relational databases where you want complete information, including unmatched records.