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);
Explanation: “HAVING” clause are worked similar as “WHERE” clause.
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.
Six Operations to Order: SELECT, FROM, WHERE, GROUP BY, HAVING, and ORDER BY.
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.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With