×

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If it doesn’t find similar records in the left table, it returns the NULL keyword in the right table row.

Syntax of the SQL Right Join

SELECT Column_Name1, Column_Name2, Column_Name3, Column_Name4, Column_Name5 FROM Table_1 Right JOIN Table_2 ON Expression;  

Column_Name1, Column_Name2, Column_Name3, Column_Name4 columns from both the table.

Right Table: Table_2, Left Table: Table1.

Expression: Condition expression with ON clause.

This conditional expression will be a comparison condition of comparison operators and logical operators of SQL. Remember that Comparison Operators are >, <, >=, <=, =, !=, LIKE, BETWEEN, etc., and Logical Operators are AND, OR, and NOT.

There is One too Many relationships between the Student and the Department table. One to Many relationships means that one Department can be allocated to one Student or more than one Student. When we execute the SQL Right Join query on these tables on Students_Id and Department_Id, all the records of the student table are displayed in the result set with the similar records in the Department records.

Department is not allocated to any students in the result set with the NULL keyword from the left side table.  

Example of Right Join in Structured Query Language:

Table Number 1: Diploma_Student

Student_IdStudent_NameFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_Id
202111Vaishnavi Patil949188859592911
202112Vaibhav Lokhande859092808582862
202113Yash Dhull908894878590893
202114Sonali Patole959092889290914
202115Axar Patel858082869284851
202116Meena Mishra787580748577783
202117Mahesh Kumbhar758075788076775
202118Sakashi Patil807874788077782
202119Sopan Bhore706875758080752
202220Prajwal Lokhande808585757880814
202221Anuja Wanare858886828485855
202222Venkatesh Iyer908987909291903
202223Anushka Sen707571748078751
202224Aakash Jain807572748580784
202225Akshay Agarwal858078889082845

Table Number 2: Department

Department_IdDepartment_Name
1Computer Engineering
2Information Technology
3Mechanical Engineering
4Automobile Engineering
5Civil Engineering
6Electrical Engineering
7Electronics and Telecommunication Engineering
8Chemical Engineering

Example 1: Execute a right join query on the Diploma_Student and Department table name.

SELECT Student_Id, First_Sem, Second_Sem, Third_Sem, Fourth_Sem, Fifth_Sem, Sixth_Sem, Total, D.Department_Id, Department_Name FROM Diploma_Student DS RIGHT JOIN Department D ON DS.Department_Id = D.Department_Id;

The above Right join query joins the Diploma_Student table and Department table and retrieves the data from the tables where DS.Department_Id = D.Department_Id.

The output of the above query is as follows:

Student_IdFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_IdDepartment_Name
202111949188859592911Computer Engineering
202115858082869284851Computer Engineering
202223707571748078751Computer Engineering
202112859092808582862Information Technology
202118807874788077782Information Technology
202119706875758080752Information Technology
202113908894878590893Mechanical Engineering
202116787580748577783Mechanical Engineering
202222908987909291903Mechanical Engineering
202114959092889290914Automobile Engineering
202220808585757880814Automobile Engineering
202224807572748580784Automobile Engineering
202117758075788076775Civil Engineering
202221858886828485855Civil Engineering
202225858078889082845Civil Engineering
NULLNULLNULLNULLNULLNULLNULLNULL6Electrical Engineering
NULLNULLNULLNULLNULLNULLNULLNULL7Electronics and Telecommunication Engineering
NULLNULLNULLNULLNULLNULLNULLNULL8Chemical Engineering
SQL Right Join

Example 2: Execute a right join query on the Diploma_Student and Department table name using the WHERE Clause.

SELECT Student_Id, First_Sem, Second_Sem, Third_Sem, Fourth_Sem, Fifth_Sem, Sixth_Sem, Total, D.Department_Id, Department_Name FROM Diploma_Student DS RIGHT JOIN Department D ON DS.Department_Id = D.Department_Id WHERE D.Department_Id IN (1, 3, 4, 6, 8);

The above Right join query joins the Diploma_Student table and Department table and retrieves the data from the tables where DS.Department_Id = D.Department_Id. The Student records show where department id is 1, 3, 4, 6, and 8. We have applied the WHERE clause condition on the Department id of the Department table.

The output of the above query is as follows:

Student_IdFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_IdDepartment_Name
202111949188859592911Computer Engineering
202115858082869284851Computer Engineering
202223707571748078751Computer Engineering
202113908894878590893Mechanical Engineering
202116787580748577783Mechanical Engineering
202222908987909291903Mechanical Engineering
202114959092889290914Automobile Engineering
202220808585757880814Automobile Engineering
202224807572748580784Automobile Engineering
NULLNULLNULLNULLNULLNULLNULLNULL6Electrical Engineering
NULLNULLNULLNULLNULLNULLNULLNULL8Chemical Engineering
SQL Right Join

Example 3: Execute a right join query on the Diploma_Student and Department table name using the ORDER BY Clause.

SELECT Student_Id, First_Sem, Second_Sem, Third_Sem, Fourth_Sem, Fifth_Sem, Sixth_Sem, Total, D.Department_Id, Department_Name FROM Diploma_Student DS RIGHT JOIN Department D ON DS.Department_Id = D.Department_Id ORDER BY D.Department_Id;

