HSBC wants to review compensation among active employees across its business divisions. Write a PostgreSQL query that ranks employees within each division and returns the highest and second-highest distinct annual salaries, including ties.
Requirements
- Join employees to divisions and consider only active employees with a non-NULL salary.
- Use a window function to rank salaries independently within each division.
- Return all employees whose rank is 1 or 2, ordered by division, rank, and employee name.
Output details
- Return columns
division_name, employee_name, annual_salary, and salary_rank, in that order.
- Order
division_name, salary_rank, and employee_name in ascending order.