ML Engineer MasterClass (April) | 6 seats left

LIMIT

LIMIT

Lesson Objectives

By the end of this lesson, you will:

  • Learn how to restrict the number of rows returned using LIMIT and OFFSET
  • Write SQL queries to retrieve the top Netflix series based on rating and viewership

🎬 Scenario: Finding the Top Netflix Series

Your manager at Netflix, Alex, wants a quick snapshot of the top 3 most-watched series. Instead of returning the entire dataset, they only need a short list.

Alex asks:

Can you get me the top 3 most-viewed Netflix series? We don’t need the full list, just the biggest hits.

Your task is to use LIMIT or OFFSET to retrieve only the highest-performing series from the netflix_series table.

series_idtitlegenrerelease_yearseasonsratingtotal_views_millions
1Stranger ThingsSci-Fi201648.7140
2Squid GameThriller202118.0200
3The WitcherFantasy201937.990
4Money HeistCrime201758.2180
5DarkSci-Fi201738.885
6BridgertonDrama202027.3100
7Breaking BadCrime200859.5120
8NarcosCrime201538.875
9The CrownDrama201658.695
10Black MirrorSci-Fi201168.8110

1. Limiting Results with LIMIT

The LIMIT keyword restricts the number of rows returned by a query. It is supported in MySQL and PostgreSQL.

Syntax

SQL
1SELECT column1, column2 
2FROM table_name 
3ORDER BY column_name DESC 
4LIMIT n;
  • LIMIT n returns only the first n rows of the result set.
  • It is usually combined with ORDER BY to return the top or bottom results.

2. Retrieving the Top 3 Most-Watched Series

To return the top 3 Netflix series by viewership, we use LIMIT 3.

Example Query

SQL
1SELECT title, total_views_millions 
2FROM netflix_series 
3ORDER BY total_views_millions DESC 
4LIMIT 3;

Output Example

titletotal_views_millions
Squid Game200
Money Heist180
Stranger Things140

What’s Happening?

  • The query sorts series by total views in descending order.
  • Only the first 3 results are displayed.

3. Using LIMIT with OFFSET

The OFFSET clause allows pagination, meaning you can skip a set number of rows before returning results.

Example Query: Finding the 4th to 6th Most-Watched Series

SQL
1SELECT title, total_views_millions 
2FROM netflix_series 
3ORDER BY total_views_millions DESC 
4LIMIT 3 OFFSET 3;

Output Example

titletotal_views_millions
Breaking Bad120
Black Mirror110
Bridgerton100

What’s Happening?

  • OFFSET 3 skips the first 3 rows.
  • LIMIT 3 returns the next 3 rows, effectively showing the 4th to 6th most-watched series.

✍️ SQL Exercises

Exercise 1: Top 5 Highest-Rated Netflix Series

Alex, your manager at Netflix, wants to know which shows have received the highest audience ratings. Write an SQL query to retrieve the top 5 highest-rated Netflix series, sorted by rating in descending order.

Expected Output Example:

titlegenrerating
Breaking BadCrime9.5
DarkSci-Fi8.8
Black MirrorSci-Fi8.8
NarcosCrime8.8
Stranger ThingsSci-Fi8.7

Write an SQL query to return the requested data.


Exercise 2: Recent Netflix Users

Netflix wants to see the most recent signups to track new users. Write an SQL query to retrieve the 5 most recent users who signed up, sorted by signup_date in descending order.

Expected Output Example:

namesignup_date
David Kim2023-01-05T00:00:00
Emma Garcia2022-07-18T00:00:00
Bob Smith2021-02-22T00:00:00
Alice Johnson2020-05-10T00:00:00
Charlie Lee2019-09-13T00:00:00

Write an SQL query to return the requested data.