Skip to content

Commit b22c79d

Browse files
authored
add leetcode
1 parent f7908d7 commit b22c79d

File tree

2 files changed

+68
-0
lines changed

2 files changed

+68
-0
lines changed

Database/175. Combine Two Tables.md

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
Table:`Person`
2+
3+
```
4+
+-------------+---------+
5+
| Column Name | Type |
6+
+-------------+---------+
7+
| PersonId | int |
8+
| FirstName | varchar |
9+
| LastName | varchar |
10+
+-------------+---------+
11+
PersonId is the primary key column for this table.
12+
```
13+
Table: `Address`
14+
15+
```
16+
+-------------+---------+
17+
| Column Name | Type |
18+
+-------------+---------+
19+
| AddressId | int |
20+
| PersonId | int |
21+
| City | varchar |
22+
| State | varchar |
23+
+-------------+---------+
24+
AddressId is the primary key column for this table.
25+
```
26+
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for each of those people:
27+
28+
```
29+
FirstName, LastName, City, State
30+
```
31+
32+
**SQL**
33+
```
34+
select a.FirstName, a.LastName, b.City, b.State
35+
from person a left join Address b
36+
on a.PersonId = b.PersonId
37+
```
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
The `Employee` table holds all employees including their managers. Every employee has an Id, and there is also a column for the manager Id.
2+
3+
```
4+
+----+-------+--------+-----------+
5+
| Id | Name | Salary | ManagerId |
6+
+----+-------+--------+-----------+
7+
| 1 | Joe | 70000 | 3 |
8+
| 2 | Henry | 80000 | 4 |
9+
| 3 | Sam | 60000 | NULL |
10+
| 4 | Max | 90000 | NULL |
11+
+----+-------+--------+-----------+
12+
```
13+
Given the `Employee` table, write a SQL query that finds out employees who earn more than their managers. For the above table, Joe is the only employee who earns more than his manager.
14+
15+
```
16+
+----------+
17+
| Employee |
18+
+----------+
19+
| Joe |
20+
+----------+
21+
22+
```
23+
24+
**SQL**
25+
```
26+
select a.name as Employee
27+
from Employee a,
28+
Employee b
29+
where a.managerId = b.Id
30+
and a.salary > b.salary
31+
```

0 commit comments

Comments
 (0)