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

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_idINTEGER
  • event_timeTIMESTAMP
  • streamTEXT
  • severityINTEGER

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_idstreamseverity
101payments5
104orders5
103inventory4

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.