Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Another way to write HAVING EVERY and HAVING ANY in Mysql

Tags:

mysql

I am trying to find the enames of managers who manage only departments with budgets larger than 1 million, but at least one department with budget less than 5 million

I understand that the query should work like this but can not get it to run in Mysql. Im guessing because Every and Any in the having do not work in mysql. Is there any alternate way to write this? Also here is the schema

Emp(eid: integer, ename: string, age: integer, salary: real) 
Works(eid: integer, did: integer, pct_time: integer) 
Dept(did: integer, dname: string, budget: real, managerid: integer)

And here is the query:

select 
  e.ename 
from 
  emp e, dept d 
where 
  e.eid=d.managerid 
group by 
  e.eid, e.ename 
having 
  every(d.budget>1000000) and 
  any(d.budget<5000000); 
like image 981
user964381 Avatar asked Dec 14 '11 12:12

user964381


People also ask

What works similar to HAVING clause in SQL?

Explanation: “HAVING” clause are worked similar as “WHERE” clause.

What can I use instead of GROUP BY in MySQL?

Having Clause is basically like the aggregate function with the GROUP BY clause. The HAVING clause is used instead of WHERE with aggregate functions. While the GROUP BY Clause groups rows that have the same values into summary rows.

What is the order of operations in SQL?

Six Operations to Order: SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY.

Is HAVING a clause in MySQL?

Description. The MySQL HAVING clause is used in combination with the GROUP BY clause to restrict the groups of returned rows to only those whose the condition is TRUE.


1 Answers

SELECT e.ename
FROM   emp e
       JOIN dept d
         ON e.eid = d.managerid
GROUP  BY e.eid,
          e.ename
HAVING MIN(d.budget) > 1000000
       AND MIN(d.budget) < 5000000;  
like image 55
Martin Smith Avatar answered Oct 05 '22 04:10

Martin Smith