This function is used to display the rank for every record int the table.The SQL server provides the functionality for providing the RANK if required for every row based on column data.
The RANK function is an inbuilt function in the SQL and it is used to get the current rank from the group of data in the table.
The main thing behind fetching the Rank in the SQL is to do self-join on the table, and then count the number of records.
Table Total_Sales
Name Sales
Himanshu 10
Manish 15
Pradeep 20
Mahesh 40
Mayank 50
We want to get the data according to the rank sales have been done by the employees.
SELECT a1.Name, a1.Sales, COUNT (a2.Sales) Sales_Rank
FROM Total_Sales a1, Total_Sales a2
WHERE a1.Sales < a2.Sales OR (a1.Sales=a2.Sales AND a1.Name = a2.Name)
GROUP BY a1.Name, a1.Sales
ORDER BY a1.Sales DESC, a1.Name DESC;
0 Comment(s)