Back to Practice
#0337

Days Between First and Last Event

MediumSQL24 min20 XP

Problem

Return user_id and active_span_days as days between first and last event.

Why This Matters

SQL interviews usually test whether you can turn a business question into the right rows, grouping level, join type, and edge-case behavior.

Schema

events(user_id INTEGER, event_date TEXT)

Examples

Example 1
Inputuser 1 active from Jan 1 to Jan 6
Outputuser 1 span is 5 days

JULIANDAY converts dates to numeric day values.

Constraints

  • Return only the requested columns.
  • Use deterministic ORDER BY when multiple rows are returned.
  • Assume SQLite-compatible SQL unless the prompt states otherwise.
CodeSQL
Visible browser tests run here when available.
Testcases1 visible / 3 hidden categories
Core SQL case
Inputuser 1 active from Jan 1 to Jan 6
Expected[[1,5],[2,0]]

JULIANDAY converts dates to numeric day values.

Hidden Test Categories
Empty or missing matchesDuplicate rows or tiesNULL values where the pattern commonly breaks