Junior — Middle
Can you list the main types of table joins in SQL and explain their usage?
sobes.tech AI
Answer from AI
Main types of joins in SQL:
- 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 used for the right table.
- RIGHT JOIN (RIGHT OUTER JOIN) — returns all records from the right table and matching records from the left.
- FULL JOIN (FULL OUTER JOIN) — returns all records from both tables, matching and non-matching, with NULLs where there is no match.
- CROSS JOIN — Cartesian product of two tables, returning all possible combinations of rows.
Example of using INNER JOIN:
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
This selects employee names and their department names, only for employees who have a corresponding department.