Standard SQL: FETCH FIRST N ROWS ONLY
Get the top 3 highest-paid employees. The ANSI-standard form is `FETCH FIRST … ROWS ONLY` (shown as a comment in the editor); the canonical solution here uses LIMIT for cross-engine compatibility. Tie-breaker: employee_id ascending. Show first_name, last_name, salary, employee_id.
- Sorting
- Top-N
Interview brief
Understand the request
Get the top 3 highest-paid employees. The ANSI-standard form is `FETCH FIRST … ROWS ONLY` (shown as a comment in the editor); the canonical solution here uses LIMIT for cross-engine compatibility. Tie-breaker: employee_id ascending. Show first_name, last_name, salary, employee_id.
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
ANSI form: ORDER BY ... FETCH FIRST N ROWS ONLY. Engines: PostgreSQL, Oracle, DB2, SQL Server 2012+.
Hint 2
MySQL has NOT adopted FETCH FIRST. SQLite supports it only on builds compiled with that option (often off). LIMIT N is the universal fallback.
Hint 3
ORDER BY salary DESC, employee_id LIMIT 3 -- or FETCH FIRST 3 ROWS ONLY on PG/SQL Server
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 ANSI standard is the long-term safe choice for new SQL on enterprise databases. Use LIMIT for MySQL/SQLite, FETCH FIRST on PostgreSQL/SQL Server/Oracle. This question pins the canonical solution to LIMIT so it grades consistently across engines.
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: