Suppose that you are given the following simple database table called Employee that has 2 columns named Employee ID and Salary:
Employee
Employee ID S
Max Salary:
select max(salary) from tbl_employee <br><br>
Second Max Salary:
select max(salary) from tbl_employee where salary < ( select max(salary) from tbl_employee);
The Best & Easiest solution:-
SELECT
max(salary)
FROM
salary
WHERE
salary < (
SELECT
max(salary)
FROM
salary
);
If we want to find Employee that gets 3nd highest salary then execute this query
SELECT a.employeeid,
a.salary
FROM (SELECT employeeid,
salary,
Dense_rank()
OVER(
ORDER BY salary) AS n
FROM employee) AS a
WHERE n = 3
What do you want
This solution will give all employee name and salary who have second highest salary
SELECT name, salary
FROM employee
WHERE salary = (
SELECT
salary
FROM employee AS emp
ORDER BY salary DESC
LIMIT 1,1
);
without nested query
select max(e.salary) as max_salary, max(e1.salary) as 2nd_max_salary
from employee as e
left join employee as e1 on e.salary != e1.salary
group by e.salary desc limit 1;
try this
select max(salary) as first,
(select salary from employee order by salary desc limit 1, 1) as second
from employee limit 1