Junior — Middle
Can you list the different types of table joins in SQL and describe their features?
sobes.tech AI
Answer from AI
In SQL, there are several main types of joins that allow you to combine data from two or more tables:
- INNER JOIN — returns only the rows where there are matches in both tables.
- LEFT JOIN (LEFT OUTER JOIN) — returns all rows from the left table and matching rows from the right; if there are no matches, NULLs will appear in the right table's columns.
- RIGHT JOIN (RIGHT OUTER JOIN) — the opposite, all rows from the right table and matching rows from the left.
- FULL JOIN (FULL OUTER JOIN) — returns all rows from both tables, filling in NULLs where there are no matches.
- CROSS JOIN — Cartesian product, each row from the first table is combined with each row from the second.
Example of INNER JOIN:
SELECT users.name, orders.id
FROM users
INNER JOIN orders ON users.id = orders.user_id;
This query will return users who have orders.