Junior — Middle
What types of joins are applicable in SQL and what are 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 records that have matches 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, the right table fields will be NULL.
- RIGHT JOIN (RIGHT OUTER JOIN) — conversely, returns all records from the right table and matching ones from the left.
- FULL JOIN (FULL OUTER JOIN) — returns all records from both tables, filling NULL where there are no matches.
- CROSS JOIN — the Cartesian product of two tables, returning all possible combinations of rows.
Features:
- INNER JOIN is used for selecting related data.
- OUTER JOINs are useful when you want to keep all data from one or both tables, even if there are no matches.
- CROSS JOIN is rarely used because it can lead to a very large number of results.
Example of INNER JOIN:
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;