Return a Stable Top Three
Return event_id, stream, and severity for the three highest-severity events.
- Sorting
- Top-N
Exercise brief
Understand the request
Incident response lead A response queue shows the three most severe events and must resolve severity ties predictably.
List the top 3 highest-paid employees with a deterministic tie-breaker (employee_id ascending). Show first_name, last_name, salary, employee_id.
Return
- Return exactly three rows.
- Order severity descending and event_id ascending.
Constraints
- Use event_id as the cutoff tie-breaker.
- Apply a three-row limit.
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
Sort the business metric before applying the limit.
Hint 2
Equal severities need a unique secondary key, especially at the cutoff.
Hint 3
Use severity DESC, event_id ASC, then take three rows.
Verified SQL answer
Attempt the problem first, then compare structure and reasoning—not just syntax.
Reveal solution and explanation
SELECT event_id, stream, severity FROM page_events ORDER BY severity DESC, event_id LIMIT 3;Why this works
A top-N query is deterministic only when its ORDER BY defines a total order. The fixture has several severity-4 rows at the cutoff, so event_id decides which one enters the top three.
Success check
The same three events are selected even when several rows share the cutoff severity.
Expected result
Use this output to verify values, aliases, ordering, and row count.
| event_id | stream | severity |
|---|---|---|
| 101 | payments | 5 |
| 104 | orders | 5 |
| 103 | inventory | 4 |
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.