How does left join work?
Introduction to LEFT JOIN
LEFT JOIN is a type of join that returns all records from the left table (clients), and the matched records from the right table (purchases). If there is no match, NULL values are returned for columns from the right table.
Sample Data
Clients Table
client_id | full_name
------------------------
1 | David
2 | Emma
3 | FrankPurchases Table
purchase_id | user_id | item
------------------------------
1 | 1 | Smartphone
2 | 1 | Charger
3 | 2 | HeadphonesSQL Query Example
The query will look like this. Let's break it down in more detail:
sql
SELECT
clients.full_name AS client_name,
purchases.item_name AS item
FROM
clients
LEFT JOIN
purchases
ON
clients.client_id = purchases.client_id;Result of the Query
As a result of executing the query, we will get the following result:
sql
SELECT user_fullname, item_name
FROM users
LEFT JOIN orders ON users.user_id = orders.order_user_id
ORDER BY user_fullname;Conclusion
In summary, the LEFT JOIN operation allows you to retrieve all records from the left table while including matched records from the right table, making it a powerful tool for data retrieval in SQL.
Short Answer
Interview readyPremium
A concise answer to help you respond confidently on this topic during an interview.