Sobes.tech
Junior — Middle

Can you explain the difference between FULL JOIN and INNER JOIN in SQL?

sobes.tech AI

Answer from AI

INNER JOIN returns only the rows that have matching values in both tables based on the join condition. If there is no matching record in one of the tables, that row will not be included in the result.

FULL JOIN (or FULL OUTER JOIN) returns all rows from both tables. When there is no match for a row in one of the tables, the missing side will contain NULL.

Example:

-- Table A
id | name
1  | Alice
2  | Bob

-- Table B
id | city
2  | London
3  | Paris

-- INNER JOIN
SELECT A.id, A.name, B.city FROM A INNER JOIN B ON A.id = B.id;
-- Result:
-- 2 | Bob | London

-- FULL JOIN
SELECT A.id, A.name, B.city FROM A FULL JOIN B ON A.id = B.id;
-- Result:
-- 1 | Alice | NULL
-- 2 | Bob   | London
-- 3 | NULL  | Paris

Thus, INNER JOIN filters only matching records, while FULL JOIN retains all records from both tables, filling missing values with NULL.