Rank function in sql
How to use rank function in sql.
3 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Mangesh barmate87 reputationPosted Sep 7, 2012, 8:19 AM EDT
Rank() function generates gap, but Dense_Rank() function not generate gap.
Gap means if you have 2 common value lets say 500 ist value and 500 2nd value after 400 3rd value. then Rank() gives 1,1,3.
But Dense_Rank() function gives 1,1,2.
See below link....
http://www.c-sharpcorner.com/UploadFile/6897bc/ranking-functions/
Try this!!!
Sukesh MarlaRank #461 · 5.4K reputationPosted Sep 7, 2012, 9:14 AM EDT
http://www.sukesh-marla.com/2010/08/using-ranking-functions.html
Check this is correct answer if it helped.
Akkiraju IvaturiRank #473 · 5.3K reputationPosted Sep 7, 2012, 2:42 PM EDT
USE AdventureWorks;GO
SELECT c.FirstName, c.LastName
,ROW_NUMBER() OVER (
ORDER BY a.PostalCode) AS 'Row Number'
,RANK() OVER (
ORDER BY a.PostalCode) AS 'Rank'
,DENSE_RANK() OVER (
ORDER BY a.PostalCode) AS 'Dense Rank'
,NTILE(4) OVER (
ORDER BY a.PostalCode) AS 'Quartile'
,s.SalesYTD, a.PostalCode
FROM Sales.SalesPerson s
INNER JOIN Person.Contact c
ON s.SalesPersonID = c.ContactID
INNER JOIN Person.Address a
ON a.AddressID = c.ContactID
WHERE TerritoryID IS NOT NULL
AND SalesYTD <> 0;
Resultset: