答案 0 :(得分:10)
如果您只想限制结果集中的行数,可以使用LIMIT
,有或没有ORDER BY
:
SELECT department, salary
FROM employees
ORDER BY salary DESC
LIMIT 10
如果要为每个组提供最高值,可以使用标准SQL row_number()
窗口函数。例如,按薪水获得每个部门的前3名员工:
SELECT department, salary
FROM (
SELECT department, salary row_number() OVER (
PARTITION BY department
ORDER BY salary DESC) AS rn
FROM employees
)
WHERE rn <= 3