SQL
Rank Rows within Groups Using ROW_NUMBER()
Learn to use the ROW_NUMBER() window function to assign a unique rank to each row within a partitioned set of data in SQL, ordered by specific criteria.
SELECT
id, name, score, category,
ROW_NUMBER() OVER (PARTITION BY category ORDER BY score DESC) as rank_in_category
FROM scores_table;
How it works: This query assigns a sequential rank to each row within groups defined by `category`, based on `score` in descending order. `ROW_NUMBER()` is a window function that assigns a unique, sequential integer to each row within its partition, starting from 1.