Right join in SQL, DBMS
Right join shows all rows from the right table and shows only matching rows from the left table.
RegistrationNo | Marks |
T4Tutorials1 | 77 |
T4Tutorials2 | 32 |
T4Tutorials4 | 55 |
Table: Exams
Inner join shows results from both tables where there is any match between columns in both tables.
RegistrationNo | Fee |
T4Tutorials1 | 1000 |
T4Tutorials2 | 2000 |
Table: Accounts
Now, we have two different tables. If we want to show the record of both tables in one single table then we can use right join to join both two tables.
Query of right join
SELECT Exams.RegistrationNo, Exams.Marks, Accounts.Fee FROM Exams RIGHT JOIN Accounts ON Exams.RegistrationNo = Accounts.RegistrationNo
In this query Accounts is the right table.
RegistrationNo | Marks | Fee |
T4Tutorials1 | 77 | 1000 |
T4Tutorials2 | 32 | 2000 |
Table: Generated after Right Join
Comparison of Right-join with other joins
Type | Description | Syntax |
Right (Outer) Join | Returns all rows from the right table and the matched rows from the left table. | SELECT * FROM table1 RIGHT JOIN table2 ON table1.column = table2.column; |
Semi Join | Returns only the rows from the left table where there is a match in the right table. | SELECT * FROM table1 WHERE EXISTS (SELECT 1 FROM table2 WHERE table1.column = table2.column); |
Cross Join | Returns the Cartesian product of the two tables, resulting in all possible combinations of rows. | SELECT * FROM table1 CROSS JOIN table2; |
Full (Outer) Join | Returns all rows when there is a match in either the left or right table. | SELECT * FROM table1 FULL JOIN table2 ON table1.column = table2.column; |
Inner Join | Returns only the rows where there is a match in both tables based on the specified condition. | SELECT * FROM table1 INNER JOIN table2 ON table1.column = table2.column; |
Left (Outer) Join | Returns all rows from the left table and the matched rows from the right table. | SELECT * FROM table1 LEFT JOIN table2 ON table1.column = table2.column; |
Self Join | Joins a table with itself, treating it as if it were two separate tables. | SELECT * FROM table1 t1 INNER JOIN table1 t2 ON t1.column = t2.column; |
Test Your Understandings |
1.Right join shows some rows from right table ? YES / NO
2. Right join shows all rows from left table? YES / NO
3. SELECT Exams.Marks, Accounts.Fee FROM Exams Right JOIN Accounts?
In this query, exams are the right table and accounts is the left the table.
Yes/No
Topic Covered
Right join in SQL, DBMS.