力扣(Leecode)數據庫-部分題目

/* 
編寫一個 SQL 查詢,滿足條件:無論 person 是否有地址信息,都需要基於上述兩表提供 person 的以下信息:
FirstName, LastName, City, State
表1: Person
列名         類型
PersonId    int
FirstName    varchar
LastName    varchar
PersonId 是上表主鍵

表2: Address
列名            類型
AddressId    int
PersonId    int
City    varchar
State    varchar

 */

select FirstName, LastName, City, State
from Person p
left join address a
on p.PersonId = a.PersonId

/*
編寫一個 SQL 查詢,獲取 Employee 表中第二高的薪水(Salary).
潛臺詞是如果不存在第二高的薪水,那麼查詢應返回 null
*/

select IFNULL( (select DISTINCT Salary from Employee ORDER BY Salary DESC LIMIT 1 ,1), null)
as secondhighestSalary

/*
分數排名(178)—中等

編寫一個 SQL 查詢來實現分數排名。如果兩個分數相同,則兩個分數排名(Rank)相同。請注意,
平分後的下一個名次應該是下一個連續的整數值。換句話說,名次之間不應該有“間隔”。結果示例:
| Score | Rank |

+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
*/

SELECT score, (select count DISTINCT(score) from Scores where  score >=s1.score)   as rank 
from Scores s2
order by score desc;


/*
連續出現的數字(180)—中等

編寫一個 SQL 查詢,查找所有至少連續出現三次的數字。
| Id | Num |
這裏的join指的是inner join
*/
兩種解法
一是利用join

select DISTINCT l1.num from Logs l1
join Logs l2 on l1.id = l2.id-1
join Logs l3 on l2.id = l3.id -1
where l1.num = l2.num and l2.num = l3.num;


一種是不使用連接,用條件判斷

select DISTINCT l1.num
from Logs l1, Logs l2, Logs l3
where l1.id+1 = l2.id and l2.id+1 = l3.id
and l1.num = l2.num and l2.num = l3.num;


/*
超過經理收入的員工(181)—簡單
Employee 表包含所有員工,他們的經理也屬於員工。每個員工都有一個 Id,此外還有一列對應員工的經理的 Id。
| Id | Name | Salary | ManagerId |
| 1 | Joe | 70000 | 3 |
| 2 | Henry | 80000 | 4 |
| 3 | Sam | 60000 | NULL |
| 4 | Max | 90000 | NULL |

給定 Employee 表,編寫一個 SQL 查詢,該查詢可以獲取收入超過他們經理的員工的姓名。在上面的表格中
,Joe 是唯一一個收入超過他的經理的員工。
*/
兩種解法
一種是join

select E1.Name 
from Employee E1 join Employee E2 on E1.ManagerId = E2.Id
where E1.Salary > E2.Salary


一種是不使用連接,用條件判斷

select E1.Name
from Employee E1,Employee E2
where E1.ManagerId = E2.Id and E1.Salary > E2.Salary;

/*
7.查找重複的電子郵箱(182)—簡單
編寫一個 SQL 查詢,查找 Person 表中所有重複的電子郵箱。
| Id | Email |
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | [email protected] |
根據以上輸入,你的查詢應返回以下結果:
| Email |
| [email protected] |
說明:所有電子郵箱都是小寫字母。
*/

select DISTINCT(Email)
from Person
group by Email
having count(Email) > 1;

第二種

select DISTINCT(p1.Email)
from Person p1,Person p2
where p1.Email = p2.Email
and p1.id != p2.id;

 

/*
8.從不訂購的客戶(183)—簡單

某網站包含兩個表,Customers 表和 Orders 表。
編寫一個 SQL 查詢,找出所有從不訂購任何東西的客戶。
Customers 表:
| Id | Name |
| 1 | Joe |
| 2 | Henry |
| 3 | Sam |
| 4 | Max |
Orders 表:
| Id | CustomerId |
| 1 | 3 |
| 2 | 1 |
例如給定上述表格,你的查詢應返回:
| Customers |
| Henry |
| Max |
*/
兩種解法
一種是左連接,然後判空。

select C.name from Customers C left join Orders O 
on C.Id = O.CustomerId
where O.CustomerId = null;


一種是使用not in 但時間有點長

select name from Customers C
where C.id not in(
    select CustomerId from Orders);

/*
9.部門工資最高的員工(184)—中等
Employee 表包含所有員工信息,每個員工有其對應的 Id, salary 和 department Id。
| Id | Name | Salary | DepartmentId |
| 1 | Joe | 70000 | 1 |
| 2 | Henry | 80000 | 2 |
| 3 | Sam | 60000 | 2 |
| 4 | Max | 90000 | 1 |
Department 表包含公司所有部門的信息。
| Id | Name |
| 1 | IT |
| 2 | Sales |
編寫一個 SQL 查詢,找出每個部門工資最高的員工。例如,根據上述給定的表格,
Max 在 IT 部門有最高工資,Henry 在 Sales 部門有最高工資。
| Department | Employee | Salary |
| IT | Max | 90000 |
| Sales | Henry | 80000 |
*/
方式一:用in 和子查詢

select D.Name as Department, E.Name as Name, Salary
from Employee E join Department D 
on E.DepartmentId = D.Id
where (E.DepartmentId, Salary) in
    (select DepartmentId, Salary from Employee 
    group by DepartmentId)


方式二:用>=all()

select D.Name as Department, E.Name as Name, Salary
from Employee E join Department D 
on E.DepartmentId = D.Id
where E.Salary >= all(
select Salary from Employee E2
where E.DepartmentId = E2.DepartmentId);


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章