Tutorials

SQL JOIN


SQL RIGHT JOIN


In Sql, Right JOIN selects all the records from the right column (table 2) and only those records from the left table that satifies the given condition. 

If there is no match in the left table for the given condition then result will be NULL for those columns.

 

Diagrammatic Explanation

right join by tutorialsinhand

 

Syntax of right join


SELECT column1, column2,...,columnN 
FROM table1 
RIGHT JOIN table2 
ON table1.column = table2.column;

We can select column from both the tables by specifying it as table1.column1, table1.column2,....., table2.column1, table2.column2 and so on in above query. See the example section for more clarity.

 

Example on right join

We will take two tables tih_Courses and Registration as shown below:

tih_Courses
Course_ID Course_Name Professor Mode_of_Training
001 .Net Mukesh Tiwari Online and Offline
002 Java Rajneesh Shukla Online and Offline
003 ANSI SQL Rajneesh Shukla Online
004 Basics of Computer Shweta  Online


Registration
Course_ID Course_Name Registered_Students Total_Strength
001 .Net 107 120
002 Java 118 130
009 Oracle 100 110
010 Ruby 80 90
Query-1:Select all the course name and registered student to the respective courses.
SELECT tih_Courses.Course_Name, Registration.Registered_Students 
FROM tih_Courses 
RIGHT JOIN Registration 
ON tih_Courses.Course_ID = Registration.Course_ID;

On executing above query successfully, we get:

Result
tih_Courses.Course_ID tih_Courses.Course_Name Registration.Registered_Students
001 .Net 107
002 Java 118
NULL NULL 100
NULL NULL 80

All the rows from the table 2 (Registration ) is returned and from the table 1 (tih_Courses) only those matching condition is returned. 

For registered student in Registration table 100 and 80 we donot have any matching course id in table tih_Courses so they will have NULL value.

 

 

 



Please Share this page
Views : 8