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

Calculate and Return Page Two

Return page two of page_events with page size four, ordered by event_time descending and event_id descending.

  • Sorting
  • Top-N

Exercise brief

Understand the request

Operations dashboard engineer An event feed displays four rows per page using newest-first offset pagination.

Skip the first 5 employees by employee_id and return the next 5. Show first_name, last_name, salary, employee_id.

Return

  • Return rows five through eight of the ordered feed.
  • Return event_id, event_time, stream, and severity.

Constraints

  • Calculate OFFSET as (page - 1) × page_size.
  • Use event_id to resolve equal timestamps.

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

Page two starts after one complete page.

Hint 2

For page 2 and page size 4, the offset is 4.

Hint 3

Order by event_time DESC, event_id DESC before applying OFFSET and the row limit.

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_time DESC, event_id DESC LIMIT 4 OFFSET 4;

Why this works

Offset pagination uses OFFSET = (page - 1) × page_size. The unique secondary key is essential because several event timestamps tie across page boundaries.

Success check

The query returns exactly the second deterministic four-row page.

Expected result

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

event_idevent_timestreamseverity
1042025-05-04 08:00:00orders5
1072025-05-03 09:00:00payments4
1062025-05-03 09:00:00orders4
1082025-05-03 08:00:00orders4

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.