Sobes.tech
Junior — Middle

Explain the differences between an inner join and an outer join in SQL.

sobes.tech AI

Answer from AI

In SQL, inner (INNER JOIN) and outer joins (OUTER JOIN) are used to combine rows from two tables, but they differ in how they select rows:

  • Inner join (INNER JOIN) returns only those rows that have matches in both tables based on the join condition.

  • Outer join (OUTER JOIN) returns all rows from one table and the matching rows from the other. If there is no match, NULLs will appear in the places of missing data.

Outer joins come in three types:

  • LEFT OUTER JOIN — all rows from the left table + matching rows from the right
  • RIGHT OUTER JOIN — all rows from the right table + matching rows from the left
  • FULL OUTER JOIN — all rows from both tables, with NULLs where there are no matches

Example:

SELECT * FROM users INNER JOIN orders ON users.id = orders.user_id;
-- Returns only users with orders

SELECT * FROM users LEFT JOIN orders ON users.id = orders.user_id;
-- Returns all users, even if they have no orders (orders will be NULL)
Explain the differences between an inner join and an… - sobes.tech