Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find max and second max salary for a employee table MySQL

Tags:

sql

mysql

Suppose that you are given the following simple database table called Employee that has 2 columns named Employee ID and Salary:

  Employee
  Employee ID    Salary
   3            200
   4            800
   7            450

I wish to write a query select max(salary) as max_salary, 2nd_max_salary from employee

then it should return

  max_salary   2nd_max_salary
   800             450

i know how to find 2nd highest salary

   SELECT MAX(Salary) FROM Employee
  WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )

or to find the nth

  SELECT FROM Employee Emp1 WHERE (N-1) = ( SELECT COUNT(DISTINCT(Emp2.Salary)) FROM Employee Emp2
  WHERE Emp2.Salary > Emp1.Salary)

but i am unable to figureout how to join these 2 results for the desired result

like image 814
dpsdce Avatar asked Feb 03 '14 05:02

dpsdce


People also ask

How do you find Max and second max in SQL?

In the 1st example find the 2nd largest value in column “Income” and in the 2nd one find the 2nd largest value in “Cost”. Method-1: Syntax: SELECT MAX (column_name) FROM table_name WHERE column_name NOT IN (SELECT Max (column_name) FROM table_name);

How can find second and third highest salary in MySQL?

SELECT MAX(Salary) From Employee WHERE Salary < ( SELECT Max(Salary) FROM Employee); You can use this SQL query if the Interviewer ask you to get second highest salary in MySQL without using LIMIT.

How do you find the fetch 2nd highest salary in the employee table?

We can nest the above query to find the second largest salary. select *from employee group by salary order by salary desc limit 1,1; There are other ways : SELECT name, MAX(salary) AS salary FROM employee WHERE salary IN (SELECT salary FROM employee MINUS SELECT MAX(salary) FROM employee);

How do you find the maximum salary from an employee table?

Try using this SQL SELECT statement: SELECT * FROM employees WHERE department_id=30 AND salary = (SELECT MAX(salary) FROM employees WHERE department_id=30); This will return the employee information for only the employee in department 30 that has the highest salary.


3 Answers

You can just run 2 queries as inner queries to return 2 columns:

select   (SELECT MAX(Salary) FROM Employee) maxsalary,   (SELECT MAX(Salary) FROM Employee   WHERE Salary NOT IN (SELECT MAX(Salary) FROM Employee )) as [2nd_max_salary] 

SQL Fiddle Demo

like image 153
Szymon Avatar answered Sep 21 '22 12:09

Szymon


Try like this

SELECT (select max(Salary) from Employee) as MAXinmum),(max(salary) FROM Employee WHERE salary NOT IN (SELECT max(salary)) FROM Employee);

(Or)

Try this, n would be the nth item you would want to return

 SELECT DISTINCT(Salary) FROM table ORDER BY Salary DESC LIMIT n,1

In your case

 SELECT DISTINCT(column_name) FROM table_name ORDER BY column_name DESC limit 2,1;
like image 44
Vignesh Kumar A Avatar answered Sep 20 '22 12:09

Vignesh Kumar A


Simplest way to fetch second max salary & nth salary

select 
 DISTINCT(salary) 
from employee 
 order by salary desc 
limit 1,1

Note:

limit 0,1  - Top max salary

limit 1,1  - Second max salary

limit 2,1  - Third max salary

limit 3,1  - Fourth max salary
like image 26
Swapnil Kumbhar Avatar answered Sep 20 '22 12:09

Swapnil Kumbhar