Decision Point wants to compare compensation among currently active employees. Write a PostgreSQL query that returns every active employee who earns the second highest distinct salary, along with their department.
Requirements
- Consider only employees whose
employment_status is Active and whose salary is not NULL.
- Rank distinct salaries from highest to lowest using a window function. The query should be easy to adapt from the second highest salary to the Nth highest salary.
- Return the employee ID, employee name, department name, and salary for all employees at the selected rank.
- Order the output by employee ID ascending. Explain why
DENSE_RANK is preferable to ROW_NUMBER when tied salaries should share a rank.
Output details
- Name the output columns
employee_id, employee_name, department_name, and salary in that order.
- Exclude active employees whose
department_id has no matching department record.