SQL RIGHT JOIN returns all the rows from the right table even if there are no matches in the left table
Syntax
SELECT * FROM table_name1 RIGHT 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 RIGHT JOIN orders as o ON u.id=o.uid ;
+——+——–+
| name | items |
+——+——–+
| AAAA | Pen |
| BBBB | Watch |
| CCCC | shoe |
| DDDD | mobile |
+——+——–+
4 rows in set (0.00 sec)