Back to Chapters
Chapter Progress: 0 %0 / 6 Lessons Read
‹›

4. Data engineering patterns

4.4 Running totals and LAG: compare a row with the one before

Highlights & notes

Select lesson text to highlight it, or tap Add note.

    Bookmarks

    Bookmark a lesson to see it here.

      The aggregates you know (SUM, AVG, COUNT) also work as window functions. Add OVER (ORDER BY ...) and SUM becomes a running total: each row shows the total so far.

      • SUM(amount) OVER (ORDER BY order_date) → total up to this row
      • LAG(amount) OVER (ORDER BY order_date) → the amount from the previous row
      • LEAD(amount) OVER (...) → the amount from the next row

      LAG answers "what changed since last time?": growth from yesterday, days since the last order, a jump in a sensor reading. The first row has no previous row, so LAG gives NULL there.

      pandas vs SQL

      • df["amount"].cumsum() → SUM(amount) OVER (ORDER BY order_date)
      • df["amount"].shift(1) → LAG(amount) OVER (ORDER BY order_date)
      Must Know:

      The ORDER BY inside OVER (...) decides which row counts as "previous". Leave it out and the running total or LAG has no meaning. Add PARTITION BY to restart it per customer or per day.

      How it fits together

      1. Rows in date order
      2. Look back at earlier rows
      3. Total so far / previous value

      Watch the query run

      Read the last column from top to bottom: 55000, then 55000 + 700, and so on.

      SQL

      SELECT order_date, amount,       SUM(amount) OVER (         ORDER BY order_date       ) AS running_totalFROM ordersWHERE status = 'delivered'ORDER BY order_date;

      Rows at this step

      order_date | amount | running_total
      -----------+--------+--------------
      2026-01-05 | 55000  | 55000
      2026-01-06 | 700    | 55700
      2026-01-09 | 1500   | 57200
      2026-01-15 | 52000  | 109200
      2026-01-20 | 11500  | 120700

      Practice in SQL

      For customer 1's orders, show order_date, amount and the amount of the previous order as prev_amount. Sort by order_date.

      Tables you can query: customers, orders
      customers
      id | name  | city
      ---+-------+------
      1  | Asha  | Noida
      2  | Ravi  | Delhi
      3  | Meera | Pune
      4  | Kabir | Noida
      5  | Zoya  | NULL
      
      orders
      id  | customer_id | product  | amount | status    | order_date
      ----+-------------+----------+--------+-----------+-----------
      101 | 1           | Laptop   | 55000  | delivered | 2026-01-05
      102 | 2           | Mouse    | 700    | delivered | 2026-01-06
      103 | 1           | Keyboard | 1500   | delivered | 2026-01-09
      104 | 3           | Monitor  | 12000  | cancelled | 2026-01-12
      105 | 2           | Laptop   | 52000  | delivered | 2026-01-15
      106 | 4           | Mouse    | 650    | pending   | 2026-01-18
      107 | 1           | Monitor  | 11500  | delivered | 2026-01-20
      108 | 3           | Keyboard | NULL   | pending   | 2026-01-22
      Expected output
      order_date | amount | prev_amount
      -----------+--------+------------
      2026-01-05 | 55000  | NULL
      2026-01-09 | 1500   | 55000
      2026-01-20 | 11500  | 1500

      Reference solution
      SELECT order_date, amount,
             LAG(amount) OVER (
               ORDER BY order_date
             ) AS prev_amount
      FROM orders
      WHERE customer_id = 1
      ORDER BY order_date;

      Question 1 · Easy

      What does LAG(amount) return?

      Question 2 · Medium

      What does LAG return on the first row of a window?

      Question 3 · Hard

      Amounts in date order are 100, 50, 200. What is the running total on the third row?

      Key takeaways

      SUM with OVER (ORDER BY ...) gives a running total. LAG reads the previous row and LEAD the next. The ORDER BY inside OVER sets the row order.

      Lesson 22 of 24

      Save your progress

      Start free and your progress, code and notes stay saved. Free for 1 month, no card needed.

      Start free Log in
      PreviousRunning totals and LAG: compare a row with the one before
      Save your progress free for 1 month. Start free · Log in