7

I want to get the name of the employee who has the minimum salary. Is there a way to do this using only one query? I have given my query below, it doesn't work because the having clause requires a condition. Is there any way to give a condition in the having clause that will retreive the employee name with the minimum salary?

SELECT first_name,min(salary) as "sal"
FROM Employees
GROUP BY first_name 
having min(salary);
Sindu_
  • 1,347
  • 8
  • 27
  • 67

8 Answers8

7

How about using ROWNUM?

SELECT *
FROM(SELECT first_name, salary
     FROM Employees
     ORDER BY salary
) WHERE ROWNUM = 1
Jeffrey Kemp
  • 59,135
  • 14
  • 106
  • 158
MarcinJuraszek
  • 124,003
  • 15
  • 196
  • 263
4
SELECT first_name, salary  as "sal" 
FROM   employees
WHERE  salary =(SELECT MIN(salary) 
                FROM   employees);
Dba
  • 6,511
  • 1
  • 24
  • 33
Nadee
  • 67
  • 3
3

Try this solution, inspired from here:

SELECT e1.first_name, e1.salary AS "sal"
FROM Employees e1
LEFT OUTER JOIN Employees e2
ON (e1.id <> e2.id AND e1.salary > e2.salary)
WHERE e2.id IS NULL;
Community
  • 1
  • 1
evalarezo
  • 1,134
  • 7
  • 13
2

With a single SELECT statement:

SELECT MIN( first_name ) KEEP ( DENSE_RANK FIRST ORDER BY salary ASC, first_name ASC ) AS first_name,
       MIN( salary     ) KEEP ( DENSE_RANK FIRST ORDER BY salary ASC, first_name ASC ) AS salary
FROM   Employees;

SQLFIDDLE

However, if there are multiple people with the same minimum salary then this will only get the one with the name which is first alphabetically.

You can get all the names, but it does require multiple SELECT statements:

SELECT first_name, salary
FROM   Employees
WHERE  salary = ( SELECT MIN(salary) FROM Employees ); 

But having multiple SELECT statements isn't a bad thing.

SQLFIDDLE

MT0
  • 143,790
  • 11
  • 59
  • 117
1
SELECT TOP 1 WITH TIES *
FROM employees
ORDER BY salary ASC
Ben
  • 51,770
  • 36
  • 127
  • 149
Nadee
  • 67
  • 3
0

If you need the employee with the lowest salary why don't you use Order By ..

SELECT top 1 first_name,min(salary) as LowestSalary
FROM Employees order by Salary asc
MusicLovingIndianGirl
  • 5,909
  • 9
  • 34
  • 65
  • Thanks but I tried your query in SQL developer. It gives this error: 00923. 00000 - "FROM keyword not found where expected" – Sindu_ Nov 04 '13 at 04:51
  • 2
    This is incorrect; the OP is using Oracle and TOP is not valid syntax. – Ben Nov 04 '13 at 08:54
0

Try this implementation:

SELECT first_name,min(salary) as "sal"
FROM Employees
GROUP BY first_name 
having min(salary) >0;
Albin
  • 1,000
  • 1
  • 11
  • 33
user63814
  • 11
  • 4
0

If you want to do this with only one query, while also retrieving all employees with the minimum salary (example: you have a minimum salary of $40,000, but two employees have this exact salary) you could join the table with itself. This solution also uses the 'having' clause that you included in your original question.

SELECT e.first_name,e.salary AS "sal"
FROM Employees e, Employees e2
GROUP BY first_name 
HAVING MIN(e2.salary)=e.salary;
RenTheRoot
  • 23
  • 1
  • 7