LIMIT & OFFSET SQL practice problemEasyVerified answer

Preview the First N Rows

Return any 4 employees as a data sample.

  • Sorting
  • Top-N

Interview brief

Understand the request

Data sampling analyst A random-check audit needs a small representative sample of the employee list.

Preview the first 7 employees by employee_id. Show employee_id, first_name, last_name, department_id.

Return

  • Return first_name, last_name, and department_id.

Constraints

  • Use LIMIT to cap the result at 4 rows.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

employees

  • employee_idINTEGER
  • first_nameTEXT
  • last_nameTEXT
  • department_idINTEGER

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

For a deterministic preview, sort by a unique column (employee_id) before limiting.

Hint 2

For a RANDOM sample (different rows each run), use ORDER BY RANDOM() — covered in the ORDER BY scenario.

Hint 3

ORDER BY employee_id LIMIT 7

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 7;

Why this works

Two different intents people lump under 'sample': (1) deterministic preview = ORDER BY <unique> LIMIT N (this question); (2) random sample = ORDER BY RANDOM() LIMIT N. Pick the one your use-case needs — they're not interchangeable.

Expected result

Use this output to verify values, aliases, ordering, and row count.

employee_idfirst_namelast_namedepartment_id
1JohnDoe1
2JaneSmith2
3MikeJohnson1
4SarahWilliams3
5DavidBrown1
6LisaDavis2
7TomWilson3

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics: