Middle
Tell me, what types of JOIN are there.
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 based on a specific condition:
- INNER JOIN — returns only the rows where there are matches in both tables.
- LEFT JOIN (LEFT OUTER JOIN) — returns all rows from the left table and the matching rows from the right table. If there are no matches, the columns from the right table will contain NULL.
- RIGHT JOIN (RIGHT OUTER JOIN) — returns all rows from the right table and the matching rows from the left table.
- FULL JOIN (FULL OUTER JOIN) — returns all rows from both tables, filling with NULL where there are no matches.
- CROSS JOIN — the Cartesian product of two tables, returning all possible combinations of rows.
Example of INNER JOIN:
SELECT users.name, orders.id
FROM users
INNER JOIN orders ON users.id = orders.user_id;
This query will return the names of users and their orders only if the user has orders.