SQL Tutorial

SQL Tutorial SQL Introduction SQL Syntax SQL Data Types SQL OPERATORS SQL COMMANDS SQL Queries

SQL Database

SQL Create Database SQL DROP Database SQL SELECT Database

SQL Table

SQL TABLE SQL CREATE TABLE SQL COPY TABLE SQL ALTER TABLE SQL DELETE SQL TRUNCATE TABLE SQL DROP TABLE SQL UPDATE TABLE SQL INSERT TABLE

SQL SELECT

SQL SELECT Statement SQL SELECT WHERE Clause SQL SELECT IN Operator SQL BETWEEN Operator SQL SELECT BETWEEN Operator SQL SELECT AND Operator SQL SELECT OR Operator SQL SELECT LIKE Operator SQL SELECT DISTINCT SQL SELECT SUM SQL SELECT MAX SQL SELECT MIN SQL SELECT AVG

SQL Clause

SQL WHERE Clause SQL GROUP BY CLAUSE SQL ORDER BY Clause SQL HAVING Clause

SQL INSERT

SQL INSERT Statement SQL INSERT INTO Statement SQL INSERT INTO Values SQL INSERT INTO SELECT SQL Insert multiple rows

SQL JOIN

SQL JOIN SQL Inner Join SQL Left Join SQL Right Join SQL Full Join SQL CROSS Join

SQL OPERATOR

SQL Comparison SQL LOGICAL Operator SQL Cast Operator SQL Arithmetic

Difference

SQL vs NOSQL WHERE vs HAVING DELETE vs DROP GROUP BY vs ORDER BY DROP vs TRUNCATE SQL IN vs SQL EXISTS Difference between Delete, Drop and Truncate in SQL

MISC

SQL SubQuery SQL CASE Commit and Rollback in SQL Pattern Matching in SQL DDL Commands in SQL DML Commands in SQL Types of SQL Commands SQL COUNT SQL Primary Key SQL FOREIGN KEY SET Operators in SQL Check Constraint in SQL SQL EXCEPT SQL VIEW SQL WHERE Statement SQL CRUD Operation Where Condition in SQL TCL Commands in SQL Types of SQL JOINS SQL Nth Highest Salary SQL NOT OPERATOR SQL UNION ALL SQL INTERSECT SQL Data Definition Language SQL Data Manipulation Language SQL Data Control Language SQL CONSTRAINTS SQL Aggregate Operators SQL KEYS Codd’s Rules in SQL What is SQL Injection? Trigger In SQL SQL WHERE Multiple Conditions Truncate function in SQL SQL Formatter WEB SQL SQL Auto Increment Save Point in SQL space() function in SQL SQL Aggregate Functions SQL Topological Sorting SQL Injection SQL Cloning Tables SQL Aliases SQL Handling Duplicate Update Query in SQL Grant Command in SQL SQL SET Keyword SQL Order BY LIMIT SQL Order BY RANDOM

How To

How to use the BETWEEN operator in SQL How To Use INNER JOIN In SQL How to use LIKE in SQL How to use HAVING Clause in SQL How to use GROUP BY Clause in SQL How To Remove Duplicates In SQL How To Delete A Row In SQL How to add column in table in SQL ? How to drop a column in SQL? How to create a database in SQL? How to use COUNT in SQL? How to Create Temporary Table in SQL? How to Add Foreign Key in SQL? How to Add Comments in SQL? How To Use Group By Clause In SQL How To Use Having Clause In SQL How To Delete Column In Table How To Compare Date In SQL How index works in SQL How to calculate age from Date of Birth in SQL How to Rename Column name in SQL What are single row and multiple row subqueries?

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