Skip to content
Open
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
75 changes: 75 additions & 0 deletions SQL/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# 175. Combine Two Tables

Table: Person

```sql
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| personId | int |
| lastName | varchar |
| firstName | varchar |
+-------------+---------+
personId is the primary key column for this table.
This table contains information about the ID of some persons and their first and last names.
```

Table: Address
```sql
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| addressId | int |
| personId | int |
| city | varchar |
| state | varchar |
+-------------+---------+
addressId is the primary key column 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.

Return the result table in any order.

The query result format is in the following example.

Example 1:

```sql
Input:
Person table:
+----------+----------+-----------+
| personId | lastName | firstName |
+----------+----------+-----------+
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+
Address table:
+-----------+----------+---------------+------------+
| addressId | personId | city | state |
+-----------+----------+---------------+------------+
| 1 | 2 | New York City | New York |
| 2 | 3 | Leetcode | California |
+-----------+----------+---------------+------------+
Output:
+-----------+----------+---------------+----------+
| firstName | lastName | city | state |
+-----------+----------+---------------+----------+
| Allen | Wang | Null | Null |
| Bob | Alice | New York City | New York |
+-----------+----------+---------------+----------+
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.
```


### INNER JOIN
INNER JOIN (內部連接) 為等值連接,必需指定等值連接的條件,而查詢結果只會返回符合連接條件的資料。

### LEFT JOIN
可以用來建立左外部連接,查詢的 SQL 敘述句 LEFT JOIN 左側資料表 (table_name1) 的所有記錄都會加入到查詢結果中,即使右側資料表 (table_name2) 中的連接欄位沒有符合的值也一樣。

### RIGHT JOIN
相對於 LEFT JOIN,RIGHT JOIN 可以用來建立右外部連接,查詢的 SQL 敘述句 RIGHT JOIN 右側資料表 (table_name2) 的所有記錄都會加入到查詢結果中,即使左側資料表 (table_name1) 中的連接欄位沒有符合的值也一樣。
7 changes: 7 additions & 0 deletions SQL/combineTwoTables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

SELECT Person.firstname, Person.lastName,Address.city, Address.state
FROM Person
Left JOIN Address
on Person.personId=Address.personId