×

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 ><>=<==, !=LIKEBETWEEN, etc., and Logical Operators are ANDOR, 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 COPY Table

In this tutorial, we will learn how to copy tables in SQL with the help of examples. Using the SELECT INTO statement in the SQL we can copy one table into...

4 minutes read.

How to remove duplicates in SQL

Introduction There are some specific rules that needs to be followed while creating the database objects. To improve the performance of a database, a primary key, clustered and non-clustered indexes, and...

9 minutes read.

Difference between Delete, Drop and Truncate in SQL

What is the Delete command in SQL? In DML (Data Manipulation Language), we use the delete command that allows us to delete the some entries and modify the databases in SQL....

4 minutes read.

SET Operators in SQL

The operator used to join or combine two queries is none other than SET operators. Operators categorized into SET operators are as follows: UNION Operator.UNION ALL’ Operator.INTERSECT Operator.MINUS Operator. Rules to be...

8 minutes read.

SQL UPDATE

SQL UPDATE The SQL UPDATE statement is utilized to update and modify the records present into a database. It is used to change the already existing records stored in the tables...

3 minutes read.

SQL Aliases

SQL Aliases: These are used to give a temporary name for a column or table. These are used to make tables or columns more readable. These are used to rename the table...

2 minutes read.

Commit and Rollback in SQL

In Structured Query Language, we have Data Definition Language (DDL) and Data Manipulation Language (DML) commands the same way we have Transaction Control Language (TCL) commands in the Structured Query...

4 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.

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.

How to compare date in SQL

In this section, we will learn about how dates can be compared in SQL. We can compare any random date with another date stored in a column of a table.This comparison...

4 minutes read.

SQL SELECT OR Operator

This SQL tutorial explains and helps us understand how to use the OR operator in the SELECT query with examples. The OR operator in the Structured Query Language is used to...

3 minutes read.

How to use COUNT in SQL?

How to use COUNT in SQL Introduction COUNT( ) is an aggregate function in SQL.This function counts the number of records in a table if the condition is not specified.If the condition...

4 minutes read.

SQL Aggregate Operators

In SQL, the aggregate operators perform a calculation on multiple rows or multiple values of a single column and give the output a single value. Here, multiple rows of data are...

4 minutes read.

SQL Data Definition Language

Data definition language directly effects on the structure/schema of the database. CREATE, ALTER, DROP are the commands of DDL.CREATE: Creates new database, table, or view of table.ALTER: Modifies the database or...

3 minutes read.

SQL Wildcards

SQL WILDCARD CHARACTERS: SQL wild card character is used to substitute zero or more characters of a string. These characters are used with the “LIKE” operator. Wild Card Characters in SQL: %_[]^- “%” : It is...

3 minutes read.

How to Add Foreign Key in SQL?

How to Add Foreign Key in SQL Foreign key is an attribute or a set of attributes that references to primary key of same table or another table (relation). Foreign key creation along...

4 minutes read.

SQL Handling Duplicate

Removing Duplicates using DISTINCT Keyword: By using the DISTINCT keyword in SQL we can remove duplicate characters from tables or databases. A table contains more duplicate values and duplicate values can cause...

4 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 INSERT INTO Statement

SQL INSERT INTO statement adds data to the newly created tables or existing tables. We can add single records or multiple records in a table by using this query. There are...

3 minutes read.

SQL Commands

SQL commands are classified into four groups on the basis of their nature. DDL (Data Definition Language) DML (Data Manipulation Language) DCL (Data Control Language) DQL (Data Query Language) NOTE: This...

1 minute read.