Sobes.tech
Junior — Middle

What is the difference between using INNER JOIN and OUTER JOIN in SQL when joining tables?

sobes.tech AI

Answer from AI

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

OUTER JOIN (LEFT, RIGHT, or FULL) returns all rows from one or both tables, including those without matches. In such cases, missing values are filled with NULL.

Example:

-- INNER JOIN: only matching records
SELECT * FROM users INNER JOIN orders ON users.id = orders.user_id;

-- LEFT OUTER JOIN: all users, even if they have no orders
SELECT * FROM users LEFT JOIN orders ON users.id = orders.user_id;