码迷,mamicode.com
首页 > 数据库 > 详细

SQL-数据库刷题

时间:2018-08-02 13:50:52      阅读:1717      评论:0      收藏:0      [点我收藏+]

标签: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 all
select 2,'Henry','80000',2 union all
select 3,'Sam','60000',2 union all
select 4,'Max','90000',1 union all
select 5,'Janet','69000',1 union all
select 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 Salary
from 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-数据库刷题

标签:union   where   number   you   art   难度   rom   partition   sts   

原文地址:https://www.cnblogs.com/Frank99/p/9406580.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!