This is the database:
EMPLOYEE (fmane, 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)
The question asked is...
Give the last name and SSN of the unmarried employees who work on two or more projects.
SELECT e.Lname, e.ssn
FROM Employee AS e
WHERE e.ssn IN (
SELECT w.essn
FROM works_on w
GROUP BY w.essn
HAVING count(*) >= 2
)
AND e.ssn NOT IN (
SELECT essn
FROM dependent
WHERE relationship = 'Spouse'
);
My answer was wrong because I didn't use the e.ssn NOT IN
clause.
My questions are:
1) What is the group by
used for? strictly for works on only?
2) Regarding FROM Employee AS e
, is AS
a typo or a command?
3) Does having
mean including? Can it be replaced by some other command?