# One Year to the Next

> This year versus last year. Growing or shrinking?

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

Domain: SQL · Difficulty: hard · Seniority: L5 · Asked in: Airbnb

## Problem

The growth dashboard keeps celebrating signup jumps, but the growth team suspects the gains are piling onto a few busy days instead of spreading across the calendar. From `users`, give each signup year the number of separate days that saw at least one signup, next to the prior year's figure and the change from it as a whole-number percent.

## Tables

### users

- user_id INTEGER
- username TEXT
- email TEXT
- signup_date TEXT
- account_status TEXT
- age_bucket TEXT

Sample rows (10 of 200):

| user_id | username | email | signup_date | account_status | age_bucket |
| --- | --- | --- | --- | --- | --- |
| 100 | alice | alice@example.com | 2025-02-02 | inactive | 25-34 |
| 197 | aaron42 | aaron42@example.com | 2026-03-03 | suspended | 35-44 |
| 294 |  amelia | amelia@example.com | 2024-04-04 | pending_verification | 45-54 |
| 391 | arjun  | arjun@example.com | 2025-05-05 | active | 55-64 |
| 488 | ava99 | ava99@example.com | 2026-06-06 | inactive | 65+ |
| 585 | andrew_k | andrew_k@example.com | 2024-07-07 | suspended | NULL |
| 682 | anika | anika@example.com | 2025-08-08 16:34:00 | pending_verification | 18-24 |
| 779 | aiden | aiden@example.com | 2026-09-09 14:53:00 | active | 25-34 |
| 876 | aria_b | aria_b@example.com | 2024-10-10 | inactive | 35-44 |
| 973 | brian | brian@example.com | 2025-11-11 | suspended | 45-54 |

## Expected output

First 4 of 4 rows:

| signup_year | active_signup_days | prev_year_active_days | yoy_growth_pct |
| --- | --- | --- | --- |
| 2023 | 21 | NULL | NULL |
| 2024 | 49 | 21 | 133 |
| 2025 | 42 | 49 | -14 |
| 2026 | 53 | 42 | 26 |

## Worked solution and explanation

### What this is really asking

This is a count-the-days-not-the-rows problem hiding behind a growth chart, with an integer-division trap stacked on top. The phrase 'signup activity' pulls you toward counting rows, but the metric is how many separate calendar days saw a signup, and many users land on the same day. Count rows and you report signups; you want the number of days those signups fall on, and a day stays one day even when a row happens to carry a time on the end. Then the second trap: a difference of two integer counts divided by an integer count stays an integer, so a real 133 percent jump collapses to a blunt 100 and every smaller swing flattens to 0. Miss either and the numbers look plausible but are wrong.

---

### Break down the requirements

#### Step 1: Count days, not rows or strings

Take the year with `strftime('%Y', signup_date)`, then count each calendar day once with `COUNT(DISTINCT strftime('%Y-%m-%d', signup_date))`. A busy day with many signups is still one day, and so is '2025-08-08 16:34:00': as a raw string it differs from the plain '2025-08-08' another user signed up on, and 2025 would read 43 days instead of 42.

#### Step 2: Pull the prior year inline

Compute `LAG(active_signup_days) OVER (ORDER BY signup_year)` once, in `with_prior_year`, on the yearly aggregate, and reuse the named `prev_year_active_days`. The first year has no predecessor, so it gets `NULL`, and that `NULL` propagates through the division to give a null growth for the earliest row.

#### Step 3: Make the division real, once

Cast the year-over-year difference to `REAL` once, then divide by last year's count, multiply by 100 and round to a whole percent. One real operand is all the division needs; casting the denominator too adds nothing.

---

### The solution

**Active signup days, year over year**

```sql
WITH yearly_signups AS (
  SELECT strftime('%Y', signup_date) AS signup_year,
         COUNT(DISTINCT strftime('%Y-%m-%d', signup_date)) AS active_signup_days
  FROM users
  GROUP BY signup_year
),
with_prior_year AS (
  SELECT signup_year,
         active_signup_days,
         LAG(active_signup_days) OVER (ORDER BY signup_year) AS prev_year_active_days
  FROM yearly_signups
)
SELECT signup_year,
       active_signup_days,
       prev_year_active_days,
       ROUND(
         CAST(active_signup_days - prev_year_active_days AS REAL)
         / prev_year_active_days
         * 100
       ) AS yoy_growth_pct
FROM with_prior_year
```

> **The window runs on four rows, not twenty million**
>
> One full scan of 20M rows to aggregate the distinct dates, then `LAG` over four yearly rows. An index on `signup_date` does not help a full-table aggregate; memory stays tiny because the CTE collapses to one row per year.

> **Placement of the cast is the seniority tell**
>
> Whether you count distinct days rather than rows, where you put the cast (one operand, before the division, not around the finished ratio), how you handle the null first year, and whether you reach for `LAG` over a self-join.

> **A time of day is not a second day**
>
> `signup_date` is text, and two rows were written with a time of day. `COUNT(DISTINCT signup_date)` compares strings, so '2025-08-08 16:34:00' and '2025-08-08' count as two days. Cut every value to its date before counting: it moves 2025 from 43 days to 42, and its growth from -12 to -14 percent.

> **Two integer counts never make a percentage**
>
> Two ways to go wrong on one metric. `COUNT(*)` counts signups, not days, so a year where crowds cluster on a few days looks as active as one that trickled in daily. And dividing without a cast keeps integer math: 28 over 21 becomes 1, so 133 percent reports 100, while the 2025 and 2026 swings both read 0. A suspiciously round 100 or a flat 0 in your output is the tell.

> **Shrink first, then look back**
>
> First instinct is a self-join: `users y1 JOIN users y2 ON y2.year = y1.year - 1`. Logically fine, but you join 20M rows to themselves before aggregating. Aggregate first in a CTE, counting distinct dates per year, then look back one row with `LAG`.

---

### Common follow-up questions

## Common follow-up questions

- How would you filter to only years with negative growth? _(Filter on `yoy_growth_pct` in an outer `SELECT`; `LAG` cannot live in `WHERE`.)_
- What if growth is versus a rolling 3-year average instead? _(Swap `LAG` for `AVG(active_signup_days) OVER (ORDER BY signup_year ROWS BETWEEN 3 PRECEDING AND 1 PRECEDING)`.)_
- How does the answer shift with soft-deletes via `account_status`? _(Decide if a deleted account counts. If not, filter inside the CTE before aggregating.)_

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/one_year_to_the_next)
- [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). DataDriven is the data engineering interview community. Live code execution in SQL, Python, and Spark sandboxes. Every feature is open to every member.