Basic LIMIT (with ORDER BY)
Return the first 5 employees from the table.
- CTEs
- Sorting
- Top-N
Interview brief
Understand the request
HR analyst A quick sample of the employee table is needed to validate the export format.
List the first 5 employees by employee_id ascending. Show employee_id, first_name, last_name, department_id.
Return
- Return all columns.
Constraints
- Use LIMIT 5.
Data you will use
Review the relevant tables before deciding how to join, filter, or aggregate them.
employees
employee_idINTEGERfirst_nameTEXTlast_nameTEXTdepartment_idINTEGER
Hints, when you need them
Open one clue at a time so you still do the reasoning.
Hint 1
LIMIT N restricts the result to at most N rows, but it operates on rows already produced — without ORDER BY the engine may return any N rows.
Hint 2
Always pair LIMIT with an ORDER BY that defines what 'first' means (here, employee_id ascending).
Hint 3
SELECT employee_id, first_name, last_name, department_id FROM employees ORDER BY employee_id LIMIT 5;
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT employee_id, first_name, last_name, department_id FROM employees ORDER BY employee_id LIMIT 5;Why this works
The single most common LIMIT bug in production is using it without ORDER BY. Today's query may return employee_ids 1-5; tomorrow, after an index is added or the engine optimiser changes, it could return any 5 rows. Make ORDER BY a habit.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| employee_id | first_name | last_name | department_id |
|---|---|---|---|
| 1 | John | Doe | 1 |
| 2 | Jane | Smith | 2 |
| 3 | Mike | Johnson | 1 |
| 4 | Sarah | Williams | 3 |
| 5 | David | Brown | 1 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics: