SQL-数据库刷题

Posted 拂髯客

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL-数据库刷题相关的知识,希望对你有一定的参考价值。

因是个人总结,只列出对自己有用的或较难的:

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-数据库刷题的主要内容,如果未能解决你的问题,请参考以下文章

Microsoft SQL Server 代码片段收集

缺少 SQL SERVER 2014 代码片段

sql Oracle代码片段

sql 日期转换代码片段 - Dato,120,konvertere

跟着大伟学SQL:从基础到进阶,从刷题到面试

以下代码片段是不是容易受到 Rails 5 中 SQL 注入的影响?