This join returns all the rows from the left table with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values.
Use this when you only want to return rows that have matching data in the left table, even if there's no matching rows in the right table.
Ex:
Left Table(emp Table)
------------------------------------------------------------------------
empID FirstName LastName UserName
------------------------------------------------------------------------
1 John Miller John
2 Steve Jobs Steve
3 Billgates john Billgates
4 Ozzy Ozzbourne Ozzy
5 Homer Gain Homer
--------------------------------------------------------------------------
Right Table(dept Table)
------------------------------------------------------
empID DeptName
------------------------------------------------------
1 IT
2 Networking
3 Networking
4 Networking
12 Admin
------------------------------------------------------
Query:
SELECT * FROM emp AS e
LEFT JOIN dept AS d
ON e.empid = d.empid
result:
empID FirstName LastName UserName IndividualId AccessLevel
1 John Miller John 1 IT
2 Steve Jobs Steve 2 Networking
3 Billgates john Billgates 3 Networking
4 Ozzy Ozzbourne Ozzy 4 Networking
5 Homer Gain Homer NULL NULL
Use this when you only want to return rows that have matching data in the left table, even if there's no matching rows in the right table.
Ex:
Left Table(emp Table)
------------------------------------------------------------------------
empID FirstName LastName UserName
------------------------------------------------------------------------
1 John Miller John
2 Steve Jobs Steve
3 Billgates john Billgates
4 Ozzy Ozzbourne Ozzy
5 Homer Gain Homer
--------------------------------------------------------------------------
Right Table(dept Table)
------------------------------------------------------
empID DeptName
------------------------------------------------------
1 IT
2 Networking
3 Networking
4 Networking
12 Admin
------------------------------------------------------
Query:
SELECT * FROM emp AS e
LEFT JOIN dept AS d
ON e.empid = d.empid
result:
empID FirstName LastName UserName IndividualId AccessLevel
1 John Miller John 1 IT
2 Steve Jobs Steve 2 Networking
3 Billgates john Billgates 3 Networking
4 Ozzy Ozzbourne Ozzy 4 Networking
5 Homer Gain Homer NULL NULL
No comments:
Post a Comment