views:

70

answers:

5
  1. what is the query to return Name and Salary of employee Having Max Salary

A: 
Select e.name, e.salary from employee e where
  not exists (select salary from employee e1 where e.salary < e1.salary)

This will of course return more than one record if there are multiple people with the max salary.

deinst
+5  A: 
SELECT Name, Salary FROM Minions
WHERE Salary = (SELECT Max(Salary) FROM Minions)
Neil N
+1: For calling the table "Minions"
OMG Ponies
@OMG Ponies: If you leave the implementation details to me, I will have some fun with them ;)
Neil N
A: 

set rowcount 1 select name, salary from employees order by salary desc

or

select name, salary from employees where salary=max(salary)

Of course this could be dependant on type of sql (which you didn't supply), column names (which you didn't supply), table names (which you didn't supply)...

So...take that for what it's worth.

Webjedi
A: 
select name, salary from (select * from salary_table order by salary desc limit 1)
publicRavi
I didn't downvote you but that assumes using MySQL, PostgreSQL, or SQLite - won't work on Oracle or SQL Server. Also, don't need the subquery.
OMG Ponies
Thanks for the feedback. If only the person who down-voted explained why :)
publicRavi
OMG Ponies
A: 

A couple of proprietary solutions

SELECT TOP 1 [WITH ties] Name, Salary
FROM employee
ORDER BY  Salary DESC


SELECT Name, Salary
FROM employee
ORDER BY  Salary DESC
LIMIT 1

And a standard one

WITH E AS
(
    SELECT Name, Salary, 
    ROW_NUMBER() OVER (ORDER BY Salary DESC) RN /*Or RANK() for ties*/
    FROM employee
)
SELECT Name, Salary FROM E WHERE RN=1
Martin Smith
Interestingly, all of these seem to ignore the fact that there could be more than one employee with the maximum salary value. Of course, the question did ask for an "employee" but it could really be "employees".
Steven Oxley