標籤:union where number you art 難度 rom partition sts
因是個人總結,只列出對自己有用的或較難的:
leetcode 標記難度 困難需求:-- 部門工資前三高的員工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 |+----+----------+編寫一個 SQL 查詢,找出每個部門工資前三高的員工。例如,根據上述給定的表格,查詢結果應返回:+------------+----------+--------+| Department | Employee | Salary |+------------+----------+--------+| IT | Max | 90000 || IT | Randy | 85000 || IT | Joe | 70000 || Sales | Henry | 80000 || Sales | Sam | 60000 |+------------+----------+--------+第一種解法:;with Employee(Id,Name,Salary,DepartmentId) AS( select 1,'Joe','70000',1 union allselect 2,'Henry','80000',2 union allselect 3,'Sam','60000',2 union allselect 4,'Max','90000',1 union allselect 5,'Janet','69000',1 union allselect 6,'Randy','85000',1), Department(Id,Name) AS( SELECT 1,'IT' UNION ALL SELECT 2,'Sales' ) select d.Name as Department,e.Name as Employee,e.Salary from ( SELECT *,ROW_NUMBER()OVER(partition by DepartmentId order by Salary desc) as Rank FROM Employee) e join Department d on e.DepartmentId = d.Id where Rank<=3 order by d.Id ASC第二種解法:為了避免 有相同排名出現,採用 DENSE_RANK 密級排名 select d.Name as Department,e.Name as Employee,e.Salary from ( SELECT *,Dense_Rank()OVER(partition by DepartmentId order by Salary desc) as Rank FROM Employee) e join Department d on e.DepartmentId = d.Id where Rank<=3 order by d.Id ASC
leetcode 難度標記: 簡單給定一個 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 Id from Weather w where Temperature > (select Temperature from Weather l where dateadd(day,1,l.RecordDate) = w.RecordDate)
leetcode 難度標記: 簡單編寫一個 SQL 查詢,擷取 Employee 表中第二高的薪水(Salary) 。+----+--------+| Id | Salary |+----+--------+| 1 | 100 || 2 | 200 || 3 | 300 |+----+--------+例如上述 Employee 表,SQL查詢應該返回 200 作為第二高的薪水。如果不存在第二高的薪水,那麼查詢應返回 null。+---------------------+| SecondHighestSalary |+---------------------+| 200 |+---------------------+解法:SELECT MAX(SALARY) AS SecondHighestSalary FROM Employee WHERE Salary <(SELECT MAX(SALARY) FROM Employee)
leetcode 難度標記: 中等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 |+------------+----------+--------+/* Write your T-SQL query statement below */select d.Name as Department ,e.Name as Employee ,e.Salary as Salaryfrom Employee e join Department d on e.DepartmentId = d.Id join( SELECT DepartmentId ,max(Salary) as Salary FROM Employee Group by DepartmentId ) grp on e.Salary = grp.Salary and e.DepartmentId = grp.DepartmentId
SQL-資料庫刷題