Junior — Middle
Explain the difference between an inner join and a left outer join in SQL.
sobes.tech AI
Answer from AI
Inner Join returns only the rows that have matching values in both tables. If there is no corresponding record in one of the tables, that row will not appear in the result.
Left Outer Join (LEFT JOIN) returns all rows from the left table and the matching rows from the right table. If there is no match for a row from the left table, the result will include the values from the left table and NULL for the right table.
Example:
Table A:
| id | name |
|---|---|
| 1 | Alice |
| 2 | Bob |
Table B:
| id | city |
|---|---|
| 1 | London |
- INNER JOIN on id:
| id | name | city |
|---|---|---|
| 1 | Alice | London |
- LEFT JOIN on id:
| id | name | city |
|---|---|---|
| 1 | Alice | London |
| 2 | Bob | NULL |
Thus, LEFT JOIN preserves all records from the left table, while INNER JOIN only the matching ones.