跳到内容
当前位置:首页>央企考试真题>SQL面试高频题:如何查询每个部门薪资前三的员工?(大厂必考)

SQL面试高频题:如何查询每个部门薪资前三的员工?(大厂必考)

  • 2026-10-17 08:11:18
SQL面试高频题:如何查询每个部门薪资前三的员工?(大厂必考)

SQL面试高频题:如何查询每个部门薪资前三的员工?(大厂必考)

💡 导读

大家好,我是蔡老师。

在SQL面试中,「分组TopN问题」 是出现频率最高的考点之一,不管是数据分析、大数据开发、Java后端岗,10家公司有8家都会考。

很多同学一听到“分组+排名”就慌,其实这类题有固定解题模板,掌握了窗口函数,直接套用就能拿满分。

今天就用 「各部门薪资Top3员工」 这道最经典的题,给大家讲透分组排名的用法,看完这篇,所有TopN类问题都能秒解!

💡 文末福利:后台回复【SQL】领取《SQL面试高频50题》,窗口函数、分组TopN、连续访问题全覆盖!


🎯 面试真题

题目要求

有一张员工表 employees,包含员工ID、姓名、部门、薪资。 需求:查询出每个部门中,薪资排名前三的员工信息。

表结构

CREATE TABLE employees (    emp_id INT,             -- 员工ID    emp_name VARCHAR(20),   -- 员工姓名    department VARCHAR(20),-- 部门    salary INT-- 薪资);

测试数据

INSERT INTO employees VALUES(1, 'Alice', '研发', 28000),(2, 'Bob',   '研发', 26000),(3, 'Cindy', '研发', 25000),(4, 'David', '研发', 22000),(5, 'Eva',   '运营', 20000),(6, 'Frank', '运营', 19000),(7, 'Grace', '运营', 18000),(8, 'Helen', '运营', 16000);

🔑 解题思路(3步万能模板)

这类题只有一种标准答案写法:子查询 + 窗口函数。

核心步骤

  1. 分区:用 PARTITION BY department 按部门分组,让每个部门单独排名;
  2. 排序:用 ORDER BY salary DESC 按薪资从高到低排序;
  3. 排名:用 ROW_NUMBER() 生成行号,外层筛选 <=3 即可。

✅ 标准答案(直接背)

SELECT emp_id, emp_name, department, salaryFROM (-- 子查询:给每个部门的员工生成薪资排名SELECT        emp_id,        emp_name,        department,        salary,-- 核心:按部门分区,按薪资倒序排名ROW_NUMBER() OVER (PARTITION BY department ORDER BY salary DESC        ) AS rank_numFROM employees) t-- 外层筛选:只保留排名前三WHERE rank_num <= 3;

📊 运行结果

emp_id
emp_name
department
salary
1
Alice
研发
28000
2
Bob
研发
26000
3
Cindy
研发
25000
5
Eva
运营
20000
6
Frank
运营
19000
7
Grace
运营
18000

完美实现:每个部门只保留薪资前3名员工。


🚀 面试必问:3个排名函数区别(高频考点)

面试官一定会追问: ROW_NUMBER()、RANK()、DENSE_RANK() 有什么区别?

一句话总结

  • ROW_NUMBER():连续不重复排名(1,2,3,4)
  • RANK():跳跃排名(1,2,2,4)
  • DENSE_RANK():连续并列排名(1,2,2,3)

业务场景怎么选?

  1. 必须取前3个人 → 用 ROW_NUMBER()
  2. 薪资相同算并列,且允许超过3条 → 用 RANK()
  3. 并列不跳号,前三档都要 → 用 DENSE_RANK()

📌 面试加分回答

你可以主动跟面试官说:

“这道题是标准的分组TopN问题,最优解法是使用窗口函数。先用 PARTITION BY 按部门分组,ORDER BY 按薪资排序,再用 ROW_NUMBER() 生成行号,最后外层筛选 <=3。如果业务允许并列薪资都进入前三,我会把函数换成 RANK() 或 DENSE_RANK()。”

这段话一说,面试官直接觉得你很专业!


📢 写在最后

分组TopN是SQL面试最基础、最常考的题型,没有之一。 只要记住: PARTITION BY 分组 + ORDER BY 排序 + 排名函数 + 外层筛选 所有同类题都能秒杀!

如果这篇对你有帮助,欢迎点赞+在看,我会持续更新大厂SQL面试真题!

👉 免费领取资料

关注公众号,后台回复:【SQL】 领取《SQL面试高频50题》【HR】 领取《人事与背景调查面试题参考答案》【进群】 加入大数据面试交流群,每日真题打卡!

