题解 | #获取当前薪水第二多的员工的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
全部评论

相关推荐

死在JAVA的王小美:哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈哈,我也是,让我免了一轮,但是硬气拒绝了
点赞 评论 收藏
分享
评论
点赞
收藏
分享
正在热议
# 25届秋招总结 #
442405次浏览 4511人参与
# 春招别灰心,我们一人来一句鼓励 #
41942次浏览 531人参与
# 北方华创开奖 #
107431次浏览 599人参与
# 地方国企笔面经互助 #
7962次浏览 18人参与
# 同bg的你秋招战况如何? #
76670次浏览 561人参与
# 虾皮求职进展汇总 #
115613次浏览 886人参与
# 阿里云管培生offer #
120231次浏览 2219人参与
# 实习,投递多份简历没人回复怎么办 #
2454658次浏览 34857人参与
# 实习必须要去大厂吗? #
55771次浏览 961人参与
# 提前批简历挂麻了怎么办 #
149901次浏览 1977人参与
# 投递实习岗位前的准备 #
1195935次浏览 18548人参与
# 你投递的公司有几家约面了? #
33205次浏览 188人参与
# 双非本科求职如何逆袭 #
662208次浏览 7394人参与
# 如果公司给你放一天假,你会怎么度过? #
4753次浏览 55人参与
# 机械人春招想让哪家公司来捞你? #
157628次浏览 2267人参与
# 如果你有一天可以担任公司的CEO,你会做哪三件事? #
11561次浏览 287人参与
# 发工资后,你做的第一件事是什么 #
12704次浏览 62人参与
# 工作中,努力重要还是选择重要? #
35804次浏览 384人参与
# 参加完秋招的机械人,还参加春招吗? #
20126次浏览 240人参与
# 我的上岸简历长这样 #
452016次浏览 8088人参与
# 实习想申请秋招offer,能不能argue薪资 #
39299次浏览 314人参与
# 非技术岗是怎么找实习的 #
155868次浏览 2120人参与
牛客网
牛客企业服务