what is the query to return Name and Salary of employee Having Max Salary
views:
70answers:
5
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
2010-07-28 17:45:29
+5
A:
SELECT Name, Salary FROM Minions
WHERE Salary = (SELECT Max(Salary) FROM Minions)
Neil N
2010-07-28 17:45:54
+1: For calling the table "Minions"
OMG Ponies
2010-07-28 17:50:01
@OMG Ponies: If you leave the implementation details to me, I will have some fun with them ;)
Neil N
2010-07-28 18:18:50
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
2010-07-28 17:46:01
A:
select name, salary from (select * from salary_table order by salary desc limit 1)
publicRavi
2010-07-28 17:46:27
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
2010-07-28 17:48:31
Thanks for the feedback. If only the person who down-voted explained why :)
publicRavi
2010-07-28 17:50:12
OMG Ponies
2010-07-28 18:00:04
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
2010-07-28 17:48:40
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
2010-07-28 20:46:46