PRO SQL Modules
SQL Basics1h 20m • 4 lessons
Multiple Tables1h 40m • 5 lessons
Query Restructuring40m • 2 lessons
Data Transformation1h 40m • 5 lessons
Analytical SQL2h • 6 lessons
ORDER BY

Dan Lee
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_id | title | genre | release_year | seasons | rating | total_views_millions |
---|---|---|---|---|---|---|
1 | Stranger Things | Sci-Fi | 2016 | 4 | 8.7 | 140 |
2 | Squid Game | Thriller | 2021 | 1 | 8.0 | 200 |
3 | The Witcher | Fantasy | 2019 | 3 | 7.9 | 90 |
4 | Money Heist | Crime | 2017 | 5 | 8.2 | 180 |
5 | Dark | Sci-Fi | 2017 | 3 | 8.8 | 85 |
6 | Bridgerton | Drama | 2020 | 2 | 7.3 | 100 |
7 | Breaking Bad | Crime | 2008 | 5 | 9.5 | 120 |
8 | Narcos | Crime | 2015 | 3 | 8.8 | 75 |
9 | The Crown | Drama | 2016 | 5 | 8.6 | 95 |
10 | Black Mirror | Sci-Fi | 2011 | 6 | 8.8 | 110 |
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:
title | rating |
---|---|
Breaking Bad | 9.5 |
Dark | 8.8 |
Narcos | 8.8 |
Black Mirror | 8.8 |
Stranger Things | 8.7 |
The Crown | 8.6 |
Money Heist | 8.2 |
Squid Game | 8 |
The Witcher | 7.9 |
Bridgerton | 7.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:
title | total_views_millions |
---|---|
Squid Game | 200 |
Money Heist | 180 |
Stranger Things | 140 |
The Witcher | 90 |
… | … |
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:
title | rating | total_views_millions |
---|---|---|
Breaking Bad | 9.5 | 120 |
Black Mirror | 8.8 | 110 |
Dark | 8.8 | 85 |
Narcos | 8.8 | 75 |
Stranger Things | 8.7 | 140 |
The Crown | 8.6 | 95 |
… | … | … |
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).
- Filter by release_year > 2017.
- ORDER BY release_year DESC, then rating DESC.
Expected Output:
title | release_year | rating |
---|---|---|
Squid Game | 2021 | 8.0 |
Bridgerton | 2020 | 7.3 |
The Witcher | 2019 | 7.9 |
Write an SQL query to return the requested data.