編寫一個 SQL 查詢,獲取 Employee 表中第 n 高的薪水(Salary)算法
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
複製代碼
例如上述 Employee 表,n = 2 時,應返回第二高的薪水 200。若是不存在第 n 高的薪水,那麼查詢應返回 null。bash
+------------------------+
| getNthHighestSalary(2) |
+------------------------+
| 200 |
+------------------------+
複製代碼
分組去重加分頁查詢語法函數
CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
SET N = N - 1;
RETURN (
select Salary from Employee GROUP BY Salary ORDER BY Salary desc LIMIT N,1
);
END
複製代碼
編寫一個 SQL 查詢來實現分數排名。若是兩個分數相同,則兩個分數排名(Rank)相同。請注意,平分後的下一個名次應該是下一個連續的整數值。換句話說,名次之間不該該有「間隔」。ui
+----+-------+
| Id | Score |
+----+-------+
| 1 | 3.50 |
| 2 | 3.65 |
| 3 | 4.00 |
| 4 | 3.85 |
| 5 | 4.00 |
| 6 | 3.65 |
+----+-------+
複製代碼
例如,根據上述給定的 Scores 表,你的查詢應該返回(按分數從高到低排列):spa
+-------+------+
| Score | Rank |
+-------+------+
| 4.00 | 1 |
| 4.00 | 1 |
| 3.85 | 2 |
| 3.65 | 3 |
| 3.65 | 3 |
| 3.50 | 4 |
+-------+------+
複製代碼
考慮兩件事:1按分數降序排列 2 分數相同屬同一級code
select Score,(SELECT COUNT(DISTINCT score) from Scores WHERE score >= s.score) as Rank from scores s ORDER BY Score DESC
複製代碼
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 是惟一一個收入超過他的經理的員工。ip
+----------+
| Employee |
+----------+
| Joe |
+----------+
複製代碼
經過get
解答:經過自鏈接 關聯表兩次查詢 組裝出員工表每一個員工對應的經理信息 便可經過條件判斷取出結果string
select e1.Name Employee from Employee e1,Employee e2 where e1.ManagerId=e2.Id
and e1.Salary>e2.Salary
複製代碼
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 部門有最高工資。
解答 內鏈接匹配查詢出全部員工對應的部門名稱 而後經過子查詢條件匹配
select d.Name as Department,e.Name as Employee, e.Salary
from Department d join Employee e on d.Id=e.DepartmentId
where e.Salary >=(select max(Salary) from Employee where DepartmentId=d.Id)
複製代碼
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 |
| 5 | Janet | 69000 | 1 |
| 6 | Randy | 85000 | 1 |
+----+-------+--------+--------------+
複製代碼
Department 表包含公司全部部門的信息。
+----+----------+
| Id | Name |
+----+----------+
| 1 | IT |
| 2 | Sales |
+----+----------+
複製代碼
SELECT Department.Name AS Department, e1.Name AS Employee, e1.Salary AS Salary
FROM Employee e1
JOIN Department
ON e1.DepartmentId = Department.Id
WHERE 3 > (
SELECT COUNT(DISTINCT e2.Salary)
FROM Employee e2
WHERE e2.Salary > e1.Salary AND e1.DepartmentId = e2.DepartmentId
)
複製代碼
編寫一個 SQL 查詢,來刪除 Person 表中全部重複的電子郵箱,重複的郵箱裏只保留 Id 最小 的那個。
+----+------------------+
| Id | Email |
+----+------------------+
| 1 | john@example.com |
| 2 | bob@example.com |
| 3 | john@example.com |
+----+------------------+
複製代碼
Id 是這個表的主鍵。
咱們可使用如下代碼,將此表與它自身在電子郵箱列中鏈接起來。
SELECT p1.* FROM Person p1,Person p2 WHERE p1.Email = p2.Email
複製代碼
而後咱們須要找到其餘記錄中具備相同電子郵件地址的更大 ID。因此咱們能夠像這樣給 WHERE 子句添加一個新的條件。
SELECT p1.* FROM Person p1,Person p2 WHERE p1.Email = p2.Email AND p1.Id > p2.Id
複製代碼
由於咱們已經獲得了要刪除的記錄,因此咱們最終能夠將該語句更改成 DELETE。
DELETE p1 FROM Person p1, Person p2 WHERE p1.Email = p2.Email AND p1.Id > p2.Id
複製代碼
給定一個 Weather 表,編寫一個 SQL 查詢,來查找與以前(昨天的)日期相比溫度更高的全部日期的 Id。
+---------+------------------+------------------+
| Id(INT) | RecordDate(DATE) | Temperature(INT) |
+---------+------------------+------------------+
| 1 | 2015-01-01 | 10 |
| 2 | 2015-01-02 | 25 |
| 3 | 2015-01-03 | 20 |
| 4 | 2015-01-04 | 30 |
+---------+------------------+------------------+
複製代碼
例如,根據上述給定的 Weather 表格,返回以下 Id:
+----+
| Id |
+----+
| 2 |
| 4 |
+----+
複製代碼
select * from weather w1 join weather w2 on DATEDIFF(w1.RecordDate,w2.RecordDate)=1
id RecordDate Temperature w2.id 昨天的日期 w2.Temperature(昨天的溫度)
2 2019-01-02 25 1 2019-01-01 10
4 2019-01-04 30 3 2019-01-03 20
4 2019-01-04 30 3 2019-01-03 20
條件帥選今天比昨天溫度高的數據 w1.Temperature>w2.Temperature
select w1.id from weather w1 join weather w2 on DATEDIFF(w1.RecordDate,w2.RecordDate)=1 where w1.Temperature>w2.Temperature
複製代碼
Trips 表中存全部出租車的行程信息。每段行程有惟一鍵 Id,Client_Id 和 Driver_Id 是 Users 表中 Users_Id 的外鍵。Status 是枚舉類型,枚舉成員爲 (‘completed’, ‘cancelled_by_driver’, ‘cancelled_by_client’)。
+----+-----------+-----------+---------+--------------------+----------+
| Id | Client_Id | Driver_Id | City_Id | Status |Request_at|
+----+-----------+-----------+---------+--------------------+----------+
| 1 | 1 | 10 | 1 | completed |2013-10-01|
| 2 | 2 | 11 | 1 | cancelled_by_driver|2013-10-01|
| 3 | 3 | 12 | 6 | completed |2013-10-01|
| 4 | 4 | 13 | 6 | cancelled_by_client|2013-10-01|
| 5 | 1 | 10 | 1 | completed |2013-10-02|
| 6 | 2 | 11 | 6 | completed |2013-10-02|
| 7 | 3 | 12 | 6 | completed |2013-10-02|
| 8 | 2 | 12 | 12 | completed |2013-10-03|
| 9 | 3 | 10 | 12 | completed |2013-10-03|
| 10 | 4 | 13 | 12 | cancelled_by_driver|2013-10-03|
+----+-----------+-----------+---------+--------------------+----------+
複製代碼
Users 表存全部用戶。每一個用戶有惟一鍵 Users_Id。Banned 表示這個用戶是否被禁止,Role 則是一個表示(‘client’, ‘driver’, ‘partner’)的枚舉類型。
+----------+--------+--------+
| Users_Id | Banned | Role |
+----------+--------+--------+
| 1 | No | client |
| 2 | Yes | client |
| 3 | No | client |
| 4 | No | client |
| 10 | No | driver |
| 11 | No | driver |
| 12 | No | driver |
| 13 | No | driver |
+----------+--------+--------+
複製代碼
查出 2013年10月1日 至 2013年10月3日 期間非禁止用戶的取消率。基於上表,你的 SQL 語句應返回以下結果,取消率(Cancellation Rate)保留兩位小數。
+------------+-------------------+
| Day | Cancellation Rate |
+------------+-------------------+
| 2013-10-01 | 0.33 |
| 2013-10-02 | 0.00 |
| 2013-10-03 | 0.50 |
+------------+-------------------+
複製代碼
解法
#先求出非禁止用戶的全部記錄
select * from Trips t join Users u on t.Client_Id =u.Users_Id where u.Banned='No'
經過if函數加分組 2013年10月1日 至 2013年10月3日 期間非禁止用戶的取消率
IF(expr1,expr2,expr3),若是expr1的值爲true,則返回expr2的值,若是expr1的值爲false,
round(x,d) ,x指要處理的數,d是指保留幾位小數
select t.Request_at as Day,
round(count( IF ( t.Status !="completed", t.Status, NULL ) ) / count(t.Status),2) as `Cancellation Rate`
from Trips t join Users u on t.Client_Id =u.Users_Id
where u.Banned='No' and t.request_at between "2013-10-01" and "2013-10-03" group by t.Request_at
複製代碼
+---------+---------+
| id | student |
+---------+---------+
| 1 | Abbot |
| 2 | Doris |
| 3 | Emerson |
| 4 | Green |
| 5 | Jeames |
+---------+---------+
複製代碼
假如數據輸入的是上表,則輸出結果以下:
+---------+---------+
| id | student |
+---------+---------+
| 1 | Doris |
| 2 | Abbot |
| 3 | Green |
| 4 | Emerson |
| 5 | Jeames |
+---------+---------+
複製代碼
注意:若是學生人數是奇數,則不須要改變最後一個同窗的座位。
先把簡單的偶數都-1;而後對於非最大的奇數id+1;最後(即else)若是存在未變化的數則值不變
select
case #若是
when id%2=0 then id-1 # id%2爲偶數 則返回 id-1
when id<(select max(id) from seat) then id+1 #若是表中最大的ID 小於當前返回的ID 則執行 id+1 也就是取最大ID
else id #若是存在未變化的數則值不變
end as id,student from seat order by id
複製代碼