SQL LEFT JOIN

Understanding SQL LEFT JOIN with Examples

Understanding SQL LEFT JOIN with Examples

Interested in above project ,Click Below
WhatsApp
Telegram
LinkedIn

SQL LEFT JOIN

Multiple tables can be combined into a single result set in SQL using the JOIN method, which is based on a relevant column. Among the various types of joins, the LEFT JOIN is particularly useful when you want to retrieve all records from one table while fetching matching records from another table where available.

Only the matching records from the right table are included when an LEFT JOIN is performed; all data from the left table are returned. In the event that no match is discovered, NULL will appear in the right table’s columns.

Complete Python Course with Advance topics:-Click Here
SQL Tutorial :-Click Here

SQL LEFT JOIN Syntax

The general syntax for performing a LEFT JOIN operation in SQL is:

SELECT Table1.Column1, Table2.Column2
FROM Table1
LEFT JOIN Table2
ON Table1.CommonColumn = Table2.CommonColumn;

Let’s now take a closer look into LEFT JOIN using actual MySQL database samples.

Example 1: Employee and Department Tables

Consider two tables: staff and unit.

Staff Table

StaffIDStaff_NameSalary
101Rahul Sharma70000
102Neha Verma52000
103Manish Yadav48000
104Priya Kapoor45000
105Karan Joshi62000
106Anjali Rathi55000
107Aman Taneja30000
108Simran Kaur58000
109Sahil Arora40000
110Nitin Mehta31000

Unit Table

UnitIDUnit_NameStaff_ID
1HR101
2IT103
3Finance104
4Sales105
5R&D107
6Marketing109
7Support110

Query to Perform LEFT JOIN:

SELECT s.StaffID, s.Staff_Name, s.Salary, u.UnitID, u.Unit_Name
FROM staff s
LEFT JOIN unit u
ON s.StaffID = u.Staff_ID;

Output:

StaffIDStaff_NameSalaryUnitIDUnit_Name
101Rahul Sharma700001HR
102Neha Verma52000NULLNULL
103Manish Yadav480002IT
104Priya Kapoor450003Finance
105Karan Joshi620004Sales
106Anjali Rathi55000NULLNULL
107Aman Taneja300005R&D
108Simran Kaur58000NULLNULL
109Sahil Arora400006Marketing
110Nitin Mehta310007Support

Example 2: Loans and Customers

Consider two tables: credit and client.

See also  SQL SELECT from Multiple Tables

Credit Table

LoanIDBranchAmount
1B120000
2B215000
3B350000
4B4120000
5B580000
6B635000

Client Table

ClientIDClient_NameLoanID
1Rohan Kapoor1
2Meera Singh4
3Tanya Verma5
4Rakesh Mehta2

Query to Perform LEFT JOIN:

SELECT c.LoanID, c.Branch, c.Amount, cl.ClientID, cl.Client_Name
FROM credit c
LEFT JOIN client cl
ON c.LoanID = cl.LoanID;

Output:

LoanIDBranchAmountClientIDClient_Name
1B1200001Rohan Kapoor
2B2150004Rakesh Mehta
3B350000NULLNULL
4B41200002Meera Singh
5B5800003Tanya Verma
6B635000NULLNULL

Example 3: Customers and Orders

Consider two tables: buyers and purchases.

Buyers Table

BuyerIDBuyer_NameAgeSalary
1Aditya Roy3560000
2Priyanka Yadav2848000
3Raj Malhotra3075000
4Pooja Sharma2954000

Purchases Table

PurchaseIDPurchase_DateBuyerIDAmount
1012022-01-1024500
1022022-02-1537000

Query to Perform LEFT JOIN:

SELECT b.BuyerID, b.Buyer_Name, b.Age, b.Salary, p.PurchaseID, p.Purchase_Date, p.Amount
FROM buyers b
LEFT JOIN purchases p
ON b.BuyerID = p.BuyerID;

Output:

BuyerIDBuyer_NameAgeSalaryPurchaseIDPurchase_DateAmount
1Aditya Roy3560000NULLNULLNULL
2Priyanka Yadav28480001012022-01-104500
3Raj Malhotra30750001022022-02-157000
4Pooja Sharma2954000NULLNULLNULL

Download New Real Time Projects :-Click here
Complete Advance AI topics:- CLICK HERE

By using LEFT JOIN, you can efficiently combine data from multiple tables while ensuring that all records from the left table are included, even if no corresponding data exists in the right table.

See also  Understanding SQL Injection: A Major Web Security Threat

For more such SQL tutorials, keep exploring UpdateGadh!


sql left join example
sql left join multiple tables
sql join
sql left join
sql left join example
linq to sql left join
sql left join vs right join
sql left join vs left outer join
sql left join on multiple columns
sql left outer join
sql left join vs right join
right join in sql
inner join in sql
cross join in sql

🎓 Need Complete Final Year Project?

Get Source Code + Report + PPT + Viva Questions (Instant Access)

🛒 Visit UpdateGadh Store →
💬 Chat Now