Monday, January 19, 2015

SQL: WHERE clause with SELECT multiple columns IN / NOT IN

You need to put the columns separated by comma in parentheses before IN / NOT IN and put the SELECT statement after the IN / NOT IN in parentheses too. For example you want to get the information of all the managers except the managers in the human resource department, the SQL below works.

SELECT *
FROM MANAGER m1
WHERE (m1.ID, m1.SSN, m1.PHONE) NOT IN (
      SELECT m2.ID, m2.SSN, m2.PHONE
       FROM MANAGER m2, EMPLOYEE e
      WHERE m2.ID = e.ID AND e.departmentID = 'humanResource';

OR
      SELECT *
      FROM MANAGER m1
      WHERE (m1.ID, m1.SSN, m1.PHONE) NOT IN (
             SELECT e.ID, e.SocialID, e.PhNbr
             FROM EMPLOYEE e
             WHERE e.departmentID = 'humanResource';

--------------------------------------------------------------------------------------------------------------------

                        
If you have ever asked yourself these questions, this is the book for you. What is the meaning of life? Why do people suffer? What is in control of my life? Why is life the way it is? How can I stop suffering and be happy? How can I have a successful life? How can I have a life I like to have? How can I be the person I like to be? How can I be wiser and smarter? How can I have good and harmonious relations with others? Why do people meditate to achieve enlightenment? What is the true meaning of spiritual practice? Why all beings are one? Read the book free here.

No comments:

Post a Comment