SQL LEFT JOIN Keyword
- Previous Page SQL Inner Join
- Next Page SQL Right Join
SQL LEFT JOIN Keyword
The LEFT JOIN keyword returns all rows from the left table (table_name1), even if there are no matching rows in the right table (table_name2).
LEFT JOIN Keyword Syntax
SELECT column_name(s) FROM table_name1 LEFT JOIN table_name2 ON table_name1.column_name=table_name2.column_name
Note:In some databases, LEFT JOIN is called LEFT OUTER JOIN.
Original Table (used in the example):
"Persons" Table:
Id_P | LastName | FirstName | Address | City |
---|---|---|---|---|
1 | Adams | John | Oxford Street | London |
2 | Bush | George | Fifth Avenue | New York |
3 | Carter | Thomas | Changan Street | Beijing |
"Orders" Table:
Id_O | OrderNo | Id_P |
---|---|---|
1 | 77895 | 3 |
2 | 44678 | 3 |
3 | 22456 | 1 |
4 | 24562 | 1 |
5 | 34764 | 65 |
LEFT JOIN (LEFT JOIN) Example
Now, we want to list all people, as well as their orders - if any.
You can use the following SELECT statement:
SELECT Persons.LastName, Persons.FirstName, Orders.OrderNo FROM Persons LEFT JOIN Orders ON Persons.Id_P=Orders.Id_P ORDER BY Persons.LastName
Result Set:
LastName | FirstName | OrderNo |
---|---|---|
Adams | John | 22456 |
Adams | John | 24562 |
Carter | Thomas | 77895 |
Carter | Thomas | 44678 |
Bush | George |
The LEFT JOIN keyword will return all rows from the left table (Persons), even if there are no matching rows in the right table (Orders).
- Previous Page SQL Inner Join
- Next Page SQL Right Join