How to find second highest salary of a given table in oracle database | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 36

How to find second highest salary of a given table in oracle database

10th Dec 2018, 4:23 PM
Jyoti Giri
16 Answers
+ 15
SELECT max(salary) FROM Employee WHEREsalary < (SELECT max(salary) FROM Employee);
10th Dec 2018, 5:38 PM
Adarsh Srivastava
Adarsh Srivastava - avatar
+ 30
Sorry sorry it's execute thank u so much Adarsh Srivastava 🙏🙏🙏
10th Dec 2018, 6:12 PM
Jyoti Giri
+ 22
Adarsh Srivastava only second highest salary.. If we used this command then all rows selected expected highest salary of a table..
10th Dec 2018, 6:01 PM
Jyoti Giri
+ 8
Mind that this is an untested query, an alternative by using ROWNUM limiter: SELECT salary FROM payroll WHERE ROWNUM = 2 ORDER BY salary DESC;
11th Dec 2018, 3:50 AM
Ipang
+ 8
Ipang If this doesn't work, the ORDER BY clause may need to be applied as a subquery. Example: SELECT * FROM ( SELECT salary FROM payroll ORDER BY salary DESC ) WHERE ROWNUM = 2;
11th Dec 2018, 5:09 AM
David Carroll
David Carroll - avatar
+ 6
[aside] Is this homework? I ask because I see this question semi-regularly with minor changes that don't make the problem that's actually being solved unique. One top-level abstraction could cover this case and numerous variations (vs the one-off's), but the value of an abstraction depends on whether this is likely to keep repeating.
10th Dec 2018, 6:34 PM
Kirk Schafer
Kirk Schafer - avatar
+ 6
Yes David Carroll that probably how I should've written it in the first place : ) I can't test it though, nowhere near any server, but your query looks more selective than my proposal. Thanks David ; )
11th Dec 2018, 5:16 AM
Ipang
+ 4
Jyoti I dont think so....It will give 2nd highest salary surely..
10th Dec 2018, 6:04 PM
Adarsh Srivastava
Adarsh Srivastava - avatar
+ 3
Jyoti no need to be sorry...My pleasure
10th Dec 2018, 6:15 PM
Adarsh Srivastava
Adarsh Srivastava - avatar
+ 3
Not a comment on the challenge or in a working technique, just on how I write some queries. I have good success using Common Table Expressions (CTE). It's a formalized technique to combine more than one query as named entities. It's usually pretty fast for me to come up with good answers to complex queries. Opening word is WITH: WITH name1 AS ( query_1 ), name2 AS ( query_2 ) query_3 ; If there are more queries to add, the process is repreated with all subqueries, and followed by the outside query. WITH COSAL AS ( SELECT salary , empid, fname, lname, dept FROM payroll ORDER BY salary DESC ) SELECT salary FROM COSAL WHERE ROWNUM = 2 ; I just find this format easier to read, construct, and use. If I need to design as I go, this is the simplest way for me to keep things straight.
11th Dec 2018, 2:10 PM
JoeSponge
JoeSponge - avatar
+ 3
Use ranking select * from ( select salary , dense_rank() over (order by salary desc) ranking from employee ) where ranking = 2 -- Replace 2 with any value of N
12th Dec 2018, 10:57 AM
Prokopios Poulimenos
Prokopios Poulimenos - avatar
+ 1
How do you mean pls
10th Dec 2018, 5:38 PM
success
success - avatar
+ 1
Select salary from (Select salary from Employee order by salary desc limit 1) order by salary aesc limit 1
11th Dec 2018, 1:25 PM
abhiyan
abhiyan - avatar
+ 1
Select max(salary) from Employee where salary <>(Select max(salary)from Employee)
27th Dec 2018, 10:57 AM
Sunny Pamnani
Sunny Pamnani - avatar
0
Тут есть русский
12th Dec 2018, 7:48 AM
юлия Гординская
юлия Гординская - avatar
0
Nice joti
4th Apr 2020, 9:54 PM
Ariberry
Ariberry - avatar