有人可以解释以下获取“n"个最高薪水的 SQL 查询是如何工作的 [英] Can somebody explain how the following SQL query to get the 'n' highest salary works

查看:24
本文介绍了有人可以解释以下获取“n"个最高薪水的 SQL 查询是如何工作的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

选择 *来自员工 AWHERE 3=(选择计数(*) +1来自员工 B其中 B.salary > A.salary)

SELECT * FROM employee A WHERE 3=(select count(*) +1 from employee B where B.salary > A.salary)

这获得了第三高的薪水;有人可以解释这个查询背后的逻辑以及它是如何工作的.

This gets the 3rd highest salary; can somebody explain the logic behind this query and how it works.

推荐答案

换句话说,此查询将是选择拥有其他两个薪水更高的员工的员工".所以,结果是工资第三高的员工.

In words, this query would be "Select the employee who has two other people with a greater salary." So, the result is the employee with the third highest salary.

请注意,如果有两个或更多人的薪水完全相同,则此查询可能会失败.

Note that this query can fail if there are two or more people with the exact same salary.

这篇关于有人可以解释以下获取“n"个最高薪水的 SQL 查询是如何工作的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