Top N with ORDER BY DESC + LIMIT
Return the top 3 highest-paid employees.
- CTEs
- Sorting
- Top-N
Interview brief
Understand the request
Compensation analyst The pay dashboard only shows the top earners in its preview card.
List the top 3 highest-paid employees with a deterministic tie-breaker (employee_id ascending). Show first_name, last_name, salary, employee_id.
Return
- Return first_name, last_name, and salary.
Constraints
- Use ORDER BY salary DESC with LIMIT 3.
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
employees
first_nameTEXTlast_nameTEXTsalaryDECIMALemployee_idINTEGER
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
For Top-N queries: ORDER BY <metric> DESC, then LIMIT N keeps the first N highest values.
Hint 2
Always add a unique tie-breaker column. Without it, ties at position N can spill into position N+1 unpredictably.
Hint 3
ORDER BY salary DESC, employee_id LIMIT 3
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT first_name, last_name, salary, employee_id FROM employees ORDER BY salary DESC, employee_id LIMIT 3;Why this works
The salary, employee_id pair makes the result reproducible across engines and across re-runs. If two employees share the highest salary (Mike at 80000 here is unique, but in larger data this matters), the tie-breaker decides who appears in the Top 3.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| first_name | last_name | salary | employee_id |
|---|---|---|---|
| Mike | Johnson | 80000 | 3 |
| Amy | Taylor | 78000 | 8 |
| Kevin | Martinez | 76000 | 13 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics: