When working with databases and analyzing data, ranking records is very important for organizing information based on certain conditions. One ranking function called `DENSE_RANK()` is useful because it assigns ranks to rows without leaving any empty spaces or gaps. This guide explains what `DENSE_RANK()` is, how it operates, and when to use it effectively in SQL.
DENSE_RANK()
.DENSE_RANK()
in conjunction with other SQL functions to produce detailed and insightful reports.The DENSE_RANK() function in SQL assigns a rank number to each row within a section or partition of the results. It works differently than the RANK() function, which may skip rank numbers when there are ties or identical values. With DENSE_RANK(), the ranks are assigned one after the other continuously, with no gaps. So if two rows have the same value and are tied for a rank, the very next rank number is used right after, without skipping any numbers.
SQL
DENSE_RANK() OVER (
[PARTITION BY partition_expression]
ORDER BY sort_expression [ASC | DESC]
)
To understand how `DENSE_RANK()` works, let’s consider an example. Suppose you have a table named `sales` with the following data:
| Product | Sales |
|---------|-------|
| A | 100 |
| B | 200 |
| C | 200 |
| D | 300 |
Using the `DENSE_RANK()` function to rank these products by their sales in descending order would look like this:
SQL
SELECT Product, Sales,
DENSE_RANK() OVER (ORDER BY Sales DESC) AS Rank
FROM sales;
The result would be:
| Product | Sales | Rank |
|---------|-------|------|
| D | 300 | 1 |
| B | 200 | 2 |
| C | 200 | 2 |
| A | 100 | 3 |
As shown, products B and C have the same sales amount and are both ranked 2nd. The next rank is 3rd, without any gaps.
`DENSE_RANK()` is particularly useful in various scenarios, such as:
Let’s explore a few examples to illustrate the use of `DENSE_RANK()` in different contexts.
Consider a `products` table with columns `product_id`, `product_name`, and `price`. To rank products by their price in descending order:
SQL
SELECT product_id, product_name, price,
DENSE_RANK() OVER (ORDER BY price DESC) AS price_rank
FROM products;
This query will assign ranks to products based on their price, with the highest-priced product ranked first.
Suppose you have an `employees` table with columns `employee_id`, `department_id`, and `salary`. To rank employees within each department by their salary:
SQL
SELECT employee_id, department_id, salary,
DENSE_RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS salary_rank
FROM employees;
This query will rank employees within each department separately, ensuring that the ranking is based on their salary.
While both `RANK()` and `DENSE_RANK()` are used to rank rows based on specified criteria, they differ in handling ties:
Example:
Given the same `sales` table, using `RANK()` instead of `DENSE_RANK()`:
SQL
SELECT Product, Sales,
RANK() OVER (ORDER BY Sales DESC) AS Rank
FROM sales;
The result would be:
| Product | Sales | Rank |
|---------|-------|------|
| D | 300 | 1 |
| B | 200 | 2 |
| C | 200 | 2 |
| A | 100 | 4 |
Notice the gap between ranks 2 and 4.
The `DENSE_RANK()` function is a useful tool in SQL for giving rank numbers to rows in a dataset based on certain conditions. The ranks will be one after the other, without any gaps, even if some rows have the same value and are tied. Understanding and using `DENSE_RANK()` can improve your ability to analyze data effectively and present it clearly. Whether you need to identify top performers, deal with ties or identical values, or do statistical analysis, `DENSE_RANK()` provides a solid way to rank data without leaving any empty spaces in the ranking sequence.
A. When there are ties in the ranking sequence, the SQL DENSE_RANK() method prevents gaps by giving a rank to each row inside a partition of the result set.
A. Whereas RANK() inserts gaps in the ranking sequence after tied values, DENSE_RANK() assigns the same rank to tied values without any gaps.
A. Yes, you can rank rows within different partitions of a result set using DENSE_RANK() and the PARTITION BY clause. This enables distinct ranking sequences according to the designated order within every partition.