Top-K Salaries Per Department
Updated
Problem statement
The table employees contains employee salaries and department assignments. The table query_parameters contains exactly one row with the requested value k.
For each department, rank its distinct salary levels from highest to lowest using dense ranks. Return every employee whose salary rank is at most k, including all employees tied at a qualifying salary.
Return department_id, employee_id, employee_name, salary, and salary_rank. Order rows by department_id ascending, then salary descending, then employee_id ascending. Do not use LIMIT.
Table schema
Use the same input data with any supported language. Open the Schema tab in the editor to see the generated SQL setup or Pandas DataFrames.
employees
One row per employee.
| Column | Type | Nullable | Description |
|---|---|---|---|
| employee_idPK | Integer | No | — |
| employee_name | Text | No | — |
| department_id | Integer | No | — |
| salary | Integer | No | — |
query_parameters
Exactly one row containing the requested number of distinct salary levels.
| Column | Type | Nullable | Description |
|---|---|---|---|
| k | Integer | No | — |
Expected result
Your query or function must return these columns.
| Column | Type | Nullable | Description |
|---|---|---|---|
| department_id | Integer | No | — |
| employee_id | Integer | No | — |
| employee_name | Text | No | — |
| salary | Integer | No | — |
| salary_rank | Integer | No | — |
Row order: must match exactly. Numeric tolerance: 0.
Constraints
1 <= employees.length <= 200000employee_idis unique.department_id,employee_name, andsalaryare non-NULL.0 <= salary <= 1000000000query_parameterscontains exactly one row and1 <= k <= 100.
More Infosys Onsite Interview questions
- Minimum Coins for a TargetONSITE INTERVIEW · Seen Nov 2026
- Delete Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Find Duplicate Employee RecordsONSITE INTERVIEW · Seen Nov 2026
- Minimum Insertion Steps to Make a String PalindromeONSITE INTERVIEW · Seen Nov 2026
- Maximize the Even-Odd Index Product GapONSITE INTERVIEW · Seen Oct 2026
- Minimum Nonadjacent Flips for an Alternating Binary StringONSITE INTERVIEW · Seen Oct 2026
- Tenth-Highest Distinct SalaryONSITE INTERVIEW · Seen Oct 2026
- Assign Cookies With Matching ParityONSITE INTERVIEW · Seen Sep 2026
- Count Equal Code Pairs Within Distance KONSITE INTERVIEW · Seen Sep 2026
- Daily Successful Transactions Per DepartmentONSITE INTERVIEW · Seen Sep 2026
Similar questions at other companies
- Daily USD Totals with Effective-Dated Exchange RatesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Most Recent Order for Each CustomerCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Fifth Highest Salary In A DepartmentZS · ONSITE INTERVIEWZS interview questions
- In-Memory Database QueriesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
- Order Book Data ValidationJane Street · ONSITE INTERVIEWJane Street interview questions
- Nth-Highest Distinct Salary Without Ranking FunctionsMastercard · ONSITE INTERVIEWMastercard interview questions
- Contest Participants with Long Totals and Short QuestionsThe Walt Disney Company · ONSITE INTERVIEWThe Walt Disney Company interview questions
- First Order per Merchant per DateAmazon · ONSITE INTERVIEW
- Block Transaction Count TrendsChainalysis · ONSITE INTERVIEWChainalysis interview questions
- Third-Highest Distinct SalaryExpedia · ONSITE INTERVIEWExpedia interview questions