SQL LEFT JOIN

SQL LEFT JOIN  returns all rows from the left table even if there are no matches in the right table.

Syntax

SELECT * FROM table_name1 LEFT JOIN table_name2
ON table_name1.column_name=table_name2.column_name

MySQL Table – users

|+—-+——+
| id | name |
+—-+——+
|  1 | AAAA |
|  2 | BBBB |
|  3 | CCCC |
|  4 | DDDD |
|  5 | EEEE |
+—-+——+

MySQL Table – Orders

+—–+—–+——–+
| oid | uid | items  |
+—–+—–+——–+
|   1 |   1 | Pen    |
|   2 |   2 | Watch  |
|   3 |   3 | shoe   |
|   4 |   4 | mobile |
+—–+—–+——–+

Example

mysql> SELECT u.name,o.items FROM users as u LEFT JOIN orders as o ON u.id=o.uid ;
+——+——–+
| name | items  |
+——+——–+
| AAAA | Pen    |
| BBBB | Watch  |
| CCCC | shoe   |
| DDDD | mobile |
| EEEE | NULL   |
+——+——–+
5 rows in set (0.01 sec)