Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the MIN function in the having clause

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);
like image 678
Sindu_ Avatar asked Nov 04 '13 04:11

Sindu_


People also ask

Can we use MIN function in HAVING clause?

Example - Using MIN function You could also use the SQL MIN function to return the name of each department and the minimum salary in the department. The SQL HAVING clause will return only those departments where the minimum salary is greater than $35,000.

Can we use MIN function in WHERE clause in Oracle?

The aggregate functions can be used in conjunction with the WHERE clause to gain further insights from our data. One of these is the MIN() function. In SQL, the MIN() function is used to compute the smallest or minimum value of numeric values in a column.

What does MIN () do in SQL?

The SQL MIN() and MAX() Functions The MIN() function returns the smallest value of the selected column. The MAX() function returns the largest value of the selected column.

Can we use aggregate function in HAVING clause?

The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.


2 Answers

How about using ROWNUM?

SELECT *
FROM(SELECT first_name, salary
     FROM Employees
     ORDER BY salary
) WHERE ROWNUM = 1
like image 146
MarcinJuraszek Avatar answered Sep 22 '22 13:09

MarcinJuraszek


SELECT first_name, salary  as "sal" 
FROM   employees
WHERE  salary =(SELECT MIN(salary) 
                FROM   employees);
like image 43
Nadee Avatar answered Sep 22 '22 13:09

Nadee