题解 | #获取当前薪水第二多的员工的emp_no以及其对应的薪水salary#

So 1. In MySQL and SQL servers like PostgreSQL, AGG function and single columns cannot be selected at the same time unless the single columns are also in GROUP BY otherwise, it will get the error. So we could have only the AGG function selected and then it will take multiple inputs and give single output. This is why the code below is not working in MySQL: it says no GROUP BY used. but we see several single columns in the SELECT clause with MAX()

SELECT e.emp_no, MAX(s.salary), e.last_name, e.first_name
FROM employees AS e
INNER JOIN salaries AS s
ON e.emp_no=s.emp_no
WHERE salary<(SELECT MAX(salary) FROM salaries)

But the code above works in SQLlite it seems that SQLite has fewer limits than SQL. I do not know why... '

Correct commands: use nested WHERE to filter: second highest is the highest one excluding the highest one.

SELECT e.emp_no,salary, last_name, first_name FROM employees e 
INNER JOIN salaries s 
ON e.emp_no=s.emp_no
WHERE salary = (SELECT MAX(salary) FROM salaries
               WHERE salary<(SELECT MAX(salary) FROM salaries))
);

Extention:
Find the emp_no who has the third-highest or N-highest salary, return the emp_no and the counterpart salary:

SELECT e.emp_no, s.salary, e.last_name, e.first_name
FROM employees AS e
INNER JOIN salaries AS s
ON e.emp_no=s.emp_no
WHERE salary=
(SELECT s1.salary FROM salaries AS s1
INNER JOIN salaries AS s2
ON s1.salary <= s2.salary AND s1.to_date='9999-01-01' and s2.to_date='9999-01-01'
GROUP BY s1.salary
HAVING COUNT(DISTINCT s2.salary)=2
);

!!! We can JOIN ON all comparision operators: =,<=,>=.. and logic operatoer: AND, OR NOT
!!!! the to_date='9999-01-01' is added to ensure all employees are in position. So it is okay to only have s1.salary <= s2.salary then we will have a inner joined table with each s1.salary might be linked to several s2.salary as long as the s1.salary is not the maximum one.
t.e.x salary: [700, 600, 500, 600, 400]
s1.salary ------------ s2.salary
700 -------------------- 700 : 1 as coount, 700 only less then 700
600 ---------------------700 : 3 as count, 600 less than 700, 600
600----------------------600
600----------------------600
500----------------------700: 4 as count, 500 less than 700,600,500
500----------------------600
500----------------------600
500----------------------500
400----------------------700: 5 as count 400 less than 700,600,500,400
...
AFTER The table is joinned and collected all info based on ON condition: s1.salary<=s2.salary
GROUP BY s1.salary
we did not apply AGG on GROUP BY so it is still list all s1.salary original data. No aggregation happens.


filter the GROUP BY category with HAVING COUNT(s2.salary). COUNT s2.salary per category here it is per s1.salary.

so here we apply COUNT on GROUP BY to filter, HAVING statement is filtering aggregation result in conjunction with GROUP BY.
DISTINCT removes the duplicative s2.salary that bigger than s1.salary. So as long as the count distinct number of s2.salary that bigger than s1.salary is 2, then this s1.salary is the second highest salary.

WHERE to_date='9999-01-01' this one has no big effect on filtering. it is adding for ensuring all data is in valid. This is why some solutions uses this one as ON in INNER JOINE but actually it is INNER JOIN by s1.salary<=s2.salary.

Slotuin 2:

SELECT e1.emp_no,t2.salary, e1.last_name, e1.first_name FROM employees e1,
(SELECT s1.emp_no, s1.salary FROM salaries AS s1
WHERE (SELECT COUNT(DISTINCT s2.salary) FROM salaries AS s2 
       WHERE s2.salary >= s1.salary)=2) AS t2
WHERE e1.emp_no=t2.emp_no 

Solution3:

SELECT e1.emp_no,s1.salary, e1.last_name, e1.first_name FROM employees e1
INNER JOIN salaries s1 
ON e1.emp_no=s1.emp_no 
WHERE (SELECT COUNT(DISTINCT s2.salary) FROM salaries AS s2 
       WHERE s2.salary >= s1.salary)=2
全部评论

相关推荐

合不合适,我自己说了才算
码农索隆:hr:“真执着啊,来我公司当法人吧”
点赞 评论 收藏
分享
点赞 评论 收藏
分享
06-13 17:33
门头沟学院 Java
顺序不记了,大致顺序是这样的,有的相同知识点写分开了1.基本数据类型2.基本数据类型和包装类型的区别3.==和equals区别4.ArrayList与LinkedList区别5.hashmap底层原理,put操作时会发生什么6.说出几种树型数据结构7.B树和B+树区别8.jvm加载类机制9.线程池核心参数10.创建线程池的几种方式11.callable与runnable区别12.线程池怎么回收线程13.redis三剑客14.布隆过滤器原理,不要背八股,说说真正使用时遇到了问题没有(我说没有,不知道该怎么回答了)15.堆的内存结构16.自己在写项目时有没有遇见过oom,如何处理,不要背八股,根据真实经验,我说不会17.redis死锁怎么办,watchdog机制如何发现是否锁过期18.如何避免redis红锁19.一个表性别与年龄如何加索引20.自己的项目的QPS怎么测的,有没有真正遇到大数量表21.说一说泛型22.springboot自动装配原理23.springmvc与springboot区别24.aop使用过嘛?动态代理与静态代理区别25.spring循环依赖怎么解决26.你说用过es,es如何分片,怎么存的数据,1000万条数据怎么写入库中27.你说用limit,那么在数据量大之后,如何优化28.rabbitmq如何批次发送,批量读取,答了延迟队列和线程池,都不对29.计网知不知道smtp协议,不知道写了对不对,完全听懵了30.springcloud知道嘛?只是了解反问1.做什么的?短信服务,信息量能到千万级2.对我的建议,基础不错,但是不要只背八股,多去实际开发中理解。面试官人不错,虽然没露脸,但是中间会引导我回答问题,不会的也只是说对我要求没那么高。面完问我在济宁生活有没有困难,最快什么时候到,让人事给我聊薪资了。下午人事打电话,问我27届的会不会跑路,还在想办法如何使我不跑路,不想扣我薪资等。之后我再联系吧,还挺想去的😭,我真不跑路哥😢附一张河科大幽默大专图,科大就是大专罢了
查看30道真题和解析
点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
07-11 11:00
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务