SQL -Structured Query Language - SQL Joins - Right Join Tutorial
Right Join or Right Outer Join is used to retrieve all the records from the right table ( table which is mentioned second – table2) and the records that are matched from the left table( table which is mentioned first – table1).
We can perform the right joins in multiple tables.
The syntax for right joins is-
SELECT column_name1,column_name2,..,column_nameN
FROM
table_1 RIGHT JOIN table_2
ON
table_1.column_name = table_2.column_name;
For example-
SELECT *
FROM
employees
RIGHT JOIN
job_history
WHERE
employees.employee_id = job_history.employee_id;
Better to use AS (Alias) for the long table name-
SELECT e.first_name,e.email,e.salary,jh.start_date,jh.end_date
FROM
employees as e
RIGHT JOIN
job_history as jh
WHERE
e.employee_id = jh.employee_id;
Here we will get all records from the right table (job_history) and only those records where id will get matched in both employees and job_history table.
Another record that is not matched in the left table (table1) will not get retrieved.