其他
数据科学家常见的5个SQL面试问题
作者 | Alexei Ledenev
翻译 | 天道酬勤,责编 | Carol
出品 | CSDN云计算(ID:CSDNcloud)
在任何以数据为中心的工作中,对SQL有深刻的理解都是成功的关键,尽管这不是工作中最有趣的部分。事实上,除了SELECT FROM WHERE GROUP BY ORDER BY之外,还有更多的SQL方法。你知道的功能越多,操作和查询所需的内容就越容易。
作者希望在本文中学习和交流以下两件事:
1)学习和教一些基本功能以外的SQL函数
2)探讨一些SQL面试练习问题
* 本文中的问题仅来自Leetcode
编写一个SQL查询用于从Employee表中获取第二高的薪水。例如,给定下面的Employee表,查询应返回200作为第二高的薪水。如果没有第二高的薪水,则查询应返回null。
+----+--------+
| Id | Salary |
+----+--------+
| 1 | 100 |
| 2 | 200 |
| 3 | 300 |
+----+--------+
IFNULL(表达,alt):如果为null,则ifnull()返回指定的值,否则返回期望的值。 如果没有第二高的薪水,我们会使用它返回null。 OFFSET:offset与ORDERBY子句一起使用可忽略指定的前n行。这会很有用,因为你希望获得第二行(第二高的薪水)
SELECT
IFNULL(
(SELECT DISTINCT Salary
FROM Employee
ORDER BY Salary DESC
LIMIT 1 OFFSET 1
), null) as SecondHighestSalary
FROM Employee
LIMIT 1
SELECT MAX(salary) AS SecondHighestSalary
FROM Employee
WHERE salary != (SELECT MAX(salary) FROM Employee)
+----+---------+
| Id | Email |
+----+---------+
| 1 | a@b.com |
| 2 | c@d.com |
| 3 | a@b.com |
+----+---------+
SELECT Email
FROM (
SELECT Email, count(Email) AS count
FROM Person
GROUP BY Email
) as email_count
WHERE count > 1
HAVING是一个子句,从本质上讲,你可以将WHERE语句与聚合(GROUP BY)结合使用。
SELECT Email
FROM Person
GROUP BY Email
HAVING count(Email) > 1
+---------+------------------+------------------+
| 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是计算两个日期之间的差,用于确保我们将今天的温度与昨天的温度进行比较。
SELECT DISTINCT a.Id
FROM Weather a, Weather b
WHERE a.Temperature > b.Temperature
AND DATEDIFF(a.Recorddate, b.Recorddate) = 1
+----+-------+--------+--------------+
| 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 |
+----+----------+
+------------+----------+--------+
| Department | Employee | Salary |
+------------+----------+--------+
| IT | Max | 90000 |
| IT | Jim |90000 |
| Sales | Henry | 80000 |
+------------+----------+--------+
IN子句允许你在WHERE语句中使用多个OR子句。例如,WHERE country ='Canada'或country ='USA'与WHERE country IN('Canada','USA')相同。
在这种情况下,我们希望过滤部门表来仅显示每个部门的最高薪水(即DepartmentId)。然后,我们可以将两个表连接在一起,其中DepartmentId和Salary在已过滤的Department表中。
SELECT
Department.name AS 'Department',
Employee.name AS 'Employee',
Salary
FROM Employee
INNER JOIN Department ON Employee.DepartmentId = Department.Id
WHERE (DepartmentId , Salary)
IN
( SELECT
DepartmentId, MAX(Salary)
FROM
Employee
GROUP BY DepartmentId
)
+---------+---------+
| 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 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
解析云原生与云计算本质区别,别再傻傻分不清楚了! Go 大败!Google 宣布 Fuchsia 终端开发只支持 C/C++/Dart 干货!从0到1教你打造一个令人上瘾的聊天机器人?
游戏之道
真实版“删库跑路”?程序员蓄意破坏线上生产环境!