- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- 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 |
- Related Articles
- Difference Between Left, Right and Full Outer Join
- Difference Between Inner Join and Outer Join in SQL
- Full outer join in PySpark dataframe
- Difference between Inner and Outer join in SQL
- How to do an inner join and outer join of two data frames in R?
- StringJoiner Class vs String.join() Method to Join String in Java
- Python - Merge Pandas DataFrame with Left Outer Join
- Usage and syntax of INNER and OUTER JOIN in DB2
- Python - Merge Pandas DataFrame with Right Outer Join
- How can we distinguish between MySQL CROSS JOIN and INNER JOIN?
- Python - Merge Pandas DataFrame with Outer Join
- Python - Merge Pandas DataFrame with Inner Join
- What MySQL returns if I do not use the keyword ‘RIGHT’ or ‘LEFT’ while writing the query for RIGHT JOIN or LEFT JOIN?
- Compare two tables and return missing ids using MySQL LEFT OUTER JOIN
- How can we convert subqueries to INNER JOIN?
