MySQL Tutorial
The MySQL LEFT JOIN returns all the values from the rows of left table.
It also includes the matched values from right table but if there is no matching in both tables, it returns NULL.
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
We've a table named Students and Teachers in our database that contains the following records:
ROLL_NO | STU_NAME | SUBJECT |
---|---|---|
1 | Will | C++ |
2 | SAM | Python |
3 | Rani | Node.js | 4 | Rim | Java |
5 | Micheal | JAVA |
6 | Lara | PHP |
TECH_NO | TECH_NAME | SUBJECT | ROLL_NO |
---|---|---|---|
20 | Kayla | PHP | 6 |
21 | Albert | Java | 4 |
21 | Albert | Java | 5 |
22 | Helen | Node.js | 3 | 23 | Anne | Python | 2 |
24 | Jaime | C++ | 1 |
The following MySQL statement will select all Students, and Teachers they might have:
SELECT Students.NAME, Teachers.TECH_NO
FROM Students
LEFT JOIN Teachers ON
Students.ROLL_NO=Teachers.TECH_NO
ORDER BY Students.NAME;
Now, after executing the above MySQL statement,
STU_NAME | TECH_NO |
---|---|
Lara | 24 |
Micheal | 21 |
Rani | 23 |
Rim | 21 | SAM | 23 |
Will | 24 |