Count Days with Higher Sales Volume Than the Previous Day

Quick Overview

Write a SQL query that counts how many days had a strictly higher sales volume than the day before, returning a single number. It tests the LAG window function, how comparisons behave with NULL, and returning zero when nothing qualifies.

Count Days with Higher Sales Volume Than the Previous Day

Company: IBM

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Count the days whose `volume` is strictly higher than the previous day's (use `LAG()`). The first day is never counted. Return a single row with one column `GrowthDays`. ```hint Check the edge rows Look at what your comparison returns for the first day and for a day whose volume equals the day before. Neither should be counted. ``` ### Input Table(s) Table: `Sales` | Column | Type | Meaning | | --- | --- | --- | | sale_date | DATE | Day the volume is recorded for. Primary key. | | volume | INT | Sales volume on that day. NOT NULL. | Keys: `sale_date` is the primary key, so there is one row per date. ### Output Contract - Exactly one row with exactly one column, named `GrowthDays` with that capitalization. - `GrowthDays` is the number of rows whose `volume` is strictly greater than the `volume` of the previous row in `sale_date` order. - If no day grows, the row still appears with `GrowthDays = 0`. ### Example `Sales` | sale_date | volume | | --- | --- | | 2024-01-01 | 100 | | 2024-01-02 | 150 | | 2024-01-03 | 120 | | 2024-01-04 | 200 | | 2024-01-05 | 200 | | 2024-01-06 | 250 | Expected result | GrowthDays | | --- | | 3 | Day 2 (150 > 100), day 4 (200 > 120) and day 6 (250 > 200) have higher volume than their previous day. Day 3 decreased and day 5 stayed the same, so `GrowthDays` is 3. ### Constraints and Clarifications - The table has 1 to 10,000 rows, one per `sale_date`. - `volume` is a non-negative integer and is never NULL. - The previous day means the previous row when the table is ordered by `sale_date`, which is what `LAG()` returns. If a date is missing, for example `2024-01-03`, then `2024-01-04` is compared with `2024-01-02`. - The earliest date has no previous day and is never counted. A table with a single row returns 0. - Equal volume on consecutive days is not growth.

Overview: Write a SQL query that counts how many days had a strictly higher sales volume than the day before, returning a single number. It tests the LAG window function, how comparisons behave with NULL, and returning zero when nothing qualifies.

IBM logo
IBM
Nov 24, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Count the days whose volume is strictly higher than the previous day's (use LAG()). The first day is never counted. Return a single row with one column GrowthDays.

Input Table(s)

Table: Sales

ColumnTypeMeaning
sale_dateDATEDay the volume is recorded for. Primary key.
volumeINTSales volume on that day. NOT NULL.

Keys: sale_date is the primary key, so there is one row per date.

Output Contract

  • Exactly one row with exactly one column, named GrowthDays with that capitalization.
  • GrowthDays is the number of rows whose volume is strictly greater than the volume of the previous row in sale_date order.
  • If no day grows, the row still appears with GrowthDays = 0 .

Example

Sales

sale_datevolume
2024-01-01100
2024-01-02150
2024-01-03120
2024-01-04200
2024-01-05200
2024-01-06250

Expected result

GrowthDays
3

Day 2 (150 > 100), day 4 (200 > 120) and day 6 (250 > 200) have higher volume than their previous day. Day 3 decreased and day 5 stayed the same, so GrowthDays is 3.

Constraints and Clarifications

  • The table has 1 to 10,000 rows, one per sale_date .
  • volume is a non-negative integer and is never NULL.
  • The previous day means the previous row when the table is ordered by sale_date , which is what LAG() returns. If a date is missing, for example 2024-01-03 , then 2024-01-04 is compared with 2024-01-02 .
  • The earliest date has no previous day and is never counted. A table with a single row returns 0.
  • Equal volume on consecutive days is not growth.