在having子句中使用MIN函数

时间:2013-11-04 04:36:39

标签: sql oracle min having

我想获得具有最低工资的员工的姓名。有没有办法只使用一个查询?我在下面给出了我的查询,它不起作用,因为having子句需要一个条件。有没有办法在having子句中给出一个条件,用最低工资来检索员工姓名?

SELECT first_name,min(salary) as "sal"
FROM Employees
GROUP BY first_name 
having min(salary);

7 个答案:

答案 0 :(得分:6)

如何使用ROWNUM

SELECT *
FROM(SELECT first_name, salary
     FROM Employees
     ORDER BY salary
) WHERE ROWNUM = 1

答案 1 :(得分:3)

SELECT first_name, salary  as "sal" 
FROM   employees
WHERE  salary =(SELECT MIN(salary) 
                FROM   employees);

答案 2 :(得分:2)

尝试此解决方案,灵感来自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;

答案 3 :(得分:1)

SELECT TOP 1 WITH TIES *
FROM employees
ORDER BY salary ASC

答案 4 :(得分:1)

使用单个SELECT声明:

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

但是,如果有多个人具有相同的最低工资,那么这将只获得名称首先按字母顺序排列的人。

您可以获取所有名称,但它确实需要多个SELECT语句:

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

但是,拥有多个SELECT语句并不是件坏事。

SQLFIDDLE

答案 5 :(得分:0)

如果您需要薪水最低的员工,为什么不使用订购依据 ..

SELECT top 1 first_name,min(salary) as LowestSalary
FROM Employees order by Salary asc

答案 6 :(得分:0)

选择first_name,min(salary)作为“ sal” 来自员工 GROUP BY first_name min(salary)> 0;