What are the different types of joins in SQL?
EasyConceptSQL: Joins
Answer
A join combines rows from two tables using a condition, usually equality between a foreign key and a primary key. The join type decides what happens to rows that have no match.
- INNER JOIN: only the rows that match in both tables.
- LEFT (OUTER) JOIN: all rows of the left table; unmatched rows get
NULLin the right table's columns. RIGHT JOIN is the mirror image. - FULL OUTER JOIN: all rows from both sides. MySQL does not support it; emulate it with a
UNIONof a left and a right join. - CROSS JOIN: every combination of rows (m × n rows). A self join joins a table with itself using two aliases.
Example:
students(id, name) marks(student_id, score)
1 | Asha 1 | 90
2 | Ravi 3 | 75
Joining on id = student_id:
INNER JOIN: Asha | 90LEFT JOIN: Asha | 90, Ravi | NULLRIGHT JOIN: Asha | 90, NULL | 75FULL OUTER JOIN: all three rows aboveCROSS JOIN: 2 × 2 = 4 rows