Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 7 additions & 11 deletions src/main/java/g0101_0200/s0175_combine_two_tables/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

Easy

SQL Schema

Table: `Person`

+-------------+---------+
Expand All @@ -13,8 +11,7 @@ Table: `Person`
| lastName | varchar |
| firstName | varchar |
+-------------+---------+

personId is the primary key column for this table.
personId is the primary key (column with unique values) for this table.
This table contains information about the ID of some persons and their first and last names.

Table: `Address`
Expand All @@ -27,19 +24,18 @@ Table: `Address`
| city | varchar |
| state | varchar |
+-------------+---------+

addressId is the primary key column for this table.
addressId is the primary key (column with unique values) for this table.
Each row of this table contains information about the city and state of one person with ID = PersonId.

Write an SQL query to report the first name, last name, city, and state of each person in the `Person` table. If the address of a `personId` is not present in the `Address` table, report `null` instead.
Write a solution to report the first name, last name, city, and state of each person in the `Person` table. If the address of a `personId` is not present in the `Address` table, report `null` instead.

Return the result table in **any order**.

The query result format is in the following example.
The result format is in the following example.

**Example 1:**

**Input:**
**Input:**

Person table:
+----------+----------+-----------+
Expand All @@ -48,7 +44,7 @@ The query result format is in the following example.
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+

Address table:
+-----------+----------+---------------+------------+
| addressId | personId | city | state |
Expand All @@ -68,4 +64,4 @@ The query result format is in the following example.

**Explanation:**

There is no address in the address table for the personId = 1 so we return null in their city and state. addressId = 1 contains information about the address of personId = 2.
There is no address in the address table for the personId = 1 so we return null in their city and state. addressId = 1 contains information about the address of personId = 2.
15 changes: 4 additions & 11 deletions src/main/java/g0101_0200/s0175_combine_two_tables/script.sql
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
# Write your MySQL query statement below
# #Easy #Database #SQL_I_Day_5_Union #2022_06_26_Time_491_ms_(32.30%)_Space_0B_(100.00%)
SELECT
FirstName,
LastName,
City,
State
FROM
Person
LEFT JOIN
Address
USING (PersonId);
# #Easy #Database #SQL_I_Day_5_Union #2026_11_30_Time_336_ms_(96.67%)_Space_0B_(100.00%)
SELECT p.firstName , p.lastName , a.city , a.state
FROM Person p left join Address a
on p.personId = a.personId;
Loading