Using the ROW_NUMBER Function in SQL
📖 Scenario: You work at a bookstore that wants to list books with a unique rank number based on their sales. This helps the store see which books are the top sellers.
🎯 Goal: Create a SQL query that assigns a row number to each book ordered by the number of sales, starting from 1 for the highest-selling book.
📋 What You'll Learn
Create a table called
books with columns id, title, and sales.Insert exactly three rows into the
books table with specified values.Write a query that uses the
ROW_NUMBER() function to assign a rank to each book ordered by sales in descending order.Select the
id, title, sales, and the row number as rank.💡 Why This Matters
🌍 Real World
Ranking items by sales or popularity is common in business reports, leaderboards, and dashboards.
💼 Career
Understanding ROW_NUMBER() helps in writing advanced SQL queries for data analysis and reporting roles.
Progress0 / 4 steps