We use cookies for site analytics. Accept to help us understand how the site is used. See our Privacy Policy for details.
A medium sql problem, graded against 3 test cases (1 of them hidden).
Query-writing problems - joins, aggregation, window functions, and query tuning.
Reach for it when you see: A schema and a question about the data rather than a function signature.
More SQLproblems →```sql
WITH recent_sessions AS (
SELECT user_id, session_date
FROM Sessions
WHERE session_date BETWEEN ($1::date - INTERVAL '29 days') AND $1::date
),
session_counts AS (
SELECT user_id, COUNT(*) AS n
FROM recent_sessions
GROUP BY user_id
)
SELECT user_id
FROM session_counts
WHERE n >= 3
ORDER BY user_id;
```
Why CTEs help: each step is a named, easily-reasoned-about subquery. You could collapse this into one query, but at 5+ steps the readability win is real.
Performance note: in older Postgres (<12) CTEs were optimization fences (always materialized). 12+ inlines them by default unless you use `AS MATERIALIZED`. SQL Server and MySQL inline; Oracle has its own knobs.
The full reference solution in every supported language stays in the editor above - reveal it there once you have had a real attempt.
Read off this problem's own test suite, so these are the cases a submission actually has to survive.
These apply to the pattern as a whole, not just this problem.