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

相关推荐

27届毕业,最近想找一段大厂实习,感觉简历有些问题,好多都不给面,求大佬们指点,最近好焦虑
重生之我学Java干...:我从后端的角度分析一下你的第一个项目,我感觉亮点不是很突出。因为我是因为组内有需求,临时上手学react干活。我用到的技术基本就cover你那个智慧园区管理平台的很多亮点了。那作为比较专业的前端,你上述的内容是不是有点单薄呢。感觉还得包装
点赞 评论 收藏
分享
点赞 评论 收藏
分享
rndguy:个人思路,抛砖引玉。 要我的话我先问清楚需求:要什么精度,什么速度,什么环境。 如果精度要求很低,平台也有点柔性的话,只需要输出pwm,然后开个中断记录各多少个脉冲,如果脉冲时间不对齐了就反馈控制电流加减就行。要求同步要求稍微高点的话可以在脉冲间做个线性插值,同步精度会高些。 但总体来说,如果直流有刷只有脉冲没有好的编码器的话很难做精准定位什么的(除非用一些电机磁路结构相关的奇技淫巧如高频注入什么的),所以要求更高就需要大量参数辨识和校准,那就慢多了。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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