When Right Join gives result like Left Join?
Answer:
Left Table
|
SubjectId |
SubjectName |
|
1 |
C Language |
|
2 |
Java Language |
Right Table
|
ChapterId |
ChapterName |
SubjectId |
|
1 |
Introduction C |
1 |
|
2 |
Operator Java |
2 |
|
3 |
Operator python |
3 |
|
4 |
Operator R |
3 |
For both of the cases Result will be like this
|
SubjectId |
SubjectName |
ChapterId |
ChapterName |
|
1 |
C Language |
1 |
Introduction C |
|
2 |
Java Language |
2 |
Operator Java |
Example: Left Join
Code:
SELECT s.SubjectId as SubjectId, s.SubjectName as SubjectName, c.ChapterId as ChapterId, c.ChapterName as ChapterName FROM Subjects as s LEFT JOIN Chapters as c ON s.SubjectId = c.SubjectId
Example: Right Join will give same result as left join
We can say this is as mirror effect.
Code:
SELECT s.SubjectId as SubjectId, s.SubjectName as SubjectName, c.ChapterId as ChapterId, c.ChapterName as ChapterName FROM Chapters as c RIGHT JOIN Subjects as s ON s.SubjectId = c.SubjectId
Example: Right Join will give below result without mirror effect
Code:
SELECT s.SubjectId as SubjectId, s.SubjectName as SubjectName, c.ChapterId as ChapterId, c.ChapterName as ChapterName FROM Subjects as s RIGHT JOIN Chapters as c ON s.SubjectId = c.SubjectId
And the above code will give the below output for only exact right join.
|
SubjectId |
SubjectName |
ChapterId |
ChapterName |
|
1 |
C Language |
1 |
Introduction C |
|
2 |
Java Language |
2 |
Operator Java |
|
NULL |
NULL |
3 |
Operator python |
|
NULL |
NULL |
4 |
Operator R |
Prerequisite Codes
If you don't have the above tables create those table from the below code and let's practice.
Code:
USE SQLExamples DROP TABLE Subjects DROP TABLE Chapters CREATE TABLE Subjects( SubjectId INT, SubjectName VARCHAR(30) ) SELECT * FROM Subjects CREATE TABLE Chapters( ChapterId INT, ChapterName VARCHAR(30), SubjectId int, )
Related Articles:
This section is dedicated exclusively to Questions & Answers. For an in-depth exploration of SQL, click the links and dive deeper into this subject.
Join Our telegram group to ask Questions
Click below button to join our groups.