
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Inner Join vs Full Outer Join vs Left Join vs Right Join in PostgreSQL
For understanding these concepts with examples, we will consider two tables, marks, and student_info, defined respectively below −
marks −
name | roll_no | perc_marks |
---|---|---|
Siddhi | 45 | 65 |
Yash | 26 | 42 |
Isha | 56 | 87 |
student_info −
name | roll_no | age | gender |
---|---|---|---|
Aniket | 12 | 26 | M |
Isha | 56 | 25 | F |
Yash | 26 | 25 | M |
As you can see, the marks table doesn’t have an entry for Aniket, while the student_info table doesn’t have an entry for Siddhi. In other words, the marks table doesn’t have an entry for roll_no 12, while the student_info table doesn’t have an entry for roll_no 45. Now, let us understand the different JOINS one by one.
INNER JOIN
It returns only those rows for which entries are present in both the tables.
SELECT marks.name, marks.roll_no, student_info.age FROM marks INNER JOIN student_info on marks.roll_no = student_info.roll_no
The output is −
name | roll_no | age |
---|---|---|
Yash | 26 | 25 |
Isha | 56 | 25 |
LEFT JOIN
This will include add entries of the left table, irrespective of whether a corresponding entry exists in the right table or not. In the query below, the marks table is the left table.
SELECT marks.name, marks.roll_no, student_info.age FROM marks LEFT JOIN student_info on marks.roll_no = student_info.roll_no
Thus, the output will contain a row for Siddhi, but not a row for Aniket, as you can see below. Since age is not present for Siddhi, its value will be null.
name | roll_no | age |
---|---|---|
Yash | 26 | 25 |
Siddhi | 45 | [null] |
Isha | 56 | 25 |
RIGHT JOIN
This will include add entries of the right table, irrespective of whether a corresponding entry exists in the left table or not. In the query below, the student_info table is the left table.
SELECT marks.name, marks.roll_no, student_info.age FROM marks RIGHT JOIN student_info on marks.roll_no = student_info.roll_no
Thus, the output will contain a row for Aniket, but not a row for Siddhi. The output of this query will be interesting. Since we are querying the name and roll_no from marks, these values won’t be shown for Aniket. Only the age is being queried from student_info and therefore, only that field will be shown for Aniket’s row. The output is shown below −
name | roll_no | age |
---|---|---|
[null] | [null] | 26 |
Yash | 26 | 25 |
Isha | 56 | 25 |
FULL OUTER JOIN
You can think of full outer joins like a union of LEFT JOIN and RIGHT JOIN. This query will return one row for each unique entry that exists in either the right table or the left table.
SELECT marks.name, marks.roll_no, student_info.age FROM marks FULL OUTER JOIN student_info on marks.roll_no = student_info.roll_no
The output, as expected, contains rows for both Siddhi and Aniket.
name | roll_no | age |
---|---|---|
[null] | [null] | 26 |
Yash | 26 | 25 |
Siddhi | 45 | [null] |
Isha | 56 | 25 |