SQL Aggregations SQL Topic exerciseMediumVerified answerSQLite + PostgreSQL + MySQL + SQL Server live · 1 guided

Recurring February Event Types

Return each February 2025 event type having at least two rows, sorted by count descending and name ascending.

  • Aggregation
  • HAVING
  • Date analysis
  • Filtering
  • Sorting

Exercise brief

Understand the request

Product telemetry analyst The February review needs recurring event types within an exact month boundary.

Return February event types whose filtered ingestion population contains at least two rows.

Return

  • Return event_name, event_count in this exact left-to-right order.

Constraints

  • Use a half-open February interval in WHERE.
  • Use HAVING COUNT(*) >= 2; do not deduplicate business_event_id.

Data you will use

Review the relevant tables before deciding how to join, filter, or aggregate them.

events

  • event_row_idINTEGER
  • event_nameVARCHAR(50)
  • occurred_atDATETIME

Hints, when you need them

Open one clue at a time so you still do the reasoning.

Hint 1

First establish the February row population; only then group and assess event frequency.

Hint 2

Use an inclusive lower timestamp and exclusive March boundary in WHERE, then apply the count threshold in HAVING.

Hint 3

SELECT event_name, COUNT(*) AS event_count FROM events WHERE /* half-open February interval */ GROUP BY event_name HAVING /* grouped row threshold */ ORDER BY event_count DESC, event_name;

Verified SQL answer

Attempt the problem first, then compare structure and reasoning—not just syntax.

Reveal solution and explanation
SELECT event_name, COUNT(*) AS event_count FROM events WHERE occurred_at >= '2025-02-01 00:00:00' AND occurred_at < '2025-03-01 00:00:00' GROUP BY event_name HAVING COUNT(*) >= 2 ORDER BY event_count DESC, event_name;

Why this works

Correctness: WHERE establishes the February row population before GROUP BY, and HAVING keeps only event groups meeting the aggregate threshold. Edge case: a row at exactly 2025-03-01 00:00:00 must be excluded; using an inclusive upper boundary would incorrectly make checkout qualify. Portability: half-open timestamp predicates and standard HAVING semantics are supported across the live engines.

Success check

Every returned type has at least two in-bound rows.

Expected result

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

event_nameevent_count
page_view7

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.