LIMIT & OFFSET SQL Topic exerciseEasyVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Return a Deterministic First Batch

Return event_id, event_time, stream, and severity for the first four events by event_id.

  • Sorting
  • Top-N

Exercise brief

Understand the request

Pipeline operations analyst A validation job needs the first four event records in a repeatable sequence.

List the first 5 employees by employee_id ascending. Show employee_id, first_name, last_name, department_id.

Return

  • Return exactly four rows.
  • Order the final result by event_id ascending.

Constraints

  • Apply the row limit after defining the order.
  • Do not rely on fixture insertion order.

Data you will use

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

page_events

  • event_idINTEGER
  • event_timeTIMESTAMP
  • streamTEXT
  • severityINTEGER

Hints, when you need them

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

Hint 1

A row limit does not define which rows are first.

Hint 2

Use the unique event_id as the stable ordering key.

Hint 3

Place the engine-specific row limit around the ordered result.

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT event_id, event_time, stream, severity FROM page_events ORDER BY event_id LIMIT 4;

Why this works

LIMIT, TOP, and FETCH cap the output but do not create an order. Ordering by the unique event_id first makes the batch reproducible across plans and engines.

Success check

The same four lowest event IDs are returned in the same order.

Expected result

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

event_idevent_timestreamseverity
1012025-05-05 10:00:00payments5
1022025-05-05 10:00:00orders3
1032025-05-05 09:00:00inventory4
1042025-05-04 08:00:00orders5

Learn the concepts behind this answer

Strengthen your understanding with these targeted learning topics:

Continue practicing

SQL Practice Online

Open the interactive workspace and practice across SQL topics.