The Combine Two Tables interview question is a fundamental database query problem. You are given two tables: Person (containing personId, firstName, and lastName) and Address (containing addressId, personId, city, and state). You need to write a SQL query to report the first name, last name, city, and state for each person in the Person table. If the address of a personId is not present in the Address table, you must still show the person's name and report the city and state as null.
This is a standard Database interview pattern used by companies like Microsoft, Amazon, and Meta to test basic SQL knowledge. It specifically evaluates whether a candidate understands the difference between an INNER JOIN and a LEFT JOIN. Since the requirement specifies that people without addresses should still be included, a simple join is not enough; a left outer join is required.
The core pattern is a LEFT JOIN. In SQL, a LEFT JOIN returns all records from the left table (Person), and the matched records from the right table (Address). If there is no match, the result is NULL on the right side.
firstName, lastName, city, statePersonLEFT JOIN AddressPerson.personId = Address.personIdPerson Table:
1, John, Doe2, Jane, SmithAddress Table:
101, 1, New York, NYResult:
John, Doe, New York, NYJane, Smith, null, nullJOIN or WHERE clause join, which would exclude Jane Smith entirely from the result.personId with addressId).Always look for keywords like "all records from table A regardless of table B" or "if not present, report null." These are immediate signals that you need a LEFT JOIN or RIGHT JOIN instead of an INNER JOIN.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Fix Names in a Table | Easy | Solve | |
| Not Boring Movies | Easy | Solve | |
| Primary Department for Each Employee | Easy | Solve | |
| Queries Quality and Percentage | Easy | Solve | |
| Customers Who Never Order | Easy | Solve |