In databases, data is stored in multiple tables and it is often necessary sometimes to combine two or more tables to fetch the required data. In MySQL, Joins enable the merging of multiple tables based on the common columns. In this article, we are going to explore MySQL LEFT JOIN which is a type of Outer Join in MySQL. In this article, we will understand everything about the LEFT JOIN we will also perform some queries with multiple Functions for better understanding.
Introduction to MySQL LEFT JOIN
Before understanding the LEFT JOIN make sure that your concepts about the INNER JOIN are clear. MySQL LEFT JOIN is a type of outer join that returns a resulting table that contains all the records from the left table and the matched records from the right table.
In simple words, the data of the 1st table is returned along with the data that is common between the first and the second table. If there is no matched data found then only the data of the left table is returned. Consider an example where we have two tables customer and order.
A LEFT JOIN will allow you to combine these tables ensuring that all the customer’s data appears in the final result of the query even if they haven’t placed any orders yet. The left table is the main priority of the LEFT JOIN along with the matched data between the two tables.

Syntax:
SELECT column1, column2, ...
FROM table1
LEFT JOIN table2 ON table1.column_name = table2.column_name;
Applications of LEFT JOIN in MySQL
- Combines data from multiple tables based on the matched conditions.
- Retrieves all record from left table: The primary purpose of a LEFT JOIN is to retrieve all records from the left (or first) table, regardless of whether there are matching records in the right (or second) table. This ensures that no data is left out in the result set. For example if we have two tables employees and projects and we perform a LEFT JOIN between them then we can find out which are the employees who are not assigned to a project till now.
- Aggregate Data and Fetch Results: Using the GROUP BY Clause along with the LEFT JOIN we can combine data from multiple tables and then fetch the required results. Like we can calculate the count, sum , average of the grouped data formed through the LEFT JOIN.
Examples of MySQL LEFT JOIN
To understand the LEFT JOIN in more depth manner, we need table on which we will perform various operations. So here we taken a 3 table called books, authors and book_authors. If you don’t know How to Create Table in MySQL then refer this.
Structure of authors Table :
CREATE TABLE authors (
author_id INT PRIMARY KEY,
author_name VARCHAR(255)
);
Structure of booksTable:
CREATE TABLE indian_books (
book_id INT PRIMARY KEY,
title VARCHAR(255),
publication_year INT
);
Structure of book_authors Table:
CREATE TABLE book_authors (
book_id INT,
author_id INT,
FOREIGN KEY (book_id) REFERENCES books(book_id),
FOREIGN KEY (author_id) REFERENCES authors(author_id)
);
Here, book_id and author_id which is the PRIMARY KEY of books and authors table acts as a FOREIGN KEY for book_authors table.
After inserting some data into tables, tables looks like:
books Table:

Table-books
authors Table:

Table-authors
book_authors Table:

Relations Between Tables:
This below image show the relations between the all the three tables.

Relation Between the tables
Example 1: MySQL LEFT JOIN with USING Clause
SELECT *
FROM books
LEFT JOIN book_authors USING (book_id);
Output:

Output
Explanation: The following query gets all the records from the left table books and the matching records from the right table book_authors. The query uses the USING Clause which perfroms the left join based on a column which is common to both the tables and has matching records.
Example 2: MySQL LEFT JOIN with GROUP BY Clause
SELECT authors.author_name, COUNT(books.book_id) as book_count
FROM authors
LEFT JOIN book_authors ON authors.author_id = book_authors.author_id
LEFT JOIN books ON book_authors.book_id = books.book_id
GROUP BY authors.author_name;
Output:

Output
Explanation: Group by is used in MYSQL to group the rows that has similar values .This query returns the frequency of books written by each author. It uses LEFT JOIN to join author table and the book_author table and then Aggregates the records using the GROUP BY Clause to calculalte the count of books written by each author. The Aggregate COUNT function is used to calculate the frequency of authors once it is grouped.
Example 3: LEFT JOIN with WHERE Clause
SELECT authors.author_name, books.title
FROM authors
LEFT JOIN book_authors ON authors.author_id = book_authors.author_id
LEFT JOIN books ON book_authors.book_id = books.book_id
WHERE books.publication_year < 2015;
Output:

Output
Explanation: WHERE Clauses is used in MySQL to filter the result set based on a specific condition. This query perfroms a LEFT JOIN between the tables authors and books to return the name of the author and the book he/she has written but only those authors who have published a book before 2015.
Example 4: MySQL LEFT JOIN to Get Unmatched Records
SELECT authors.author_name
FROM authors
LEFT JOIN book_authors ON authors.author_id = book_authors.author_id
WHERE book_authors.author_id IS NULL;
Output:

Output
Explanation: The above query is used to get the unmatched records within the tables For example consider an example where you have two tables customers and orders and you want to find out customers who have never placed an order. In such situations we can use this query to get the unmatched results. It uses a LEFT JOIN and a WHERE Clause to filter out authors with unmatched entries in book_authors. We can see that in our case we don’t have any resultant rows after executing this query which means that in our database every author has published atleast one book that is why the result set is NULL.
Difference Between WHERE & ON Clause in MySQL LEFT JOIN
Although we can achieve same results using the WHERE and ON Clause in MuSQL, but we should use them differently as they are designed for differnet purposes.
- The main purpose of the ON Clause is to specify the join conditions. The ON Clause defines how two or more tables should be joined. The ON Clause defines the common attributes between the two tables which must be matched to perform a join.
- On the other hand the main purpose of WHERE Clause is to filter the resultant table.It defines which rows must be included in the resultant set of the query. It is used to fetch only those recored which fulfiles the specified condition by the user.
Conclusion
In this article we have learned about MySQL LEFT JOIN, it’s syntax and various Clauses to apply with LEFT JOIN. You can use LEFT JOIN whenever the result of the first table is topmost priority along with the matching records of both the tables. However always use Clauses carefully to filter out the results when applying the LEFT JOIN between the tables. Always use ON Clause to specify join conditions and WHERE Clause to filter out the result.
Last Updated :
29 Dec, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...