leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)

时间:2022-11-09 14:37:05

一、Nth Highest Salary

Write a SQL query to get the nth highest salary from the Employee table.

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

For example, given the above Employee table, the nth highest salary where n = 2 is 200. If there is no nth highest salary, then the query should return null.

分析:题意为编写SQL查询获取雇员表中的第n高薪水值。例如,给定上面的雇员表,当n为2时,第n高薪水为200.如果没有第n高薪水,查询返回null。

代码:

CREATE FUNCTION getNthHighestSalary(N INT) RETURNS INT
BEGIN
set N=N-1;
RETURN (
# Write your MySQL query statement below.
SELECT DISTINCT Salary FROM Employee ORDER BY Salary DESC LIMIT N,1
);
END

 注意:我刚开始使用的是LIMIT N-1,1 但是will cause error,经过了解才发现

Seems like MySQL can only take numeric constants in the LIMIT syntax. Directly from MySQL documentation:

The LIMIT clause can be used to constrain the number of rows returned by the SELECT statement. LIMIT takes one or two numeric arguments, which must both be nonnegative integer constants (except when using prepared statements).

二、Consecutive Numbers

Write a SQL query to find all numbers that appear at least three times consecutively.

+----+-----+
| Id | Num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+

For example, given the above Logs table, 1 is the only number that appears consecutively for at least three times.

分析:题意为编写SQL去查询所有至少连续出现3次的数字。例如,给定上面的Logs表,1是唯一至少连续出现3次的数字。

代码:

使用join就好了

# Write your MySQL query statement below
SELECT DISTINCT a.Num As ConsecutiveNumbers
FROM Logs a
JOIN Logs b
ON a.Num = b.Num
JOIN Logs c
ON b.Num = c.Num
WHERE a.Id + 1 = b.Id
AND b.Id + 1 = c.Id

其他可参考解法:

# Write your MySQL query statement below
SELECT DISTINCT Num FROM (
SELECT Num, COUNT(Rank) AS Cnt FROM (
SELECT Num,
@curRank := @curRank + IF(@prevNum = Num, 0, 1) AS rank, @prevNum := Num
FROM Logs s, (SELECT @curRank := 0) r, (SELECT @prevNum := NULL) p
ORDER BY ID ASC
) t GROUP BY Rank HAVING Cnt >= 3
) n;

此解法配合使用MySQL用户定义变量和聚组函数统计连续出现的数字个数:

以题目描述的Logs表为例,上面的SQL语句中,最内层的SELECT语句执行结果如下:

+-----+------+-----------------+
| Num | rank | @prevNum := Num |
+-----+------+-----------------+
| 1 | 1 | 1 |
| 1 | 1 | 1 |
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 1 | 3 | 1 |
| 2 | 4 | 2 |
| 2 | 4 | 2 |
+-----+------+-----------------+

执行结果中的rank列将Num转化为从1开始递增的序号,但序号只在Num出现变化时增加,(连续出现的相同数字序号也相同)

第二层SELECT语句对rank进行计数,并只保留计数不小于3的条目,执行结果为:

+-----+-----+
| Num | Cnt |
+-----+-----+
| 1 | 3 |
+-----+-----+

最外层SELECT语句对Num进行去重。

三、Department Highest Salary

The Employee table holds all employees. Every employee has an Id, a salary, and there is also a column for the department Id.

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

The Department table holds all departments of the company.

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

Write a SQL query to find employees who have the highest salary in each of the departments. For the above tables, Max has the highest salary in the IT department and Henry has the highest salary in the Sales department.

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

分析:题意为

雇员表Employee保存了雇员的Id,姓名,薪水以及部门Id。

部门表Department保存了部门的Id和名称。

编写一个SQL查询,找出每一个部门中薪水最高的员工信息。而且给出了样例结果。

代码:

可以先用临时表m查找出每个部门薪水最大值,然后使用薪水值和部门Id与表Employee进行内连接,再通过部门Id与表Dempartment做内连接即可。

# Write your MySQL query statement below
SELECT d.Name AS Department, e.Name AS Employee, m.Salary FROM
Employee e
INNER JOIN
(SELECT DepartmentId, MAX(Salary) AS Salary FROM Employee GROUP BY DepartmentId) m
USING(DepartmentId, Salary)
INNER JOIN
Department d
ON d.Id = m.DepartmentId

或者这么写:

select d.Name as Department, e.Name as Employee, e.Salary as Salary
from Employee e, Department d ,
(select DepartmentId ,MAX(Salary) as Salary from Employee group by DepartmentId ) m
where e.DepartmentId=m.DepartmentId and e.Salary=m.Salary and d.Id=e.DepartmentId;

其他解法:

