Assume that there are two tables Employees and Department.
Employees has employee_id, department_id, name, ......(employee_id is unique for Employees table)
Department has department_id, department_name, employee_id........(department_id is unique for Department table)
Get the Employees as the left table and Department as the right table.
Assume that you want to know the department name of each employees.
So what we can use to find the department name of each employees? LEFT JOIN or RIGHT JOIN?
First you have to know about LEFT JOIN and RIGHT JOIN before use them.
LEFT JOIN
It takes all the rows of left table. If the condition is true then it add right table column values to left table and make a single table. If the condition is false the column values come from right table will be NULL.
RIGHT JOIN
It takes all the rows of right table. If the condition is true then it add left table column values to right table and make a single table. If the condition is false the column values come from left table will be NULL.
For above example we need all the employees and the department name each employee is working
When we consider the Employees table as the left table the we can use LEFT JOIN
example:
SELECT department_name FROM Employees LEFT JOIN Department ON Employees.department_id=Department.department_id;
If there is a department_number in Employees table which is not in Department table then it will return NULL for department_name.
You can use RIGHT JOIN here using Department table as the left table and Employees table as the right table.