Rank In SQL W3Schools Example Tutorial Guide

When it comes to working with structured data in the context of SQL and MySQL, it's essential to understand how to rank items. You might need to list items based on sales figures, popularity, or any other rating system. In this article, we'll take a look at how to accomplish this using the SQL RANK() function, along with practical examples.

SQL DENSE_RANK() Explained with Examples | Beginner SQL Window Function Tutorial
SQL DENSE_RANK() Explained with Examples | Beginner SQL Window Function Tutorial

The RANK() function assigns a unique rank to each row within a partition of a result set. Rows with the same values receive the same rank. In the following examples, we'll use the Northwind database to demonstrate practical use cases.

the three functions for ranking data in an excel spreadsheet with numbers and symbols
the three functions for ranking data in an excel spreadsheet with numbers and symbols

Ranking Products by Quantity Sold

In many scenarios, you might want to rank products based on their sales. Let's start by retrieving the top 10 products by quantity sold:

SQL RANK() Explained with Examples | Beginner SQL Window Function Tutorial
SQL RANK() Explained with Examples | Beginner SQL Window Function Tutorial

The following query will give you the desired result, ranking products based on their total quantity sold and displaying the top 10:

Using RANK() Function

SQL Window Functions Explained with Examples | Beginner SQL Tutorial
SQL Window Functions Explained with Examples | Beginner SQL Tutorial

The RANK() function picks up where the previous row's rank left off. If two rows have the same value, they will both receive the same rank, and the next rank will be skipped. Here's how to use it:

SELECT ProductName, SUM(Quantity) AS TotalQuantity, RANK() OVER (ORDER BY SUM(Quantity) DESC) AS RankPosition FROM Orders INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID GROUP BY ProductName ORDER BY TotalQuantity DESC, ProductName ASC;

Alternative: Using DENSE_RANK() Function

Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis
Sql Cheat Sheet, Learn Sql, Learn Web Development, Excel Tutorials, Sql Server, Career Options, Data Analyst, Data Scientist, Data Analysis

If you want to avoid skipping ranks when two products have the same quantity, use the DENSE_RANK() function instead. Consecutive ranks are assigned even if the values are equal:

SELECT ProductName, SUM(Quantity) AS TotalQuantity, DENSE_RANK() OVER (ORDER BY SUM(Quantity) DESC) AS RankPosition FROM Orders INNER JOIN OrderDetails ON Orders.OrderID = OrderDetails.OrderID INNER JOIN Products ON OrderDetails.ProductID = Products.ProductID GROUP BY ProductName ORDER BY TotalQuantity DESC, ProductName ASC;

Ranking Categories by Total Sales Value

SQL RIGHT() Explained with Examples | Extract Characters from the Right in SQL Server
SQL RIGHT() Explained with Examples | Extract Characters from the Right in SQL Server

Now, let's rank categories based on the total sales value of their products. We'll also consider only the top 5 categories:

Including Category Information

7 SQL Projects for Beginners | Build a Real Data Portfolio
7 SQL Projects for Beginners | Build a Real Data Portfolio
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL CASE WHEN Explained with Examples | Beginner SQL Tutorial
SQL Quick Notes Cheat Sheet | SQL Queries, Joins, Functions & Database Commands for Beginners
SQL Quick Notes Cheat Sheet | SQL Queries, Joins, Functions & Database Commands for Beginners
SQL ORDER BY clause Explained for Beginners
SQL ORDER BY clause Explained for Beginners
SQL IN NUTSHELL
SQL IN NUTSHELL
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Joins Explained with Examples | Complete JOIN Cheat Sheet for Beginners
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
SQL Basics Cheat Sheet for Beginners | Learn SQL Fast 💻
a computer diagram with many different types of commands on the top one and bottom two
a computer diagram with many different types of commands on the top one and bottom two
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
Learn SQL Fast 🚀 | Beginner SQL Commands Cheat Sheet for Data Analysts
What is SQL? Complete Beginner Guide with Examples
What is SQL? Complete Beginner Guide with Examples
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL ORDER BY Explained with Examples | Sort Data in SQL
SQL in 60 days
SQL in 60 days
SQL Basics Made Easy 💻 | SELECT, WHERE & ORDER BY with Examples
SQL Basics Made Easy 💻 | SELECT, WHERE & ORDER BY with Examples
SQL GROUP BY Explained Visually (Beginner-Friendly Guide)
SQL GROUP BY Explained Visually (Beginner-Friendly Guide)
SQL Commands Cheat Sheet: DDL, DML, DCL, TCL & Joins Explained
SQL Commands Cheat Sheet: DDL, DML, DCL, TCL & Joins Explained
SQL Aggregate Functions Made Easy 📊 | SUM, COUNT, AVG, MAX, MIN with Examples
SQL Aggregate Functions Made Easy 📊 | SUM, COUNT, AVG, MAX, MIN with Examples
SQL CTE Explained with Examples | Common Table Expression Tutorial
SQL CTE Explained with Examples | Common Table Expression Tutorial
SQL Cheat Sheet for Beginners
SQL Cheat Sheet for Beginners
SQL CHARINDEX() Explained with Examples | Find Text Position in SQL Server
SQL CHARINDEX() Explained with Examples | Find Text Position in SQL Server

To include category information, we'll join the Categories table with the previous query. Here's how to achieve this:

SELECT C.CategoryName, SUM(OD.Quantity * P.UnitPrice) AS TotalSales, DENSE_RANK() OVER (ORDER BY SUM(OD.Quantity * P.UnitPrice) DESC) AS CategoryRank FROM Orders O INNER JOIN OrderDetails OD ON O.OrderID = OD.OrderID INNER JOIN Products P ON OD.ProductID = P.ProductID INNER JOIN Categories C ON P.CategoryID = C.CategoryID GROUP BY C.CategoryName ORDER BY TotalSales DESC, CategoryName ASC;

Ranking data is a powerful technique in SQL, enabling you to gain valuable insights from your data. Keep practicing and exploring different use cases to master this essential skill. Happy coding!

Related Articles

Multiple Sql Joins Example What Is Max Date In Sql Server Create Table Sql W3s Values Sql Query W3s Sql Foreign Key W3s Sql Delete How Pivot Works In Sql W3schools Javascript Links Multiple Sql Joins In One Query W3schools Sql Engine