SQL RIGHT JOIN Keyword
Submitted by admin on Wednesday, March 23, 2011 - 21:41.
The RIGHT JOIN keyword works the same way as LEFT JOIN keyword except that it returns all rows from the right table. This means that even there is no matching record from the left join the rows will still show from the second table.
SQL RIGHT JOIN Syntax
SELECT column_name(s) FROM First_table_name RIGHT JOIN Second_table_name ON First_table_name.column_name = Second_table_name.column_name
Consider the following table for this exercise
Users
Firstname | Lastname | Salary | DeptID |
---|---|---|---|
John | Smith | 1000 | 1 |
Mathew | Simon | 3000 | 1 |
Bill | Steve | 2200 | 1 |
Amanda | Rogers | 1800 | 2 |
Steve | Hills | 2800 | 2 |
Steve | jobs | 2400 | 2 |
bill | cosby | 700 | 3 |
Departments
DeptID | DepartmentName |
---|---|
1 | Employee |
2 | Staff |
3 | Management |
4 | HR |
Example # 1
SELECT Firstname, Lastname, Salary, DepartmentName FROM Users RIGHT JOIN Departments ON Users.DeptID = Departments.DeptID
Result of the Query
Firstname | Firstname | Salary | DepartmentName |
---|---|---|---|
John | Smith | 1000 | Employee |
Mathew | Simon | 3000 | Employee |
Bill | Steve | 2200 | Employee |
Amanda | Rogers | 1800 | Staff |
Steve | Hills | 2800 | Staff |
Steve | jobs | 2400 | Staff |
bill | cosby | 700 | Management |
HR |
We have joined the two tables, and in the WHERE clause we are matching the primary keys and the foriegn keys from both tables.
Add new comment
- 75 views