LeetCode 175: Combine Two Tables

Database

Problem Description

Explanation:

To solve this problem, we need to perform a left join operation between the Person table and the Address table based on the personId. We will select the firstName, lastName, city, and state columns from both tables, matching them on personId. If an address does not exist for a person in the Address table, we need to return null values for city and state.

  1. Perform a left join between the Person and Address tables on personId.
  2. Select the firstName, lastName, city, and state columns.
  3. If an address does not exist for a person, return null for city and state.
  4. Return the result table.

Time Complexity: O(n + m) where n is the number of rows in the Person table and m is the number of rows in the Address table. Space Complexity: O(n + m) for storing the result table.

:

Solutions

# Java Solution
SELECT Person.firstName, Person.lastName, Address.city, Address.state
FROM Person
LEFT JOIN Address ON Person.personId = Address.personId;

Loading editor...