Junior — Middle
Can you explain the differences between various types of table joins in SQL?
sobes.tech AI
Answer from AI
In SQL, there are different types of joins that allow you to combine rows from two tables based on a specific condition:
- 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 the matching rows from the right; if there are no matches, the columns from the right table will be NULL.
- RIGHT JOIN (RIGHT OUTER JOIN) — the opposite, returns all rows from the right table and the matching rows from the left.
- FULL JOIN (FULL OUTER JOIN) — returns all rows from both tables, with NULLs in columns where there is no match.
Example:
SELECT a.id, b.value
FROM A a
LEFT JOIN B b ON a.id = b.a_id;
Here, all records from table A will be shown, and from B only those that match the condition; the rest will be NULL.
The choice of join type depends on the task and what data needs to be obtained.