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

ORDER BY

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 sort query results using ORDER BY
  • Understand how to order results in ascending (ASC) and descending (DESC) order
  • Write SQL queries to rank Netflix’s hit series by rating and viewership

🎬 Scenario: Ranking Netflix’s Most Popular Series

Your manager at Netflix, Alex, wants a ranked list of the most popular series. Alex asks:

I want to see our hit series ranked by total viewership. Can you sort the results from most to least watched?

Your task is to use the ORDER BY clause to produce a properly sorted list of Netflix’s top shows.

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. Sorting Data with ORDER BY

The ORDER BY clause sorts query results in a specific order, either ascending (ASC) or descending (DESC).

Syntax:

1SELECT column1, column2
2FROM table_name
3ORDER BY column_name ASC | DESC;
  • ASC sorts results in ascending order (smallest to largest).
  • DESC sorts results in descending order (largest to smallest).
  • By default, ORDER BY sorts in ascending order if ASC or DESC is not specified.

2. Sorting by Ratings in Descending Order

To list the highest-rated Netflix series first, use ORDER BY rating DESC.

Example Query: Ranking Shows by Rating

1SELECT title, rating
2FROM netflix_series
3ORDER BY rating DESC;

Output Example:

titlerating
Breaking Bad9.5
Dark8.8
Narcos8.8
Black Mirror8.8
Stranger Things8.7
The Crown8.6
Money Heist8.2
Squid Game8
The Witcher7.9
Bridgerton7.3

What’s Happening?

  • The show with the highest rating appears first.
  • The show with the lowest rating appears last.

3. Sorting by Viewership in Descending Order

To rank the most-watched Netflix shows, sort by total_views_millions in descending order.

Example Query: Ranking Shows by Viewership

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

Output Example:

titletotal_views_millions
Squid Game200
Money Heist180
Stranger Things140
The Witcher90
……

What’s Happening?

  • The show with the highest number of views appears first.
  • The show with the lowest number of views appears last.

4. Sorting by Multiple Columns

You can sort by multiple columns to break ties. For example, if two series share the same rating, you can sort them by total viewership as a secondary criterion.

Example Query: Sorting by Rating, Then Viewership

1SELECT title, rating, total_views_millions
2FROM netflix_series
3ORDER BY rating DESC, total_views_millions DESC;

Output Example:

titleratingtotal_views_millions
Breaking Bad9.5120
Black Mirror8.8110
Dark8.885
Narcos8.875
Stranger Things8.7140
The Crown8.695
………

What’s Happening?

  • Shows are first sorted by rating in descending order.
  • If two shows share the same rating, they are then sorted by total_views_millions in descending order.

✍️ SQL Exercises

Exercise 1: Sorting Newer Releases First

Alex wants a report showing shows released after 2017, sorted by release year (newest first). If two shows have the same release year, they should be sorted by rating (highest first).

  1. Filter by release_year > 2017.
  2. ORDER BY release_year DESC, then rating DESC.

Expected Output:

titlerelease_yearrating
Squid Game20218.0
Bridgerton20207.3
The Witcher20197.9

Write an SQL query to return the requested data.

Upgrade to Pro
👉 Found this lesson helpful?