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

PRO SQL Modules

SQL Basics
1h 20m • 4 lessons
  1. SQL SELECT
  2. LIMIT
  3. ORDER BY
  4. DISTINCT
Data Filtering
1h 20m • 4 lessons
  1. WHERE
  2. LIKE
  3. IN
  4. BETWEEN
Aggregations
40m • 2 lessons
  1. GROUP BY
  2. HAVING
Multiple Tables
1h 40m • 5 lessons
  1. INNER JOIN
  2. LEFT & RIGHT JOIN
  3. FULL OUTER JOIN
  4. SELF JOIN
  5. INTERSECT & EXCEPT
Query Restructuring
40m • 2 lessons
  1. Subquery
  2. Common Table Expressions (CTE)
Data Transformation
1h 40m • 5 lessons
  1. CASE
  2. String
  3. Date Manipulation
  4. Math Operations
  5. COALESCE
Analytical SQL
2h • 6 lessons
  1. Ranking - ROW_NUMBER(), RANK(), DENSE_RANK()
  2. Running Totals
  3. LAG and LEAD
  4. FIRST_VALUE, LAST_VALUE, Nth_VALUE
  5. PARTITION BY
  6. SQL BETWEEN and PRECEDING
Unlock Premium
FREE

LIMIT

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 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

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

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

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.

Upgrade to Pro

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.

Upgrade to Pro

👉 Found this lesson helpful?