Junior — Middle
Can you list the main types of table joins in SQL?
sobes.tech AI
Answer from AI
Main types of table joins in SQL:
- INNER JOIN — returns only the rows with matches in both tables.
- LEFT JOIN (or LEFT OUTER JOIN) — returns all rows from the left table and matching rows from the right; if there are no matches, the columns from the right table will be NULL.
- RIGHT JOIN (or RIGHT OUTER JOIN) — returns all rows from the right table and matching rows from the left.
- FULL JOIN (or FULL OUTER JOIN) — returns all rows from both tables, filling NULL where there are no matches.
- CROSS JOIN — Cartesian product of two tables, returning all possible combinations of rows.
Example of INNER JOIN:
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;
This query will return the names of employees and their departments only for those employees who have a corresponding department.