The above Right join query joins the Diploma_Student table and Department table and retrieves the data from the tables where DS.Department_Id = D.Department_Id, and the Student's records will display in the ascending order as we have used the ORDER BY clause in the query.

The output of the above query is as follows:

Student_IdFirst_SemSecond_SemThird_SemFourth_SemFifth_SemSixth_SemTotalDepartment_IdDepartment_Name
202111949188859592911Computer Engineering
202115858082869284851Computer Engineering
202223707571748078751Computer Engineering
202112859092808582862Information Technology
202118807874788077782Information Technology
202119706875758080752Information Technology
202113908894878590893Mechanical Engineering
202116787580748577783Mechanical Engineering
202222908987909291903Mechanical Engineering
202114959092889290914Automobile Engineering
202220808585757880814Automobile Engineering
202224807572748580784Automobile Engineering
202117758075788076775Civil Engineering
202221858886828485855Civil Engineering
202225858078889082845Civil Engineering
NULLNULLNULLNULLNULLNULLNULLNULL6Electrical Engineering
NULLNULLNULLNULLNULLNULLNULLNULL7Electronics and Telecommunication Engineering
NULLNULLNULLNULLNULLNULLNULLNULL8Chemical Engineering
SQL Right Join

Related Topics

SQL Data Manipulation Language

Data Manipulation Language manipulates/make changes in data present in a table. It only affects data/records of table, not on the schema/structure of table. INSERT, UPDATE, DELETE are the commands of DML. INSERT: Stores...

2 minutes read.

Where Condition in SQL

WHERE clause is used with the Data Manipulation Language Command in SQL, the Data Manipulation Language commands are the SELECT statement, UPDATE statement, and DELETE statement. WHERE clause condition is an...

5 minutes read.

How to Create Temporary Table in SQL?

How to Create Temporary Table in SQL  Introduction to Temporary Tables Temporary table is a table which is used to store temporary data that can be used further in the same client...

3 minutes read.

SQL IN vs SQL EXISTS

SQL IN vs SQL EXISTS This article discusses in detail about the IN and the EXISTS operators in SQL. It is a common question between developers that what is the difference...

3 minutes read.

SQL TABLE

SQL TABLE Structured Query Language (SQL) is a relational database (RDBMS) where data is stored in the form of tables, that is, in rows and columns. These tables are known as...

3 minutes read.

SQL SELECT MAX

The SQL Max() function is an aggregate function in SQL. This function returns the values which are greater in the condition. The condition may be a number, or it may...

5 minutes read.

SQL SELECT LIKE Operator

The SQL SELECT LIKE Operator tutorial helps us understand how to use the LIKE operator in the SELECT query with examples. The SQL SELECT LIKE Operator retrieves the records from the...

4 minutes read.

SQL Formatter

As a beginner, one does not focus much on Formatting the queries while writing their code. This leads to a great confusion while referring the code in future. For a...

5 minutes read.

Difference between SQL and NoSQL

SQL vs. NoSQL | Difference between SQL and NoSQL Choosing a database is the most fundamental decision that needs to be decided before starting a task. Relational and non-relational databases are...

3 minutes read.

How to use HAVING clause in SQL

In this article, we will learn about the HAVING clause concept and how to use it in SQL. What is the HAVING clause? In Structured Query Language, HAVING Clause used with GROUP...

7 minutes read.

How to add column in table in SQL ?

How to add column in table in SQL  Introduction To add a column in already created table, one needs to use the ALTER command along with the ADD clause.If in the query,...

7 minutes read.

SQL Comparision Operator

The Comparison Operator compares different data of the Structured Query Language table and checks whether the data are the same, less than, greater than, less than, or greater than equal....

14 minutes read.

SQL LOGICAL Operator

In this tutorial, we will understand the operator who falls under the logical operator in SQL with the help of examples. The SQL Logical Operator displays the query result in one...

5 minutes read.

SQL Tutorial for Beginners

SQL tutorial provides basic and advanced concepts of Structured Query Language and how you deploy SQL to work with a relational database system. Our SQL tutorial is designed for beginners...

3 minutes read.

SQL Left Join

The SQL Left Join query displays all the records from the table and displays similar records from the right table. The query displays zero records if it doesn’t find any...

4 minutes read.

How to use GROUP BY clause in SQL

In this SQL article, we will learn about the GROUP BY clause and how to use it in SQL. We will also discuss using the GROUP BY clause with the...

6 minutes read.

SQL SELECT Statement

The SQL SELECT statement is used to retrieve the data from the tables. We can also retrieve the selected records from the table using the query condition. The SQL SELECT...

5 minutes read.

How to delete column in table

Introduction In SQL, sometimes it is required to delete a column of a table.The use of ALTER TABLE command with DROP COLUMN clause will serve the purpose to delete/remove a column...

4 minutes read.

SQL Inner Join

In Structured Query Language, the most used join query is the Inner join query. Inner join query retrieves the records from one or more tables with similar data or records. The...

4 minutes read.

SQL Right Join

The SQL Right Join query displays all the table records and similar records from the left table. The query display zero records if it doesn’t find any similar records. If...

4 minutes read.