Skip to main content

Rank Functions in SQL SERVER


1. ROW_NUMBER() OVER ([PARTITION BY CLAUSE] <ORDER BY CLUASE>):

Returns the sequantial number of a row within the a partition of result set at 1 for the first row of the each partition.

2. RANK() OVER ([PARTITION BY CLAUSE] <ORDER BY CLUASE >):

Returns rank for rows within the partition of result set.

3. DENSE_RANK() OVER ([PARTITION BY CLAUSE] <ORDER BY CLUASE >):

Returns rank for rows within the partition of result set.With out any gaps in the ranking.

4. NTILE(INTEGER_EXPRESSION) OVER ([PARTITION BY CLAUSE] <ORDER BY CLUASE >):

Distributes the rows in an ordered partition into a specified number of groups.

Examples:


--create Employee table
create table Employee
(
                EmpId int identity(1,1) primary key,
                FirstName varchar(100),
                LastName varchar(100),
                JoinDate datetime ,
                Salary int ,
                Department varchar(20)
)

--Insert data to Employee table

insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Rakesh','Kalluri','2012-07-01 10:00:00.000',20000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Shabari','Vempati','2011-05-01 10:00:00.000',25000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Venkatesh','Bodupaly','2013-04-01 10:00:00.000',15000,'Bpo')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Surjan','Peddineni','2011-07-01 10:00:00.000',25000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Nani','Ch','2010-07-01 10:00:00.000',50000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Raju','Chinna','2012-07-01 10:00:00.000',25000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Kiran','Kumar','2011-07-01 10:00:00.000',20000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Raki','Kumar','2012-07-01 10:00:00.000',17000,'Bpo')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Sri','Vidya','2011-07-01 10:00:00.000',30000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Fehad','MD','2013-07-01 10:00:00.000',20000,'Bpo')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Anusha','Kumari','2011-07-01 10:00:00.000',35000,'Software')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department) values('Venky','Naidu','2013-07-01 10:00:00.000',20000,'Bpo')
insert into Employee(FirstName,LastName,JoinDate,Salary,Department)values('Radha','Kumari','2012-07-01 10:00:00.000',10000,'Bpo')

--selecting data from Employee  table
select * from Employee
  





Row_Number() with out using partition cluase
select * ,row_number() over (order by Salary desc) as Row_Num from Employee


  



Row_Number() with using partition cluase
select * ,row_number() over (partition by Department order by Salary desc) as Row_Num from Employee






rank() with out using partition cluase
select * ,rank() over (order by Salary desc) as [Rank] from Employee






rank() with using partition cluase
select * ,rank() over (partition by Department order by Salary desc) as [Rank] from Employee




dense_rank() with out using partition cluase
select * ,dense_rank() over (order by Salary desc) as [Dense_rank] from Employee





dense_rank() with using partition cluase
select * ,dense_rank() over (partition by  Department order by Salary desc) as [Dense_rank] from Employee




ntile(input_exp) with out using partition cluase
select * ,ntile(3) over (order by Salary desc) as [ntile] from Employee

In Ntile it accepts the input parameter based on input it divides the row ranking.









ntile(input_exp) with using partition cluase
select * ,ntile(3) over (partition by De6partment order by Salary desc) as [ntile] from Employee



Comments

Post a Comment

Popular posts from this blog

Coalesce function

Coalesce function returns the first non-null value among the arguments. Syntax: Coalesce (expression [,..n]) Here is example using Coalesce function Example 1 DECLARE @Str1 varchar ( 10 ), @str2 varchar ( 20 ), @Str3 varchar ( 20 ) SET @Str2 = 'Sql' , @Str3 = 'Server' SELECT COALESCE ( @Str1 , @str2 , @Str3 ) As [Coalesce] In above example @Str2 value is ‘Sql’ , @str3 value is ‘Server’  and @str1 values is Null because it not assigned any value . Output: It return’s “Sql” because Coalesce function return’s first non null value. Example 2: Coalesce in select statement. IF OBJECT_ID ( 'Employee' , 'U' ) IS NOT NULL DROP TABLE Employee CREATE TABLE Employee (   ID INT IDENTITY ( 1 , 1 ) PRIMARY KEY ,   NAME VARCHAR ( 20 ),   SALARY INT ) INSERT INTO Employee   ( NAME , SALARY ) VALUES ( 'Rakesh' , 5000 ),(NULL, 6000 ),( 'Naresh...

IDENTITY COLUMNS

Identity column specifies when ever new value is inserted into table. Identity column automatically inserted value into column. Identity Column can be specified at the time of creation of table. CREATE TABLE Employee (  EMPID INT IDENTITY ( 1 , 1 ),  EMPNAME VARCHAR ( 50 ),  SALARY INT ) In above table creation script EMPID column specifies IDENTITY property i.e., whenever a new row inserted into a Employee table EMPID Column will automatically value is inserted. INSERT INTO Employee ( EMPNAME , SALARY ) VALUES ( 'RAKESH' , 7000 ) INSERT INTO Employee ( EMPNAME , SALARY ) VALUES ( 'ALI' , 12000 ) SELECT * FROM Employee When u run above select statement is the following result is if u are trying insert value into identify columns .it will give error. INSERT INTO Employee ( EMPID , EMPNAME , SALARY ) VALUES ( 1 , 'RAKESH' , 7000 ) Msg 5...