Contents
SQL RIGHT JOIN Keyword: Main Tips
- This keyword brings back every row out of the right table into the same rows on the left table.
- The value contained in the first table is NULL where the rows were not matched.
Syntax of SQL RIGHT JOIN
Example
SELECT column_name(s)
FROM tbl1
RIGHT JOIN tbl2 ON tbl1.column_name = tbl2.column_name;
Demo Database
The "Orders" database table below:
ID | Customer_ID | Employee_ID |
---|---|---|
20408 | 2 | 7 |
20409 | 2 | 5 |
85471 | 1 | 3 |
75864 | 5 | 8 |
The "Employees" database table below:
ID | Last_Name | First_Name | Birthdate | Photos | Note |
---|---|---|---|---|---|
1 | Bob | Bobby | 11/18/1965 | EmpID1.pic | Cars are an important thing in my life... |
2 | Bill | Billy | 5/14/1954 | EmpID2.pic | Books are an important thing in my life... |
3 | Ben | Benny | 4/25/1967 | EmpID3.pic | Music is an important thing in my life... |
SQL RIGHT JOIN: Example
The code example below will bring back every data record from employees with all the placed orders:
Example
SELECT Customer_orders.ID, Employees.Last_name, Employees.First_name
FROM Customer_orders
RIGHT JOIN Employees ON Customer_orders.employee_id = Employees.ID
ORDER BY Customer_orders.ID;