基本 文件 流程 错误 SQL 调试
  1. 请求信息 : 2026-10-25 04:32:51 HTTP/1.1 GET : http://c.15386.cn/a/458167.html
  2. 运行时间 : 0.078760s [ 吞吐率:12.70req/s ] 内存消耗:4,538.11kb 文件加载:140
  3. 缓存信息 : 0 reads,0 writes
  4. 会话信息 : SESSION_ID=20ae5aabc70c02c53c9efba661eaeb93
  1. /www/wwwroot/c.15386.cn/public/index.php ( 0.79 KB )
  2. /www/wwwroot/c.15386.cn/vendor/autoload.php ( 0.17 KB )
  3. /www/wwwroot/c.15386.cn/vendor/composer/autoload_real.php ( 2.49 KB )
  4. /www/wwwroot/c.15386.cn/vendor/composer/platform_check.php ( 0.90 KB )
  5. /www/wwwroot/c.15386.cn/vendor/composer/ClassLoader.php ( 14.03 KB )
  6. /www/wwwroot/c.15386.cn/vendor/composer/autoload_static.php ( 4.90 KB )
  7. /www/wwwroot/c.15386.cn/vendor/topthink/think-helper/src/helper.php ( 8.34 KB )
  8. /www/wwwroot/c.15386.cn/vendor/topthink/think-validate/src/helper.php ( 2.19 KB )
  9. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/helper.php ( 1.47 KB )
  10. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/stubs/load_stubs.php ( 0.16 KB )
  11. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Exception.php ( 1.69 KB )
  12. /www/wwwroot/c.15386.cn/vendor/topthink/think-container/src/Facade.php ( 2.71 KB )
  13. /www/wwwroot/c.15386.cn/vendor/symfony/deprecation-contracts/function.php ( 0.99 KB )
  14. /www/wwwroot/c.15386.cn/vendor/symfony/polyfill-mbstring/bootstrap.php ( 8.26 KB )
  15. /www/wwwroot/c.15386.cn/vendor/symfony/polyfill-mbstring/bootstrap80.php ( 9.78 KB )
  16. /www/wwwroot/c.15386.cn/vendor/symfony/var-dumper/Resources/functions/dump.php ( 1.49 KB )
  17. /www/wwwroot/c.15386.cn/vendor/topthink/think-dumper/src/helper.php ( 0.18 KB )
  18. /www/wwwroot/c.15386.cn/vendor/symfony/var-dumper/VarDumper.php ( 4.30 KB )
  19. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/App.php ( 15.30 KB )
  20. /www/wwwroot/c.15386.cn/vendor/topthink/think-container/src/Container.php ( 15.76 KB )
  21. /www/wwwroot/c.15386.cn/vendor/psr/container/src/ContainerInterface.php ( 1.02 KB )
  22. /www/wwwroot/c.15386.cn/app/provider.php ( 0.19 KB )
  23. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Http.php ( 6.04 KB )
  24. /www/wwwroot/c.15386.cn/vendor/topthink/think-helper/src/helper/Str.php ( 7.29 KB )
  25. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Env.php ( 4.68 KB )
  26. /www/wwwroot/c.15386.cn/app/common.php ( 0.03 KB )
  27. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/helper.php ( 18.78 KB )
  28. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Config.php ( 5.54 KB )
  29. /www/wwwroot/c.15386.cn/config/app.php ( 0.95 KB )
  30. /www/wwwroot/c.15386.cn/config/cache.php ( 0.78 KB )
  31. /www/wwwroot/c.15386.cn/config/console.php ( 0.23 KB )
  32. /www/wwwroot/c.15386.cn/config/cookie.php ( 0.56 KB )
  33. /www/wwwroot/c.15386.cn/config/database.php ( 2.48 KB )
  34. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/facade/Env.php ( 1.67 KB )
  35. /www/wwwroot/c.15386.cn/config/filesystem.php ( 0.61 KB )
  36. /www/wwwroot/c.15386.cn/config/lang.php ( 0.91 KB )
  37. /www/wwwroot/c.15386.cn/config/log.php ( 1.35 KB )
  38. /www/wwwroot/c.15386.cn/config/middleware.php ( 0.19 KB )
  39. /www/wwwroot/c.15386.cn/config/route.php ( 1.89 KB )
  40. /www/wwwroot/c.15386.cn/config/session.php ( 0.57 KB )
  41. /www/wwwroot/c.15386.cn/config/trace.php ( 0.34 KB )
  42. /www/wwwroot/c.15386.cn/config/view.php ( 0.82 KB )
  43. /www/wwwroot/c.15386.cn/app/event.php ( 0.25 KB )
  44. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Event.php ( 7.67 KB )
  45. /www/wwwroot/c.15386.cn/app/service.php ( 0.13 KB )
  46. /www/wwwroot/c.15386.cn/app/AppService.php ( 0.26 KB )
  47. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Service.php ( 1.64 KB )
  48. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Lang.php ( 7.35 KB )
  49. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/lang/zh-cn.php ( 13.70 KB )
  50. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/initializer/Error.php ( 3.31 KB )
  51. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/initializer/RegisterService.php ( 1.33 KB )
  52. /www/wwwroot/c.15386.cn/vendor/services.php ( 0.14 KB )
  53. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/service/PaginatorService.php ( 1.52 KB )
  54. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/service/ValidateService.php ( 0.99 KB )
  55. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/service/ModelService.php ( 2.04 KB )
  56. /www/wwwroot/c.15386.cn/vendor/topthink/think-trace/src/Service.php ( 0.77 KB )
  57. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Middleware.php ( 6.72 KB )
  58. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/initializer/BootService.php ( 0.77 KB )
  59. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/Paginator.php ( 11.86 KB )
  60. /www/wwwroot/c.15386.cn/vendor/topthink/think-validate/src/Validate.php ( 63.20 KB )
  61. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/Model.php ( 23.55 KB )
  62. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/model/concern/Attribute.php ( 21.05 KB )
  63. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/model/concern/AutoWriteData.php ( 4.21 KB )
  64. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/model/concern/Conversion.php ( 6.44 KB )
  65. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/model/concern/DbConnect.php ( 5.16 KB )
  66. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/model/concern/ModelEvent.php ( 2.33 KB )
  67. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/model/concern/RelationShip.php ( 28.29 KB )
  68. /www/wwwroot/c.15386.cn/vendor/topthink/think-helper/src/contract/Arrayable.php ( 0.09 KB )
  69. /www/wwwroot/c.15386.cn/vendor/topthink/think-helper/src/contract/Jsonable.php ( 0.13 KB )
  70. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/model/contract/Modelable.php ( 0.09 KB )
  71. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Db.php ( 2.88 KB )
  72. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/DbManager.php ( 8.52 KB )
  73. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Log.php ( 6.28 KB )
  74. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Manager.php ( 3.92 KB )
  75. /www/wwwroot/c.15386.cn/vendor/psr/log/src/LoggerTrait.php ( 2.69 KB )
  76. /www/wwwroot/c.15386.cn/vendor/psr/log/src/LoggerInterface.php ( 2.71 KB )
  77. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Cache.php ( 4.92 KB )
  78. /www/wwwroot/c.15386.cn/vendor/psr/simple-cache/src/CacheInterface.php ( 4.71 KB )
  79. /www/wwwroot/c.15386.cn/vendor/topthink/think-helper/src/helper/Arr.php ( 16.63 KB )
  80. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/cache/driver/File.php ( 7.84 KB )
  81. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/cache/Driver.php ( 9.03 KB )
  82. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/contract/CacheHandlerInterface.php ( 1.99 KB )
  83. /www/wwwroot/c.15386.cn/app/Request.php ( 0.09 KB )
  84. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Request.php ( 55.78 KB )
  85. /www/wwwroot/c.15386.cn/app/middleware.php ( 0.25 KB )
  86. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Pipeline.php ( 2.61 KB )
  87. /www/wwwroot/c.15386.cn/vendor/topthink/think-trace/src/TraceDebug.php ( 3.40 KB )
  88. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/middleware/SessionInit.php ( 1.94 KB )
  89. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Session.php ( 1.80 KB )
  90. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/session/driver/File.php ( 6.27 KB )
  91. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/contract/SessionHandlerInterface.php ( 0.87 KB )
  92. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/session/Store.php ( 7.12 KB )
  93. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Route.php ( 23.73 KB )
  94. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/route/RuleName.php ( 5.75 KB )
  95. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/route/Domain.php ( 2.53 KB )
  96. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/route/RuleGroup.php ( 22.43 KB )
  97. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/route/Rule.php ( 26.95 KB )
  98. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/route/RuleItem.php ( 9.78 KB )
  99. /www/wwwroot/c.15386.cn/route/app.php ( 1.72 KB )
  100. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/facade/Route.php ( 4.70 KB )
  101. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/route/dispatch/Controller.php ( 4.74 KB )
  102. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/route/Dispatch.php ( 10.44 KB )
  103. /www/wwwroot/c.15386.cn/app/controller/Index.php ( 4.81 KB )
  104. /www/wwwroot/c.15386.cn/app/BaseController.php ( 2.05 KB )
  105. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/facade/Db.php ( 0.93 KB )
  106. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/connector/Mysql.php ( 5.44 KB )
  107. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/PDOConnection.php ( 52.47 KB )
  108. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/Connection.php ( 8.39 KB )
  109. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/ConnectionInterface.php ( 4.57 KB )
  110. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/builder/Mysql.php ( 16.58 KB )
  111. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/Builder.php ( 24.06 KB )
  112. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/BaseBuilder.php ( 27.50 KB )
  113. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/Query.php ( 15.71 KB )
  114. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/BaseQuery.php ( 45.13 KB )
  115. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/concern/TimeFieldQuery.php ( 7.43 KB )
  116. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/concern/AggregateQuery.php ( 3.26 KB )
  117. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/concern/ModelRelationQuery.php ( 20.07 KB )
  118. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/concern/ParamsBind.php ( 3.66 KB )
  119. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/concern/ResultOperation.php ( 7.01 KB )
  120. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/concern/WhereQuery.php ( 19.37 KB )
  121. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/concern/JoinAndViewQuery.php ( 7.11 KB )
  122. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/concern/TableFieldInfo.php ( 2.63 KB )
  123. /www/wwwroot/c.15386.cn/vendor/topthink/think-orm/src/db/concern/Transaction.php ( 2.77 KB )
  124. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/log/driver/File.php ( 5.96 KB )
  125. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/contract/LogHandlerInterface.php ( 0.86 KB )
  126. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/log/Channel.php ( 3.89 KB )
  127. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/event/LogRecord.php ( 1.02 KB )
  128. /www/wwwroot/c.15386.cn/vendor/topthink/think-helper/src/Collection.php ( 16.47 KB )
  129. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/facade/View.php ( 1.70 KB )
  130. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/View.php ( 4.39 KB )
  131. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Response.php ( 8.81 KB )
  132. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/response/View.php ( 3.29 KB )
  133. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/Cookie.php ( 6.06 KB )
  134. /www/wwwroot/c.15386.cn/vendor/topthink/think-view/src/Think.php ( 8.38 KB )
  135. /www/wwwroot/c.15386.cn/vendor/topthink/framework/src/think/contract/TemplateHandlerInterface.php ( 1.60 KB )
  136. /www/wwwroot/c.15386.cn/vendor/topthink/think-template/src/Template.php ( 46.61 KB )
  137. /www/wwwroot/c.15386.cn/vendor/topthink/think-template/src/template/driver/File.php ( 2.41 KB )
  138. /www/wwwroot/c.15386.cn/vendor/topthink/think-template/src/template/contract/DriverInterface.php ( 0.86 KB )
  139. /www/wwwroot/c.15386.cn/runtime/temp/fd49a371ef7e8cd36bdf18e69567f9a7.php ( 8.07 KB )
  140. /www/wwwroot/c.15386.cn/vendor/topthink/think-trace/src/Html.php ( 4.42 KB )
  1. CONNECT:[ UseTime:0.000405s ] mysql:host=172.18.0.4;port=3306;dbname=c_15386;charset=utf8mb4
  2. SHOW FULL COLUMNS FROM `fenlei` [ RunTime:0.000627s ]
  3. SELECT * FROM `fenlei` WHERE `fid` = 0 [ RunTime:0.000360s ]
  4. SELECT * FROM `fenlei` WHERE `fid` = 63 [ RunTime:0.000398s ]
  5. SHOW FULL COLUMNS FROM `set` [ RunTime:0.000580s ]
  6. SELECT * FROM `set` [ RunTime:0.000299s ]
  7. SHOW FULL COLUMNS FROM `article` [ RunTime:0.000690s ]
  8. SELECT * FROM `article` WHERE `id` = 458167 LIMIT 1 [ RunTime:0.000366s ]
  9. UPDATE `article` SET `lasttime` = 1792873972 WHERE `id` = 458167 [ RunTime:0.001074s ]
  10. SELECT * FROM `fenlei` WHERE `id` = 65 LIMIT 1 [ RunTime:0.000338s ]
  11. SELECT * FROM `article` WHERE `id` < 458167 ORDER BY `id` DESC LIMIT 1 [ RunTime:0.000530s ]
  12. SELECT * FROM `article` WHERE `id` > 458167 ORDER BY `id` ASC LIMIT 1 [ RunTime:0.000499s ]
  13. SELECT * FROM `article` WHERE `id` < 458167 ORDER BY `id` DESC LIMIT 10 [ RunTime:0.000883s ]
  14. SELECT * FROM `article` WHERE `id` < 458167 ORDER BY `id` DESC LIMIT 10,10 [ RunTime:0.000677s ]
  15. SELECT * FROM `article` WHERE `id` < 458167 ORDER BY `id` DESC LIMIT 20,10 [ RunTime:0.000541s ]
0.091136s