SQL
All Top 50 SQL Interview Questions#42
Department Top Three Salaries in SQL
HardAmazonInterview Question #42
Asked at AmazonGiven `employee` (id, name, salary, departmentId) and `department` (id, name), write a query to find employees who earn a salary in the top 3 unique salaries of their department.
Input Table: employee & department
6 rows preview| emp_name | salary | department_name |
|---|---|---|
| Joe | 85000 | IT |
| Henry | 80000 | IT |
| Sam | 60000 | IT |
| Max | 90000 | IT |
| Janet | 69000 | Sales |
| Randy | 85000 | Sales |
Expected Output Structure5 rows
| Department | Employee | Salary |
|---|---|---|
| IT | Max | 90000 |
| IT | Joe | 85000 |
| IT | Henry | 80000 |
| Sales | Randy | 85000 |
| Sales | Janet | 69000 |
Interview Context
Asked frequently in data analyst and business analyst technical rounds. Focus on clean filtering, optimal indexing usage, and unambiguous column selection.
PostgreSQL 15
Ready to execute
Click "Run Query" or press Ctrl + Enter to test your query.
Output will be verified against the test dataset.