SELECT D.Name AS Department, E.Name AS Employee, E.Salary AS Salary
FROM Employee AS E, Department AS D
WHERE E.DepartmentId = D.Id AND Salary >= ALL(
SELECT Salary
FROM Employee E_TMP
WHERE E_TMP.DepartmentId = E.DepartmentId);

  

leetcode Database3(Nth Highest Salary<—>Consecutive Numbers<—>Department Highest Salary)的更多相关文章

  1. leetcode - database - 177&period; Nth Highest Salary (Oracle)

    题目链接:https://leetcode.com/problems/nth-highest-salary/description/ 题意:查询出表中工资第N高的值 思路: 1.先按照工资从高到低排序 ...

  2. &lbrack;LeetCode&rsqb; Consecutive Numbers 连续的数字 --数据库知识&lpar;mysql&rpar;

    1. 题目名称   Consecutive Numbers 2 .题目地址 https://leetcode.com/problems/consecutive-numbers/ 3. 题目内容 写一个 ...

  3. &lbrack;LeetCode&rsqb; Department Highest Salary 系里最高薪水

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

  4. Leetcode之深度优先搜索(DFS)专题-129&period; 求根到叶子节点数字之和(Sum Root to Leaf Numbers)

    Leetcode之深度优先搜索(DFS)专题-129. 求根到叶子节点数字之和(Sum Root to Leaf Numbers) 深度优先搜索的解题详细介绍,点击 给定一个二叉树,它的每个结点都存放 ...

  5. 【LeetCode】129&period; Sum Root to Leaf Numbers 解题报告(Python)

    [LeetCode]129. Sum Root to Leaf Numbers 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https://leetcode.com/pr ...

  6. 【LeetCode】659&period; Split Array into Consecutive Subsequences 解题报告(Python)

    [LeetCode]659. Split Array into Consecutive Subsequences 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id ...

  7. LeetCode Database&colon; Consecutive Numbers

    Consecutive Numbers Write a SQL query to find all numbers that appear at least three times consecuti ...

  8. &lbrack;LeetCode&rsqb; Department Highest Salary -- 数据库知识&lpar;mysql&rpar;

    184. Department Highest Salary The Employee table holds all employees. Every employee has an Id, a s ...

  9. LeetCode DB&colon; Department Highest Salary

    The Employee table holds all employees. Every employee has an Id, a salary, and there is also a colu ...

随机推荐

  1. selenium 封装代码

    package pers.xeon.automate.auxt; import org.openqa.selenium.By; import org.openqa.selenium.WebElemen ...

  2. max&lowbar;size&comma; capacity and size 的区别

    The max_size() function returns the maximum number of elements that the container can hold. The max_ ...

  3. 操作数据&lpar;insert、update、delete&rpar;

    插入数据 使用Insert Into 插入 if(exists(select * from sys.databases where name = 'webDB')) drop database web ...

  4. 【转】IOS 怎么获取外设的广播数据AdvData

    原文网址:http://www.deyisupport.com/question_answer/wireless_connectivity/bluetooth/f/103/t/73443.aspx N ...

  5. dubbo初体验

    最近需要开发部门中某个大数据量的提取的功能,加到了一个ElasticSearch的群.在群里听说到一个框架叫dubbo,阿里系开源软件.听到群友谈的神乎其神的,什么什么功能切分多协议栈,高并发等等等. ...

  6. swagger-codegen自动生成代码工具的介绍与使用

    一.Swagger Codegen简介 Swagger Codegen是一个开源的代码生成器,根据Swagger定义的RESTful API可以自动建立服务端和客户端的连接.Swagger Codeg ...

  7. 第六节,Neural Networks and Deep Learning 一书小节&lpar;下&rpar;

    4.神经网络可以计算任何函数的可视化证明 神经网络拥有一定的普遍性,即包含一个隐藏层的神经网络可以被用来按照任意给定的精度来近似任何连续函数. 这一章使用一个实例来阐述神经网络是如何来近似一个一元函数 ...

  8. logback的使用和logback&period;xml详解,在Spring项目中使用log打印日志

    logback的使用和logback.xml详解 一.logback的介绍 Logback是由log4j创始人设计的另一个开源日志组件,官方网站: http://logback.qos.ch.它当前分 ...

  9. &lbrack;持续更新&rsqb;Python 笔记

    本文以 Python 2.7 为基础. lambda 函数实现递归 方法一:传递一个 self 参数 求阶乘: frac = lambda self, x: self(self, x - 1) * x ...

  10. Oracle 数据库字典 sys&period;col&dollar; 表中关于type&num;的解释

    sys.col$ 表是oracle基础数据字典表中的列表,表中描述了数据库中各列信息,其中type#是列的数据类型.以下表格说明了各个数值的含义,以供参考. 值 说明 1 如果列 charsetfor ...