# Event Types Spanning Multiple Months

> Some events span seasons.

Canonical URL: <https://datadriven.io/problems/event_types_spanning_multiple_months>

Domain: SQL · Difficulty: easy · Seniority: L3

## Problem

The analytics team is identifying event types with sustained activity rather than one-off spikes. Which event types appear in at least 2 different calendar months?

## Worked solution and explanation

### Why this problem exists in real interviews

Working with `event_data`, this problem isolates row numbering within partitions combined with nested subqueries. The interviewer expects candidates to articulate why `event_type`, `event_timestamp`, `payload` matter for correctness before touching the keyboard.

---

### Break down the requirements

#### Step 1: Partition by `event_type`

`PARTITION BY event_type` creates groups. Within each group, `ORDER BY event_timestamp ASC` determines the ranking.

#### Step 2: Filter to rank 1

`WHERE rnk = 1` in the outer query selects the target row per group.

---

### The solution

**Row-number for event types spanning multiple**

```sql
SELECT *
FROM (
    SELECT *,
           ROW_NUMBER() OVER (PARTITION BY event_type ORDER BY event_timestamp ASC) AS rnk
    FROM event_data
) ranked
WHERE rnk = 1
ORDER BY event_type
```

> **Cost Analysis**
>
> Window function sorts within each `event_type` partition. An index on `(event_type, event_timestamp)` avoids a full sort.

> **Interviewers Watch For**
>
> The interviewer checks whether you use ROW_NUMBER (one row) vs. RANK/DENSE_RANK (ties) based on the prompt requirements.

> **Common Pitfall**
>
> Using GROUP BY with MIN(event_timestamp) gives the value but not the other columns. ROW_NUMBER gives the full row.

---

## Common follow-up questions

- The `payload` column in `event_data` has roughly 15% NULLs. How does your query handle those rows, and would the result change if NULLs were replaced with zeros? _(Tests whether the candidate understands how NULLs propagate through aggregation functions and whether their WHERE/JOIN conditions implicitly filter them out.)_
- Your window function uses a default frame. What is the implicit frame, and would switching to ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW change anything? _(Tests knowledge of default window frames (RANGE vs ROWS) and when the distinction matters.)_
- `event_id` in `event_data` has ~100M distinct values. What index strategy keeps your query from doing a full table scan? _(Tests whether the candidate can design indexes for high-cardinality columns and understands selectivity.)_
- How would you modify this query to run incrementally as new rows arrive in `event_data` each hour? _(Tests whether the candidate can think about incremental computation vs full recomputation.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/event_types_spanning_multiple_months)
- [SQL Interview Questions](https://datadriven.io/sql-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.