Hi All,
Here I am going to show you how to get the second highest salary among all employees in a table
Tricky SQL Questions
SQL query to get second highest salary
SQL code for finding second highest salary
Write SQL query to get the second highest salary among all employees|
QUERY STATEMENT:
select id,name ,salary,rank() over (order by salary desc) AS RANKNO from salary;
+------+---------+--------+--------+
| id | name | salary | RANKNO |
+------+---------+--------+--------+
| 3 | Pravin | 50000 | 1 |
| 4 | Jay | 44000 | 2 |
| 2 | Prakash | 40000 | 3 |
| 6 | Vikash | 2500 | 4 |
| 5 | Vivek | 2200 | 5 |
| 1 | Ajay | 2000 | 6 |
+------+---------+--------+--------+
To get 2nd highest-
select A.id ,A.name ,A.salary from (select id ,name ,salary , rank() over (order by salary desc)
as rankno from salary) A where rankno =2;
+------+------+--------+
| id | name | salary |
+------+------+--------+
| 4 | Jay | 44000 |
+------+------+--------+
3rd minimum-
select A.id,A.name ,A.salary from (select id ,name ,salary , rank() over (order by salary asc) as rankno from salary) A where rankno =3;
+------+--------+--------+
| id | name | salary |
+------+--------+--------+
| 6 | Vikash | 2500 |
+------+--------+--------+
1
*****************************
#mysql
#secondhighestsalary
#TrickyMySQLQuestions