Senior
What are the types of JOIN?
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 that match the condition 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, NULLs will appear in the right table's columns.
- RIGHT JOIN (RIGHT OUTER JOIN) — similar to LEFT JOIN, but returns all rows from the right table.
- FULL JOIN (FULL OUTER JOIN) — returns all rows from both tables, filling in NULLs where there are no matches.
- CROSS JOIN — the 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;