What types of JOINs do you know in SQL?
sobes.tech AI
Answer from AI
In SQL, the following main types of JOINs are used to combine rows from two or more tables:
-
INNER JOIN — returns only those rows where there are matches in both tables based on the join condition.
-
LEFT JOIN (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 (RIGHT OUTER JOIN) — similar to LEFT JOIN, but returns all rows from the right table and matching rows from the left.
-
FULL JOIN (FULL OUTER JOIN) — returns all rows from both tables, matching and non-matching, with NULLs where there is no match.
-
CROSS JOIN — Cartesian product of two tables, each row of the first table is joined with every row of the second.
-
SELF JOIN — when a table is joined with itself, usually using aliases.
Example of INNER JOIN:
SELECT employees.name, departments.name
FROM employees
INNER JOIN departments ON employees.department_id = departments.id;