Académique Documents
Professionnel Documents
Culture Documents
Skiptocontent
WOWETLTesting
ABlogForTesters
Menu
Home
ETLTestingTutorial
InterviewQuestions
About
Contact
About
PrivacyPolicy
HowtofindnthmaxvalueinOracleusingrank
TherearenwaystofetchtheNthhighestvaluefromatable,herewecanseehowOracleinbuiltfunctionssuchasDENSE_RANK(),RANK()and
ROW_NUMBER()canbeutilizedforthiscase.
Also,wewillbediscussingtheoptingoftherightfunctionatarightplacebasedonthebusinessrequirement.
Firstwillunderstandtheexactdifferencebetweenthesethreefunctions,thenyouwillbeverycleartochoseamongtheseforyourspecification.
Createtablequerywhichcreatesstructureforemployeetable,
CREATETABLEemployee
(idNUMBER(5),
nameVARCHAR2(20),
deptVARCHAR2(20),
salaryNUMBER(8));
Insertstatementswhichcreateentriesintoemployeetable,
insertintoemployeevalues(1001,'A','HR',100000);
insertintoemployeevalues(1002,'B','HR',200000);
insertintoemployeevalues(1003,'C','HR',300000);
insertintoemployeevalues(1004,'D','QA',200000);
insertintoemployeevalues(1005,'E','QA',700000);
insertintoemployeevalues(1006,'F','TECH',800000);
http://wowetltesting.com/findnthhighestsalaryinsql/ 1/7
11/10/2016 HowtofindnthmaxvalueinOracleusingrank
insertintoemployeevalues(1007,'G','TECH',400000);
insertintoemployeevalues(1008,'H','QA',700000);
insertintoemployeevalues(1009,'I','TECH',900000);
insertintoemployeevalues(1010,'J','QA',600000);
DifferencebetweenRANK(),DENSE_RANK()andROW_NUMBER()
Twoemployeesarehavingsalaryas700000
ThebothRANKandDENSE_RANKwillassignthesameranktobothemployees(rank=3)
ButROWNUMBERwillassigntheuniquenumbersforeachrow,inourexample,itwillmap3and4respectively
Nowlookatthenextrecord,DENSE_RANKwillassign4sincethepreviousoneassignedwas3
ButRANKwillassign5sincerank3itselfithastworecords,itwillskipthe4thrankandassignonly5totheimmediatenext
1005 E QA 700000 3 3 3
1008 H QA 700000 3 3 4
1010 J QA 600000 5 4 5
1.FindthepersonwhoisgettinghighestsalaryintheorganizationusingDENSE_RANK
http://wowetltesting.com/findnthhighestsalaryinsql/ 2/7
11/10/2016 HowtofindnthmaxvalueinOracleusingrank
ApplyingranksortinginsalarydescendingorderusingDENSE_RANK.
SELECT
id,name,dept,salary,DENSE_RANK()over(ORDERBYsalaryDESC)asrankorder
FROMemployee
ORDERBYrankorder
1005 E QA 700000 3
1008 H QA 700000 3
1010 J QA 600000 4
1003 C HR 300000 6
1002 B HR 200000 7
1004 D QA 200000 7
1001 A HR 100000 8
Selectingthefirstrankalonebyusingrankthefunctionaliasnameinwherecondition(WHERErankorder=1),
SELECT
*
FROM(SELECTid,name,dept,salary,DENSE_RANK()OVER(ORDERBYsalaryDESC)asrankorder
FROMemployee)a
WHERErankorder=1;
2.Findthepersonwhoisgettinghighestsalaryineachdeptwise
http://wowetltesting.com/findnthhighestsalaryinsql/ 3/7
11/10/2016 HowtofindnthmaxvalueinOracleusingrank
ApplyingthepartitionclausebydeptandsalaryindescendingorderusingDENSE_RANK.
SELECT
id,name,dept,salary,
DENSE_RANK()OVER(PARTITIONBYdeptORDERBYsalaryDESC)asrankorder
FROMemployee
ORDERBYdept,rankorder
Selectingthefirstrankalonebyusingrankthefunctionaliasnameinwherecondition(WHERErankorder=1),
SELECT
*
FROM(SELECTid,name,dept,salary,DENSE_RANK()OVER(PARTITIONBYdeptORDERBYsalaryDESC)asrankorderFROMemployee)a
WHERErankorder=1;
3.Findthepersonwhoisgettingnthhighestsalaryineachdeptwisebetweenanyspecifiedrange(forexample10to20)
SelectingtherecordswhichfallbetweenarangeofrankusingbetweeninWHEREcondition,
SELECT
*
http://wowetltesting.com/findnthhighestsalaryinsql/ 4/7
11/10/2016 HowtofindnthmaxvalueinOracleusingrank
FROM(SELECTid,name,dept,salary,DENSE_RANK()OVER(PARTITIONBYdeptORDERBYsalaryDESC)asrankorderFROMemployee)a
WHERErankorderbetween2and3;
FormoreinformationsaboutRANK,DENSE_RANKandROW_NUMBERreferhere.
Author:balakrishnan
8yearsofexperienceinETLtestingwithstrongknowledgeofdatawarehouseconceptsandETLconcepts.Alwayspassionateaboutlearningnew
thingsfromfundamental,thinkingandimplementinginnovativeideaswithhighjobsatisfaction.Viewallpostsbybalakrishnan
LeaveaReply
Youremailaddresswillnotbepublished.Requiredfieldsaremarked*
Comment
Name*
Email*
Website
PostComment
http://wowetltesting.com/findnthhighestsalaryinsql/ 5/7
11/10/2016 HowtofindnthmaxvalueinOracleusingrank
Postnavigation
PreviousPreviouspost:JavaText/FlatFileContentReadingWithExceptionHandling
NextNextpost:DuplicateRecordTestingFromMultipleTablesInOracleSQL
AlexaRank
RecentPosts
HowToMinimizetheDataBreachWithTestDataManagementTool
Datamanagementwhenunavailabilityofdata
DatawarehouseAutomationToolDatagapsETLValidator
DuplicateRecordTestingFromMultipleTablesInOracleSQL
HowtofindnthmaxvalueinOracleusingrank
Categories
CoreJava(8)
Datawarehouseconcepts(21)
ETLConcepts(8)
ETLTesting(26)
ETLTestingAutomation(6)
SeleniumAutomation(1)
SQL(3)
Testing(5)
SUBSCRIBE
YourEmail(required)
http://wowetltesting.com/findnthhighestsalaryinsql/ 6/7
11/10/2016 HowtofindnthmaxvalueinOracleusingrank
Send
Home
ETLTestingTutorial
InterviewQuestions
About
Contact
About
PrivacyPolicy
WOWETLTestingProudlypoweredbyWordPressCopyright2016AllRightsReserved
http://wowetltesting.com/findnthhighestsalaryinsql/ 7/7