The database:
EMPLOYEE (fname, minit, lname, ssn, birthdate, address, sex, salary, superssn, dno) KEY: ssn
DEPARTMENT (dname, dnumber, mgrssn, mgrstartdate) KEY: dnumber.
PROJECT (pname, pnumber, plocation, dnum) KEY: pnumber.
WORKS_ON (essn, pno, hours) KEY: (essn, pno)
DEPENDENT (essn, dependent-name, sex, bdate, relationship) KEY: (essn, dependent-name)
I want to use left outer join
and group by
to...
Find the last name and SSN of those managers who work on 3 or more projects and who are not located in Cleveland.
Here is what I have so far:
select Lname
from Employee e outer join Department d
where (e.ssn = d.mgrssn)
and ssn NOT in (
select w.essn
from works_on w outer join Project p
where w.pno = p.pnumber
and p.plocation = 'Cleveland'
group by w.essn
having count(*) >= 3
)
Did I do it right using left outer join
and group by
? Should I divide this code into two parts, like loops?
Select JOIN
find all project for the employee
First HAVING
tell you this user doesnt have project in 'Cleveland'
Second HAVING
tell you this user has 3 project or more
.
SELECT e.Lname, e.ssn
FROM Employee e
JOIN works_on w
ON e.ssn = w.essn
JOIN Project p
ON w.pno = p.pnumber
GROUP BY e.ssn
HAVING
SUM(CASE WHEN p.plocation = 'Cleveland' THEN 1 ELSE 0 END) = 0
AND COUNT(*) >= 3
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.