Change Null Values in a Table to the Previous Value - Problem
Imagine you're working at a coffee shop where orders are recorded in a table, but sometimes the cashier forgets to enter the drink name, leaving it as null. Your task is to fill in the missing drink names by using the most recent non-null drink ordered.
You have a CoffeeShop table with two columns:
id- A unique order ID (primary key)drink- The name of the drink ordered (may contain null values)
Goal: Replace each null value with the drink name from the most recent previous row that is not null. The first row is guaranteed to have a non-null drink value.
Example:
| id | drink |
|---|---|
| 1 | Latte |
| 2 | null |
| 3 | null |
| 4 | Cappuccino |
| 5 | null |
Should become:
| id | drink |
|---|---|
| 1 | Latte |
| 2 | Latte |
| 3 | Latte |
| 4 | Cappuccino |
| 5 | Cappuccino |
Input & Output
example_1.sql โ Basic Case
$
Input:
CoffeeShop table:
| id | drink |
|----|-------------|
| 1 | Latte |
| 2 | null |
| 3 | null |
| 4 | Cappuccino |
| 5 | null |
โบ
Output:
| id | drink |
|----|-------------|
| 1 | Latte |
| 2 | Latte |
| 3 | Latte |
| 4 | Cappuccino |
| 5 | Cappuccino |
๐ก Note:
Row 2 and 3 get filled with 'Latte' from row 1. Row 5 gets filled with 'Cappuccino' from row 4. Each null value takes the most recent non-null drink.
example_2.sql โ Consecutive Non-Nulls
$
Input:
CoffeeShop table:
| id | drink |
|----|------------|
| 1 | Espresso |
| 2 | Americano |
| 3 | null |
| 4 | null |
| 5 | Mocha |
โบ
Output:
| id | drink |
|----|------------|
| 1 | Espresso |
| 2 | Americano |
| 3 | Americano |
| 4 | Americano |
| 5 | Mocha |
๐ก Note:
Rows 3 and 4 are filled with 'Americano' since it's the most recent non-null value before them. Row 5 already has a value so no change needed.
example_3.sql โ Single Null
$
Input:
CoffeeShop table:
| id | drink |
|----|-------------|
| 1 | Frappuccino |
| 2 | null |
| 3 | Tea |
โบ
Output:
| id | drink |
|----|-------------|
| 1 | Frappuccino |
| 2 | Frappuccino |
| 3 | Tea |
๐ก Note:
Only row 2 has a null value, which gets replaced with 'Frappuccino' from row 1. Row 3 already has 'Tea' so it remains unchanged.
Constraints
- 1 โค number of rows โค 105
- 1 โค id โค 109
- The first row is guaranteed to have a non-null drink value
- drink values are strings with maximum length of 50 characters
- id values are unique and serve as the primary key
Visualization
Tap to expand
Understanding the Visualization
1
Process Orders Sequentially
Read each order in sequence by ID, maintaining the natural order of the coffee shop queue
2
Track Last Complete Order
Keep a mental note of the last drink that was properly recorded (non-null)
3
Fill Missing Information
When encountering an incomplete order (null drink), use the last remembered drink name
4
Update Memory
Whenever a complete order is found, update the 'last drink' memory for future incomplete orders
Key Takeaway
๐ฏ Key Insight: The window function `LAG(drink IGNORE NULLS)` efficiently implements the "remember last complete order" logic in a single SQL operation, eliminating the need for complex nested queries or iterative processing.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code