Middle
Чем отличаются JOIN, LEFT JOIN и RIGHT JOIN?
sobes.tech AI
Answer from AI
JOIN is an operation in SQL that combines rows from two tables based on a matching condition of values in specified columns.
- JOIN (or INNER JOIN) returns only those rows where there are matches in both tables.
- LEFT 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 be NULL.
- RIGHT JOIN returns all rows from the right table and the matching rows from the left table, with NULLs where there are no matches.
Example:
SELECT * FROM A
JOIN B ON A.id = B.a_id; -- only matching rows
SELECT * FROM A
LEFT JOIN B ON A.id = B.a_id; -- all from A + matches from B
SELECT * FROM A
RIGHT JOIN B ON A.id = B.a_id; -- all from B + matches from A