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_idINTEGERevent_timeTIMESTAMPstreamTEXTseverityINTEGER
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_id | event_time | stream | severity |
|---|---|---|---|
| 101 | 2025-05-05 10:00:00 | payments | 5 |
| 102 | 2025-05-05 10:00:00 | orders | 3 |
| 103 | 2025-05-05 09:00:00 | inventory | 4 |
| 104 | 2025-05-04 08:00:00 | orders | 5 |
Learn the concepts behind this answer
Strengthen your understanding with these targeted learning topics:
Continue practicing
Build the next SQL skill
ORDER BY & Sorting
Practice deterministic SQL ordering with tie-breakers, custom priorities, NULL placement, expressions, joined data, aggregates, and portable top-N patterns.
Ranking & NTH Value
Solve deterministic ranking, top-N, distribution, positional-frame, and rolling-window problems.
SELECT Statements
Select columns, filter rows, remove duplicates, and order query results.
Open the interactive workspace and practice across SQL topics.