Affine Analytics Interview Questions | What is a RANK() function? How is it different from ROW_NUMBER()?

Question

SQL

in progress 0
TheDataMonk 4 years 2 Answers 1243 views Grand Master 0

Answers ( 2 )

  1. Rank()
    Select *, Rank() Over(order by salary)as rank from employees

    Row_Number()
    Select *, Row_Number() Over(order by salary) as rowno from employees

    Rank() function will assign same ranks to the observations who have same value(over
    which it has been ordered, in this case salary) whereas Row_Number() will assign a unique Row Number
    to every observation starting from 1 till the count of the rows.

  2. Rank()
    /*Suppose you have employees table consists of Salary*/
    A rank function assigns values according to the salary, If the employees consist of the same salary they are assigned the same rank. On the Other hand, Row_Number() assigns values with respect to the number of rows starting from 1, irrespective of values present in salary.

Leave an answer

Browse
Browse