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

Text copied!

« Previous
Next »

RANK function

June 22, 2023, 7:27 p.m. under SQL

  • Hi! You should check ROW_NUMBER function post first.

    In SQL, the RANK function is used to assign rank to each row in a result set according to the specified criteria in ORDER BY clause.

    It assigns a unique rank to each distinct row in the result set according to the ordering specified in the ORDER BY clause. If two rows have the same values for the ordering criteria, they will be assigned the same rank, and the next rank will be skipped.

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

    • PARTITION BY: This clause divides the result set into partitions to which the RANK() function is applied separately. The function will restart numbering the ranks for each partition.

    • ORDER BY: This clause specifies the order in which the rows are ranked within each partition.

    • rank_column: This is an alias for the resulting rank column.

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

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

    RANK function

    2. Let's assume you want to assign a rank to the students based on their scores within each subject.

    3. Run below SQL statement :

    SELECT
        Student_Name,
        Subjects,
        Score,
        RANK() OVER (PARTITION BY Subjects ORDER BY Score DESC) AS rank_within_subject
    FROM
        [Student];
            

    4. In the above statement, It assigns a rank to each student within each subject based on their score, with the highest scorer receiving rank 1, the next highest scorer receiving rank 2, and so on.

    RANK function

    The RANK() function in SQL assigns ranks to data based on specified criteria, aiding tasks like identifying top performers, implementing pagination, analyzing market share, and comparing competitors, crucial for informed decision-making.

    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 rank function in SQL?

    The RANK() function in SQL assigns a rank to each row in a result set based on the specified criteria.

    What is rank () Dense_rank () in SQL?

    RANK() and DENSE_RANK() are window functions in SQL used to assign rankings to rows within a partition.

    What is difference between rank () ROW_NUMBER () and Dense_rank () in SQL?

    The difference between RANK(), ROW_NUMBER(), and DENSE_RANK() lies in how they handle ties in ranking; RANK() and DENSE_RANK() leave gaps, whereas ROW_NUMBER() assigns unique sequential numbers.

    What is rank of function?

    The RANK OF function is not standard SQL; it seems like a typo or a misinterpretation of the RANK() function.
    Thank You! You should check DENSE_RANK 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

    DENSE_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