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

FREE

HAVING

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 filter aggregated data using HAVING
  • Understand the difference between WHERE and HAVING
  • Write SQL queries to analyze Netflix user engagement using aggregated filters

🎬 Scenario: Finding Active Netflix Users

Your manager at Netflix, Alex, wants to identify power users—those who have watched at least 3 different series.

Can you find users who have watched at least 3 series?

Your task is to query the watch_history table and filter results based on aggregated values.

history_iduser_idseries_idwatch_datewatch_duration_minutescompleted
112“2024-02-10T18:30:00”60false
224“2024-01-20T21:00:00”50true
331“2024-02-01T19:45:00”75true
443“2024-02-15T17:10:00”30false
5510“2024-02-18T23:00:00”45true
615“2024-02-12T14:30:00”50true
727“2024-02-15T19:00:00”40false
839“2024-02-20T20:15:00”60true
946“2024-02-22T21:45:00”30false
1058“2024-02-25T18:00:00”40true
1114“2024-03-01T20:00:00”25false
1222“2024-03-03T22:30:00”90true

1. Understanding the Difference Between WHERE and HAVING

Both WHERE and HAVING allow us to filter data, but they work differently:

ClauseUsed ForWorks With
WHEREFilters individual rows before aggregationRegular column values
HAVINGFilters aggregated results after GROUP BYAggregate functions

Example Comparison

Filtering Before Aggregation (WHERE)
Find all watch sessions that lasted more than 30 minutes:

1SELECT user_id, series_id, watch_duration_minutes  
2FROM watch_history  
3WHERE watch_duration_minutes > 30;
user_idseries_idwatch_duration_minutes
1260
2450
3175
51045
1550
2740
3960
5840
2290

Filtering After Aggregation (HAVING)
Find users who have watched at least 3 different series:

1SELECT user_id, COUNT(series_id) AS total_shows_watched  
2FROM watch_history  
3GROUP BY user_id  
4HAVING COUNT(series_id) >= 3;
user_idtotal_shows_watched
13
23

2. Using HAVING to Filter Aggregated Results

Since HAVING works after aggregation, we can use it to filter users based on how many shows they watched.

Example Query: Finding Users Who Watched 3+ Series

1SELECT user_id, COUNT(series_id) AS total_shows_watched  
2FROM watch_history  
3GROUP BY user_id  
4HAVING COUNT(series_id) >= 3;
user_idtotal_shows_watched
13
23

What’s Happening?

  • COUNT(series_id) calculates how many different shows each user has watched.
  • HAVING COUNT(series_id) >= 3 filters the result after aggregation, keeping only users who watched at least 3 series.

3. Combining WHERE and HAVING

You can use WHERE and HAVING together in a query.

For example, if we want to:

  • Exclude very short watch sessions (WHERE watch_duration_minutes > 30)
  • Find users who watched at least 2 series (HAVING COUNT(series_id) >= 2)

We can write:

1SELECT user_id, COUNT(series_id) AS total_shows_watched  
2FROM watch_history  
3WHERE watch_duration_minutes > 30  
4GROUP BY user_id  
5HAVING COUNT(series_id) >= 2;
user_idtotal_shows_watched
12
23
32
52

What’s Happening?

  • WHERE watch_duration_minutes > 30 filters out watch sessions before aggregation.
  • GROUP BY user_id groups data by user.
  • HAVING COUNT(series_id) >= 2 filters users based on the aggregated count.

✍️ SQL Exercises

Exercise 1: Finding Power Users

Alex wants a report of users who have watched at least 3 different series.

Filter condition:

  • Users must have watched at least 3 series.
  • Sort by total_shows_watched in descending order.
user_idtotal_shows_watched
13
23

Write an SQL query to return the requested data.

Upgrade to Pro

Exercise 2: Filtering Users Who Watched 2+ Series with Longer Sessions

Alex also wants to identify users who watched at least 2 series but only counting watch sessions longer than 30 minutes.

Filter condition:

  • Exclude watch sessions that lasted 30 minutes or less.
  • Count how many series each user watched.
  • Only include users who watched at least 2 series.
user_idtotal_shows_watched
23
12
32
52

Write an SQL query to return the requested data.

Upgrade to Pro

👉 Found this lesson helpful?