Query Top Ten Percent Department Salaries
Find the employees whose salary is in the top 10% of their department.
sql
SELECT id, name, department_id, salary FROM ( SELECT id, name, department_id, salary, PERCENT_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) as rnk FROM employees ) t WHERE rnk <= 0.1;
This query uses the PERCENT_RANK() window function to calculate the relative rank of each employee's salary within their respective department and filters for those in the top 10%.
PromptDB can make mistakes. Please double-check responses.