0

enter image description here

The problem is :Find the employee last names for employees who do not work on any projects.

My solution is:

SELECT E.Lname 
FROM EMPLOYEE E
WHERE E.Ssn NOT IN (SELECT * 
                    FROM EMPLOYEE E,WORKS_ON W
                    WHERE E.Ssn = W.Essn);

This should subtract the ssns from employee from the essns from works_on, However I keep getting the error "Operand should contain 1 column". What does this mean and how can I correct this code?

Dante
  • 537
  • 2
  • 4
  • 18

4 Answers4

1

The result of a NOT IN subquery must be one value. Your subquery returns all the columns in the EMPLOYEE and WORKS_ON tables. You can use NOT EXISTS instead:

SELECT E.Lname 
FROM EMPLOYEE E
WHERE NOT EXISTS (SELECT 1 
                  FROM WORKS_ON W
                  WHERE E.Ssn = W.Essn);

The 1 could be any scalar, or it could even be NULL.

mechanical_meat
  • 163,903
  • 24
  • 228
  • 223
1
SELECT E.Lname 
FROM EMPLOYEE E
WHERE E.Ssn NOT IN (SELECT E.Ssn 
                    FROM EMPLOYEE E,WORKS_ON W
                    WHERE E.Ssn = W.Essn);

not in , means not include the another value sets. So, in the next query, it should select the value set.

SELECT E.Ssn 
FROM EMPLOYEE E,WORKS_ON W
WHERE E.Ssn = W.Essn

this part will be return all of the employee's SSN who has worked.

hope it explain your questions

Chuck
  • 148
  • 1
  • 6
1
SELECT E.Lname 
FROM EMPLOYEE E
WHERE E.Ssn NOT IN (SELECT distinct Essn 
                    FROM WORKS_ON);
Amit Garg
  • 3,867
  • 1
  • 27
  • 37
1

Try this

SELECT EMPLOYEE.Lname FROM EMPLOYEE WHERE EMPLOYEE.SSN NOT IN (SELECT DISTINCT WORKS_ON.Essn FROM WORKS_ON);
Liudi Wijaya
  • 898
  • 2
  • 8
  • 24