SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Our Services
Contact
  1. SQL
  2. DENSE_RANK function

Text copied!

« Previous
Next »

DENSE_RANK function

June 23, 2023, 7:29 p.m. under SQL

  • Hi! You should check RANK function post first.

    In SQL, the DENSE_RANK function assigns a rank to each row in a result set, with no gaps in the ranking values.

    If multiple rows have the same value, they get the same rank, and the next rank is incremented by 1, without any gaps.

    The syntax of the DENSE_RANK function generally looks like this :
    SELECT
        column1,
        column2,
        column3,
        DENSE_RANK() OVER (PARTITION BY partition_column ORDER BY order_column [ASC | DESC]) AS dense_rank_column
    FROM
        table_name;
            

    • PARTITION BY: This is an optional clause that divides the result set into partitions. The ranking will be calculated separately for each partition.

    • ORDER BY: This clause specifies the column(s) based on which the ranking is determined. You can specify multiple columns for tie-breaking.

    Here's an example of how you might use the DENSE_RANK function :

    1. Let's assume we have a table named "[Student]".

    DENSE_RANK function

    2. Let's assume you want to assign a rank to the students based on their score.

    3. Run below SQL statement :

    SELECT 
        Student_Name,
        Score,
        DENSE_RANK() OVER (ORDER BY Score DESC) AS ScoreRank
    FROM Student;
            

    4. In the above statement, student with the highest score are assigned a rank of 1, and so on. Notice that students with the same score are given consecutive ranks without any gaps, which is the characteristic of the DENSE_RANK function.

    DENSE_RANK function

    Overall, the DENSE_RANK() function is a versatile tool in SQL that facilitates various analytical tasks by providing ordered rankings within result sets, handling ties gracefully, and enabling partitioned ranking for deeper data analysis.

    Remember :

    Using this function in a select statement won't modify the [Student] table directly, but it will only be reflected in the select statement's output.

    To enhance your understanding of 'SQL functions', be sure to check out our next tutorial.

    Frequently Asked Questions :

    What is the difference between the rank () and Dense_rank () functions?

    RANK() assigns the same rank to tied rows, leaving gaps in subsequent ranks, while DENSE_RANK() assigns consecutive ranks to tied rows without any gaps.

    What is the diff between Rownum () rank () and Dense_rank ()?

    ROWNUM() returns a unique number for each row in the result set, while RANK() and DENSE_RANK() assign rankings based on specified criteria.

    How to filter Dense_rank in SQL?

    To filter DENSE_RANK() in SQL, you can use a WHERE clause specifying the desired rank range or combine it with other conditions.

    What is dense rank in SQL Developer?

    In SQL Developer, DENSE_RANK() is a window function used to assign a rank to rows within a specified window or partition without any gaps between ranks.
    Thank You! You should check IIF function post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    CASE statement

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India support@sqlforgeeks.com

    © 2025 Copyright™ | All Rights Reserved | Privacy Policy