SQL Server: TOP N
Return the 5 employees with the LOWEST salary using SQL Server's `TOP N` syntax (write the canonical SQLite version with LIMIT here; the SQL Server variant is in solutionsByEngine). Tie-breaker: employee_id ascending. Show first_name, last_name, salary, employee_id.
- CTEs
- Sorting
- Top-N
Interview brief
Understand the request
Return the 5 employees with the LOWEST salary using SQL Server's `TOP N` syntax (write the canonical SQLite version with LIMIT here; the SQL Server variant is in solutionsByEngine). 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
SQL Server: SELECT TOP N ... FROM ... ORDER BY ... (TOP is between SELECT and the column list).
Hint 2
TOP N is non-standard but widely used in Microsoft shops. Knowing it is essential for SQL Server work.
Hint 3
SELECT TOP 5 ... FROM employees ORDER BY salary, employee_id;
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, employee_id LIMIT 5;Why this works
On SQL Server you can also write `TOP (N)` with a variable: `SELECT TOP (@n) …`. Without parentheses TOP only accepts a literal. There is also `TOP N PERCENT` for "the top 10% of rows".
Expected result
Use this output to verify values, aliases, ordering, and row count.
| first_name | last_name | salary | employee_id |
|---|---|---|---|
| Emma | Thomas | 52000 | 10 |
| Jane | Smith | 55000 | 2 |
| Nicole | Rodriguez | 56000 | 14 |
| Lisa | Davis | 58000 | 6 |
| Tom | Wilson | 62000 | 7 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics: