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:

iddrink
1Latte
2null
3null
4Cappuccino
5null

Should become:

iddrink
1Latte
2Latte
3Latte
4Cappuccino
5Cappuccino

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
Coffee Shop Order ProcessingOrder 1LatteMemory: LatteOrder 2nullFill with: LatteOrder 3nullFill with: LatteOrder 4CappuccinoMemory: CappuccinoOrder 5nullFill: CappuccinoWindow Function Magic:LAG(drink IGNORE NULLS) OVER (ORDER BY id)โ€ข IGNORE NULLS: Skip over null values when looking backwardsโ€ข ORDER BY id: Process orders in the correct sequenceKey Benefitsโœ“ Single pass through data (O(n) time complexity)โœ“ Database engine optimized processingโœ“ Minimal memory usage - only tracks last non-null value
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.
Asked in
Google 12 Amazon 8 Microsoft 6 Meta 4
23.5K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen