Open In App
Related Articles

SQL Right Join

Improve Article
Improve
Save Article
Save
Like Article
Like

The RIGHT JOIN keyword in SQL returns the all matching records(or rows) and the records(or rows) which are present in the right table but not in the left table .That means that, if a certain row is present in the right table but not in the left, the result will include this row but with a NULL value in each column from the left . If a record from the left table is not in the right, it will not be included in the result.

RIGHT JOIN

The syntax for a RIGHT JOIN is :-

SELECT column_name(s) 
FROM tableA
RIGHT JOIN tableB ON tableA.column_name = tableB.column_name;

SQL RIGHT JOIN EXAMPLE  :

In this example we will consider two tables employee table containing details of the employees working in the particular department the and department table containing the details of the department

employee table :

emp_no emp_name dept_no

E1

Varun Singhal

D1

E2

Amrita Aggarwal

D2

E3

Ravi Anand

D3

department table :

dept_no d_name location

D1

IT

Delhi

D2

HR

Hyderabad

D3

Finance

Pune

D4

Testing

Noida

D5

Marketing

Mathura

To perform right- join on these two tables we will use the following SQL query:

select emp_no , emp_name ,d_name, location 
from employee
right join dept on employee.dept_no = department.dept_no;

The output that we will get is as follows : 

emp_no

emp_name

d_name

location

E1

Varun Singhal

IT

Delhi

E2

Amrita Aggarwal

HR

Hyderabad

E3

Ravi Anand

Finance

Pune

[NULL]

[NULL]

Testing

Noida

[NULL]

[NULL]

Marketing

Mathura

As right join gives the matching rows and the rows that are present in the right table but not in the left table. Here in this example, we see that the department that contains no employee contains [NULL] values of emp_no and emp_name after performing the right join.

Unlock the Power of Placement Preparation!
Feeling lost in OS, DBMS, CN, SQL, and DSA chaos? Our Complete Interview Preparation Course is the ultimate guide to conquer placements. Trusted by over 100,000+ geeks, this course is your roadmap to interview triumph.
Ready to dive in? Explore our Free Demo Content and join our Complete Interview Preparation course.

Last Updated : 19 Jul, 2023
Like Article
Save Article
Previous
Next
Similar Reads