Scientists common SQL data five interview questions

Author | Alexei Ledenev

Translation | Inf, Zebian | Carol

Publisher | CSDN cloud (ID: CSDNcloud)

In any data-centric work, we have a deep understanding of SQL is the key to success, although this is not the most interesting part of the job. In fact, in addition to SELECT FROM WHERE GROUP BY ORDER BY , there are more SQL method. The more you know the function, the easier it is the content and operation of the desired query.

The authors wish to learn and communicate in this article the following two things:

1) learn and teach some of the basic functions other than SQL functions;

2) explore some SQL practice interview questions.

* Problem in this article only from Leetcode

Question 1:The second-highest salary

Write a SQL query used to obtain the second-highest salary from the Employee table. For example, the Employee table given below, the query should return 200 as the second high salary. If there is no second-highest salary, the query should return null.

+----+--------+
| Id | Salary |
+----+--------+
| 1   | 100     |
| 2   | 200     |
| 3   | 300     |
+----+--------+

1) Solution A :Use IFNULL , OFFSET

  • IFNULL (expression, alt): If null, then ifnull () Returns the specified value, otherwise return the expected value. If there is no second-highest salary, we'll use it returns null.

  • OFFSET: offset used in conjunction with negligible ORDERBY clause specified in the first n rows. This can be useful, because you want to get the second row (the second highest salary)

| Id | Salary |
+----+--------+
| 1   | 100     |
| 2   | 200     |
| 3   | 300     |
+----+--------+

2) Solution B :Use MAX ()

This query represents the selected MAX salary is not equal to the maximum salary, which is equal to select the second-highest salary.

SELECT
IFNULL(
(SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1
), null) as SecondHighestSalary
FROM Employee
LIMIT 1

问题2:重复的电子邮件

编写SQL查询以在名为Person的表中查找所有重复的电子邮件。

+----+---------+| Id | Email    |+----+---------+| 1  | [email protected]  || 2  | [email protected]  || 3  | [email protected]  |+----+---------+

1)解决方案A子查询中的COUNT()

首先,创建一个子查询来显示每封电子邮件的频率次数。然后子查询在计数大于1的地方被过滤。

SELECT EmailFROM (SELECT Email, count(Email) AS countFROM PersonGROUP BY Email) as email_countWHERE count > 1

2)解决方案B:HAVING子句

  • HAVING是一个子句,从本质上讲,你可以将WHERE语句与聚合(GROUP BY)结合使用。

SELECT EmailFROM PersonGROUP BY EmailHAVING count(Email) > 1

 

问题3:温度上升

下面给定一个天气表,编写一个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                   |+---------+------------------+------------------+

解决方案:DATEDIFF()

  • DATEDIFF是计算两个日期之间的差,用于确保我们将今天的温度与昨天的温度进行比较。

简单来说,查询是选择给定日期的温度高于昨天的温度的ID。

SELECT DISTINCT a.IdFROM Weather a, Weather bWHERE a.Temperature > b.TemperatureAND DATEDIFF(a.Recorddate, b.Recorddate) = 1

问题4:部门最高薪资

下面的雇员表中包含所有雇员。每个员工都有一个ID、一个薪水,还有一个部门ID列。

+----+-------+--------+--------------+| Id | Name   | Salary | DepartmentId |+----+-------+--------+--------------+| 1   | Joe   | 70000  | 1               || 2   | Jim   | 90000  | 1               || 3   | Henry | 80000  | 2               || 4   | Sam   | 60000  | 2               || 5   | Max   | 90000  | 1               |+----+-------+--------+--------------+

下面的部门表包含公司的所有部门。

+----+----------+| Id | Name      |+----+----------+| 1   | IT        || 2   | Sales    |+----+----------+

编写SQL查询来查找每个部门中薪水最高的员工。对于上述两个表,你的SQL查询应返回以下行(行的顺序无关紧要)。

+------------+----------+--------+| Department | Employee | Salary |+------------+----------+--------+| IT            | Max       | 90000  || IT            | Jim       |90000   || Sales        | Henry     | 80000  |+------------+----------+--------+

解决方案:IN子句

  • IN子句允许你在WHERE语句中使用多个OR子句。例如,WHERE country ='Canada'或country ='USA'与WHERE country IN('Canada','USA')相同。

  • 在这种情况下,我们希望过滤部门表来仅显示每个部门的最高薪水(即DepartmentId)。然后,我们可以将两个表连接在一起,其中DepartmentId和Salary在已过滤的Department表中。

SELECTDepartment.name AS 'Department',Employee.name AS 'Employee',SalaryFROM EmployeeINNER JOIN Department ON Employee.DepartmentId = Department.IdWHERE (DepartmentId , Salary)IN( SELECTDepartmentId, MAX(Salary)FROMEmployeeGROUP BY DepartmentId)

问题5:互换座位

玛丽是一所中学的老师,她有一张座位表,上面存储着学生的姓名和相应的座位ID。列ID是连续的增量,玛丽想为相邻的学生互换座位。

你可以编写SQL查询来输出玛丽的结果吗?

+---------+---------+
|    id     | student |
+---------+---------+
|    1      | Abbot   |
|    2      | Doris   |
|    3      | Emerson |
|    4      | Green   |
|    5      | Jeames  |
+---------+---------+

对于样本输入,输出为:

+---------+---------+
|    id     | student |
+---------+---------+
|    1      | Doris   |
|    2      | Abbot   |
|    3      | Green   |
|    4      | Emerson |
|    5      | Jeames  |
+---------+---------+

注意:如果学生人数为奇数,则无需更改最后一个座位。

解决方案:CASE WHEN

  • 可以将CASE WHEN THEN语句视为编码中的IF语句。

  • 第一条WHEN语句检查行数是否为奇数,如果行数为奇数,请确保ID号不变。

  • 第二个WHEN语句为每个id加1(例如,1,3,5变为2,4,6)

  • 同样,第三个WHEN语句将每个id减1(2,4,6变为1,3,5)

SELECT
CASE
WHEN((SELECT MAX(id) FROM seat)%2 = 1) AND id = (SELECT MAX(id) FROM seat) THENid
WHEN id%2 = 1 THEN id + 1
ELSE id - 1
END AS id, student
FROM seat
ORDER BY id

以上就是所有的解决方法,如果有不清楚的地方或其他意见,欢迎评论告诉我们!

【End】

在中国企业与「远程办公」正面相遇满月之际,2月29日,CSDN 联合广大「远程办公」工具服务企业共同举办【抗击疫情,科技公司在行动】系列之【远程办公】专题线上峰会活动:中国「远程办公」大考
扫下方二维码或点击阅读原文免费报名直播+抽取奖品+与大牛交流

提前了解峰会详情,可加小助手微信csdnai,回复远程办公,进直播群

推荐阅读 

疫情病毒全部“抹杀”?用数据模型来解读传播抑制的效果差异!

两成开发者月薪超 1.7 万、算法工程师最紧缺!| 中国开发者年度报告

为诺亚、北大提出GhostNet,使用线性变换生成特征图,准确率超MobileNet v3 | CVPR 2020

真实版“删库跑路”?程序员蓄意破坏线上生产环境!

新知识点!一文告诉你如何调试运行在Docker容器中的远程Node.js应用程序

远程办公是一阵“过渡风”还是会“继续燃烧”?

你点的每一个在看,我认真当成了喜欢

猛戳“阅读原文”,参与报名吧!

发布了1763 篇原创文章 · 获赞 4万+ · 访问量 1595万+

Guess you like

Origin blog.csdn.net/csdnnews/article/details/104568270