Last Chance to Join Data Science Interview MasterClass (this week) 🚀 | Just 2 spots remaining...

FREE

Running Totals

Course Author Daniel Lee
Instructor

Dan Lee

Learn SQL for free with interactive exercises designed to help aspiring Data Engineers, Data Analysts, and Data Scientists. Learn the basics and advanced SQL concepts by solving real-world problems from MANGO (Meta, Amazon, Netflix, Google, OpenAI)

Lesson Objectives

By the end of this lesson, you will:

  • Learn how to use SUM() OVER() to calculate running totals
  • Use AVG() OVER() for moving averages
  • Apply COUNT() OVER() to track cumulative counts
  • Write SQL queries to analyze user engagement trends on Meta posts

🎬 Scenario: Tracking Cumulative Engagement on Meta Posts

Your manager at Meta, Jordan, is interested in analyzing post engagement trends over time.

Can you provide cumulative engagement statistics, like running totals of likes and the average engagement per post?

Your task is to use window functions (SUM() OVER(), AVG() OVER(), COUNT() OVER()) to generate engagement trends. Use the meta_posts table to solve the question.

post_iduser_idpost_contentpost_datelikessharespost_status
11“Had a great day at the beach!”“2024-01-10T14:30:00”12045“Public”
22“Launching our new product!”“2024-01-12T09:20:00”25090“Friends Only”
33“Just finished a 10K run!”“2024-01-15T18:10:00”8030“Public”
44“Hosting a webinar next week.”“2024-01-18T20:45:00”5025“Private”
55“Traveling to Japan soon!”“2024-01-20T10:05:00”18075“Public”
61“Excited for the weekend!”“2024-01-25T16:15:00”9540“Friends Only”
72“Need recommendations for a new laptop.”“2024-01-27T12:40:00”7020“Public”
83“Learning SQL window functions!”“2024-01-29T08:55:00”11050“Public”
94“Working on a new AI model.”“2024-02-01T14:00:00”200100“Friends Only”
105“Book recommendations?”“2024-02-05T11:30:00”6515“Public”

1. Understanding Window Functions for Aggregations

Unlike GROUP BY, window functions perform aggregations while preserving individual rows in the result.

FunctionWhat It DoesUse Case
SUM() OVER()Computes a running total over a partitionCumulative likes per post
AVG() OVER()Calculates a moving averageAverage engagement over time
COUNT() OVER()Counts the number of rows in a partitionRunning count of posts

Syntax for Window Aggregations

1SELECT column1, column2,  
2       function() OVER (PARTITION BY column ORDER BY column) AS result  
3FROM table_name;
  • PARTITION BY groups data into subsets (optional).
  • ORDER BY defines the ranking within the window.

2. Using SUM() OVER() to Calculate Running Totals

Jordan wants a cumulative sum of likes to track engagement growth.

Example Query: Running Total of Likes

1SELECT post_id, post_date, likes,  
2       SUM(likes) OVER (ORDER BY post_date) AS running_total_likes  
3FROM meta_posts;
post_idpost_datelikesrunning_total_likes
1“2024-01-10T14:30:00”120120
2“2024-01-12T09:20:00”250370
3“2024-01-15T18:10:00”80450
4“2024-01-18T20:45:00”50500
5“2024-01-20T10:05:00”180680
6“2024-01-25T16:15:00”95775
7“2024-01-27T12:40:00”70845
8“2024-01-29T08:55:00”110955
9“2024-02-01T14:00:00”2001155
10“2024-02-05T11:30:00”651220

What’s Happening?

  • SUM(likes) OVER (ORDER BY post_date) adds up the likes progressively, creating a running total.
  • Helps track cumulative engagement growth over time.

3. Using AVG() OVER() to Calculate Moving Averages

Jordan wants to find the average number of likes per post over time.

Example Query: Rolling Average of Likes

1SELECT post_id, post_date, likes,  
2       AVG(likes) OVER (ORDER BY post_date) AS avg_likes_over_time  
3FROM meta_posts;
post_idpost_datelikesavg_likes_over_time
1“2024-01-10T14:30:00”120120
2“2024-01-12T09:20:00”250185
3“2024-01-15T18:10:00”80150
4“2024-01-18T20:45:00”50125
5“2024-01-20T10:05:00”180136
6“2024-01-25T16:15:00”95129.16666666666666
7“2024-01-27T12:40:00”70120.71428571428571
8“2024-01-29T08:55:00”110119.375
9“2024-02-01T14:00:00”200128.33333333333334
10“2024-02-05T11:30:00”65122

What’s Happening?

  • AVG(likes) OVER (ORDER BY post_date) calculates a moving average of likes per post.
  • Helps identify trends in engagement over time.

4. Using COUNT() OVER() to Track Post Activity

Jordan wants to track how many posts have been published over time.

Example Query: Running Count of Posts

1SELECT post_id, post_date,  
2       COUNT(*) OVER (ORDER BY post_date) AS running_post_count  
3FROM meta_posts;
post_idpost_daterunning_post_count
1“2024-01-10T14:30:00”1
2“2024-01-12T09:20:00”2
3“2024-01-15T18:10:00”3
4“2024-01-18T20:45:00”4
5“2024-01-20T10:05:00”5
6“2024-01-25T16:15:00”6
7“2024-01-27T12:40:00”7
8“2024-01-29T08:55:00”8
9“2024-02-01T14:00:00”9
10“2024-02-05T11:30:00”10

What’s Happening?

  • COUNT(*) OVER (ORDER BY post_date) generates a running count of posts over time.
  • Helps track content creation trends.

✍️ SQL Exercises

Exercise 1: Running Total of Likes

Jordan wants to calculate a running total of likes to track engagement growth.

Filter condition:

  • Retrieve post ID, post date, likes, and running total likes.
  • Sort by post_date in ascending order.
post_idpost_datelikesrunning_total_likes
1“2024-01-10T14:30:00”120120
2“2024-01-12T09:20:00”250370
3“2024-01-15T18:10:00”80450
4“2024-01-18T20:45:00”50500
5“2024-01-20T10:05:00”180680
6“2024-01-25T16:15:00”95775
7“2024-01-27T12:40:00”70845
8“2024-01-29T08:55:00”110955
9“2024-02-01T14:00:00”2001155
10“2024-02-05T11:30:00”651220

Write an SQL query to return the requested data.

Upgrade to Pro

Exercise 2: Tracking Running Post Count

Jordan also wants to track how many posts have been published over time.

Filter condition:

  • Retrieve post ID, post date, and running post count.
  • Sort by post_date in ascending order.
post_idpost_daterunning_post_count
1“2024-01-10T14:30:00”1
2“2024-01-12T09:20:00”2
3“2024-01-15T18:10:00”3
4“2024-01-18T20:45:00”4
5“2024-01-20T10:05:00”5
6“2024-01-25T16:15:00”6
7“2024-01-27T12:40:00”7
8“2024-01-29T08:55:00”8
9“2024-02-01T14:00:00”9
10“2024-02-05T11:30:00”10

Write an SQL query to return the requested data.

Upgrade to Pro
👉 Found this lesson helpful?