Find the group with the largest month-over-month decline in sample variance

Quick Overview

Write a SQL query that computes the monthly sample variance of readings for each group and finds the largest drop in variance from one calendar month to the next. It tests grouping by month, window functions with LAG, handling of missing months and undefined variances, and deterministic tie-breaking.

Find the group with the largest month-over-month decline in sample variance

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: medium

Interview Round: Other

Table `readings` records a numeric `value` with a `grp` label (`'A'`, `'B'` or `'C'`) and a `reading_date`. For each group, compute the per-calendar-month sample variance of `value` and find the largest month-over-month variance decline across all groups. Requirements: - Compute each month's variance with `VAR_SAMP`. - Get the previous month's value with a window function ordered by month. - `decline` = previous_variance - current_variance. - Return one row: `grp` and `decline` (rounded to 4 decimals) for the largest `decline`. ```hint Which months to compare Make sure the month you compare against really is the previous calendar month for that group, and decide what happens to a month whose sample variance does not exist. ``` ### Input Table(s) Table `readings`: | Column | Type | Meaning | |---|---|---| | `grp` | VARCHAR(5) | Group label: `'A'`, `'B'` or `'C'` | | `reading_date` | DATE | Date of the reading | | `value` | DECIMAL(10,4) | Measured value | There is no key. Each row is one reading, and two identical rows are two separate readings. ### Output Contract At most one row with these columns, in this order: | Column | Type | Meaning | |---|---|---| | `grp` | VARCHAR | Group with the largest decline | | `decline` | NUMERIC | Previous month's sample variance minus this month's, rounded to 4 decimal places | Rank candidates by `decline` descending, then by `grp` ascending, and return the first. ### Example Input `readings`: | grp | reading_date | value | |---|---|---| | B | 2024-01-12 | 44.9 | | A | 2024-03-12 | 58.5 | | A | 2024-02-12 | 55.1 | | A | 2024-02-12 | 57.4 | | A | 2024-01-12 | 39.3 | | B | 2024-01-12 | 50.6 | | B | 2024-03-12 | 46 | | A | 2024-03-12 | 12.3 | | C | 2024-02-12 | 16.9 | | C | 2024-03-12 | 26.3 | | C | 2024-02-12 | 54.8 | | B | 2024-02-12 | 14.9 | | C | 2024-03-12 | 39.3 | | C | 2024-01-12 | 20.6 | | C | 2024-01-12 | 14.7 | | A | 2024-03-12 | 15.4 | | A | 2024-03-12 | 11.7 | | B | 2024-01-12 | 30.4 | | B | 2024-02-12 | 43 | | A | 2024-03-12 | 22.1 | | A | 2024-01-12 | 28.8 | | C | 2024-02-12 | 24.9 | | C | 2024-02-12 | 48 | | C | 2024-02-12 | 35.5 | | B | 2024-03-12 | 59.7 | | A | 2024-01-12 | 40.7 | | C | 2024-03-12 | 33.9 | | A | 2024-03-12 | 12.2 | | B | 2024-01-12 | 32.3 | | B | 2024-01-12 | 12.6 | | C | 2024-02-12 | 31.1 | Output: | grp | decline | |---|---| | B | 300.9600 | Group B's sample variance is 394.805 in February 2024 (readings 14.9 and 43) and 93.845 in March 2024 (readings 46 and 59.7), a decline of 300.96. The next largest decline is group C from February to March, about 158.5227. ### Constraints and Clarifications - A month is a calendar month of `reading_date`, year included, so January 2024 and January 2025 are different months. - A decline compares a month with the immediately preceding calendar month of the same group. If that preceding month has no variance for the group, the month has no decline. - A month with a single reading has no sample variance (`VAR_SAMP` returns NULL), so it neither has a decline nor serves as the previous month for one. - `value` is never NULL. Identical rows are separate readings and all count. - The largest decline can be negative if no group's variance fell; return it anyway. If no group has two consecutive months with a variance, return no rows. - If several groups share the largest decline after rounding to 4 decimals, return the one that comes first alphabetically.

Overview: Write a SQL query that computes the monthly sample variance of readings for each group and finds the largest drop in variance from one calendar month to the next. It tests grouping by month, window functions with LAG, handling of missing months and undefined variances, and deterministic tie-breaking.

General logo
General
Nov 24, 2026
mediumData ScientistOtherData Manipulation (SQL/Python)
0
0

Table readings records a numeric value with a grp label ('A', 'B' or 'C') and a reading_date. For each group, compute the per-calendar-month sample variance of value and find the largest month-over-month variance decline across all groups.

Requirements:

  • Compute each month's variance with VAR_SAMP .
  • Get the previous month's value with a window function ordered by month.
  • decline = previous_variance - current_variance.
  • Return one row: grp and decline (rounded to 4 decimals) for the largest decline .

Input Table(s)

Table readings:

ColumnTypeMeaning
grpVARCHAR(5)Group label: 'A', 'B' or 'C'
reading_dateDATEDate of the reading
valueDECIMAL(10,4)Measured value

There is no key. Each row is one reading, and two identical rows are two separate readings.

Output Contract

At most one row with these columns, in this order:

ColumnTypeMeaning
grpVARCHARGroup with the largest decline
declineNUMERICPrevious month's sample variance minus this month's, rounded to 4 decimal places

Rank candidates by decline descending, then by grp ascending, and return the first.

Example

Input readings:

grpreading_datevalue
B2024-01-1244.9
A2024-03-1258.5
A2024-02-1255.1
A2024-02-1257.4
A2024-01-1239.3
B2024-01-1250.6
B2024-03-1246
A2024-03-1212.3
C2024-02-1216.9
C2024-03-1226.3
C2024-02-1254.8
B2024-02-1214.9
C2024-03-1239.3
C2024-01-1220.6
C2024-01-1214.7
A2024-03-1215.4
A2024-03-1211.7
B2024-01-1230.4
B2024-02-1243
A2024-03-1222.1
A2024-01-1228.8
C2024-02-1224.9
C2024-02-1248
C2024-02-1235.5
B2024-03-1259.7
A2024-01-1240.7
C2024-03-1233.9
A2024-03-1212.2
B2024-01-1232.3
B2024-01-1212.6
C2024-02-1231.1

Output:

grpdecline
B300.9600

Group B's sample variance is 394.805 in February 2024 (readings 14.9 and 43) and 93.845 in March 2024 (readings 46 and 59.7), a decline of 300.96. The next largest decline is group C from February to March, about 158.5227.

Constraints and Clarifications

  • A month is a calendar month of reading_date , year included, so January 2024 and January 2025 are different months.
  • A decline compares a month with the immediately preceding calendar month of the same group. If that preceding month has no variance for the group, the month has no decline.
  • A month with a single reading has no sample variance ( VAR_SAMP returns NULL), so it neither has a decline nor serves as the previous month for one.
  • value is never NULL. Identical rows are separate readings and all count.
  • The largest decline can be negative if no group's variance fell; return it anyway. If no group has two consecutive months with a variance, return no rows.
  • If several groups share the largest decline after rounding to 4 decimals, return the one that comes first alphabetically.