Using the MIN function in the having clause

后端 未结 7 1246
感动是毒
感动是毒 2021-01-18 07:06

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

7条回答
  •  长情又很酷
    2021-01-18 07:35

    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

提交回复
热议问题