ML Engineer MasterClass (April) | 6 seats left

ORDER BY

ORDER BY

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

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:

SQL
SELECT column1, column2
FROM table_name
ORDER 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

SQL
SELECT title, rating
FROM netflix_series
ORDER 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

SQL
SELECT title, total_views_millions
FROM netflix_series
ORDER 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

SQL
SELECT title, rating, total_views_millions
FROM netflix_series
ORDER 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.