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

FREE

IN

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 data using IN for multiple values
  • Understand when to use IN instead of multiple OR conditions
  • Write SQL queries to find Netflix series based on specific genres and ratings

🎬 Scenario: Identifying Popular Crime and Sci-Fi Series

Your manager at Netflix, Alex, wants to analyze the most popular Crime and Sci-Fi series to identify trends among viewers.

Alex asks:

Can you retrieve all Netflix series that belong to either the ‘Crime’ or ‘Sci-Fi’ genres?

Your task is to query the netflix_series table and provide the requested data.

series_idtitlegenrerelease_yearseasonsratingtotal_views_millions
1Stranger ThingsSci-Fi201648.7140
2Squid GameThriller202118200
3The WitcherFantasy201937.990
4Money HeistCrime201758.2180
5DarkSci-Fi201738.885

1. Filtering Data with IN

The IN operator allows you to filter data based on multiple values. Instead of using multiple OR conditions, IN makes queries cleaner and more efficient.

Syntax:

1SELECT column1, column2  
2FROM table_name  
3WHERE column_name IN (value1, value2, ...);
  • The query will return rows where column_name matches any value listed inside IN().
  • This is equivalent to using multiple OR conditions.

2. Using IN for Genre Filtering

Instead of writing:

1SELECT title, genre  
2FROM netflix_series  
3WHERE genre = 'Crime' OR genre = 'Sci-Fi';

You can write:

1SELECT title, genre  
2FROM netflix_series  
3WHERE genre IN ('Crime', 'Sci-Fi');

Output Example:

titlegenre
Stranger ThingsSci-Fi
DarkSci-Fi
Black MirrorSci-Fi
Money HeistCrime
NarcosCrime
Breaking BadCrime

What’s Happening?

  • IN ('Crime', 'Sci-Fi') filters only shows within these genres.
  • The query is shorter and easier to read than using multiple OR conditions.

Using IN with Numeric Values

The IN clause also works for numeric comparisons.

Example Query: Finding Series Released in 2015, 2017, or 2020

1SELECT title, release_year  
2FROM netflix_series  
3WHERE release_year IN (2015, 2017, 2020);

Output Example:

titlerelease_year
Narcos2015
Money Heist2017
Dark2017
Bridgerton2020

What’s Happening?

  • The query filters only shows released in the specified years (2015, 2017, 2020).
  • Without IN, we would have to write:sqlCopyEditWHERE release_year = 2015 OR release_year = 2017 OR release_year = 2020;

SQL Exercises

Exercise 1: Filtering Netflix Shows by Genre

Alex wants to analyze popular genres. Retrieve all Netflix series that belong to either the “Crime” or “Sci-Fi” genres, sorted alphabetically by title.

Filter condition:

  • genre must be ‘Crime’ or ‘Sci-Fi’.
  • Sort results by title in ascending order.

Expected Output:

titlegenre
Black MirrorSci-Fi
Breaking BadCrime
DarkSci-Fi
Money HeistCrime
NarcosCrime
Stranger ThingsSci-Fi

Write an SQL query to return the requested data.

Upgrade to Pro

Exercise 2: Finding Shows Released in Specific Years

Alex wants to generate a report showing all series released in 2016, 2017, or 2021, sorted by release_year in ascending order, then title in ascending order.

Filter condition:

  • release_year must be 2016, 2017, or 2021.
  • Sort by release_year in ascending order, then title in ascending order.

Expected Output:

titlerelease_year
“Stranger Things”2016
“The Crown”2016
“Dark”2017
“Money Heist”2017
“Squid Game”2021

Write an SQL query to return the requested data.

Upgrade to Pro

👉 Found this lesson helpful?