Junior — Middle
What are the main types of table joins in SQL and their differences?
sobes.tech AI
Answer from AI
Main types of JOINs in SQL and their differences:
- INNER JOIN — returns only the records that match the join condition in both tables.
- LEFT JOIN (LEFT OUTER JOIN) — returns all records from the left table and matching records from the right. If there are no matches, NULLs are shown for the right table.
- RIGHT JOIN (RIGHT OUTER JOIN) — returns all records from the right table and matching records from the left. If there are no matches, NULLs are shown for the left table.
- FULL JOIN (FULL OUTER JOIN) — returns all records from both tables, with NULLs in place where there are no matches.
- CROSS JOIN — Cartesian product of two tables, returning all possible combinations of rows.
Example of INNER JOIN:
SELECT users.name, orders.id
FROM users
INNER JOIN orders ON users.id = orders.user_id;