Affine Analytics Interview Questions | What is a RANK() function? How is it different from ROW_NUMBER()?
Question
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
It will take less than 1 minute to register for lifetime. Bonus Tip - We don't send OTP to your email id Make Sure to use your own email id for free books and giveaways
Answers ( 2 )
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.
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.