SQL Right Join – All You Need To Know!

Filed Under: SQL
SQL RIGHT JOIN

Hey, readers! In this article, we will be focusing on SQL Right Join in detail. So, let us begin!

SQL Joins enable the administrator to combine the selected values from different database tables altogether. There are different types of Joins such as:

  • Left Join
  • Right Join
  • Self Join
  • Full Join, etc

Let us now focus on the Understanding on Right Join in the upcoming section.


Working of SQL Right Join

SQL Right Join combines the table in such a manner that it joins all the records of the right table and the data records that matches the left table respectively.

RIGHT JOIN
RIGHT JOIN

The Right Join displays all the records from the right table and includes only those records from the left table which matches the right table records.

Let us now understand the structure of the Right Join.


Syntax of the Right Join

Have a look at the below syntax

SELECT column
FROM table_1
RIGHT JOIN table_2
ON table_1.column-name = table_2.column-name;

Here, table_1 represents the left table while table_2 represents the right table. We use the ON clause to see if the records of the right table matches the records of the left table.

Further SQL Right Join returns the following values–

  • All the records of the right table.
  • All the records of the left table that matches the right table.
  • Returns NULL for the records of the left table which does not matches the right table.

Implementing SQL Right Join

Let us now implement the working of SQL Right Join through examples. Here, we have created two tables — ‘Stud_Info’ and ‘Stud_score’ as follows–

create table Stud_Info(stud_id integer, Name varchar(200), sub varchar(200));

insert into Stud_Info(stud_id, Name, sub) values(001, 'John','Maths');

insert into Stud_Info(stud_id, Name, sub) values(002, 'Mary','Science');

insert into Stud_Info(stud_id, Name, sub) values(003, 'Ray','Social Science');

insert into Stud_Info(stud_id, Name, sub) values(004, 'Jim','History');

insert into Stud_Info(stud_id, Name, sub) values(005, 'Rick','Geo');

create table Stud_score(stud_id integer, Score Integer);
insert into Stud_score(stud_id, score) values(001, 25);
insert into Stud_score(stud_id, score) values(002, 45);
insert into Stud_score(stud_id, score) values(005, 46);
insert into Stud_score(stud_id, score) values(004, 68);
insert into Stud_score(stud_id, score) values(007, 100);

Let us have a look at both of the tables:

Table_1:Stud_Info
Table_1:Stud_Info
Table_2:Stud_score
Table_2:Stud_score
SELECT Stud_Info.stud_id, Stud_Info.Name, Stud_score.score
FROM Stud_Info
RIGHT JOIN Stud_score ON Stud_Info.stud_id = Stud_score.stud_id
ORDER BY Stud_Info.stud_id;

As seen it displays all the values from the right table and selects and presents only those values from the left table(Stud_Info) that matches to the right table(Stud_score). If no value matches, it represents those row values as NULL.

Output:

SQL RIGHT JOIN
SQL RIGHT JOIN

Conclusion

By this, we have come to the end of this topic. Feel free to comment below, in case you come across any question.

For more such posts related to SQL, do visit SQL with JournalDev.

Till then, Happy Learning!!


References

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages