SQL get names who didnt work on a project

后端 未结 4 1450
刺人心
刺人心 2021-01-27 10:01

I would like to retrieve the names of people who didn\'t work on a project in PostgreSQL.

I got a table named employees with their names and

相关标签:
4条回答
  • 2021-01-27 10:36

    This is usually solved using a NOT EXISTS query:

    select e.*
    from employees e
    where not exists (select *
                      from works_on wo
                      where wo.ssn = e.ssn)
    
    0 讨论(0)
  • 2021-01-27 10:47

    This is easy with NOT IN:

    select * from employees
    where ssn not in (select ssn from works_on)
    

    Alternative with LEFT JOIN:

    SELECT fname,lname
    FROM employee w
    LEFT JOIN works_on wa 
    ON (wa.ssn = w.ssn)
    WHERE wa.ssn IS NULL
    

    This alternative is useful as you sometimes have more complicated requirements that can't easily be expressed with IN or EXISTS.

    0 讨论(0)
  • 2021-01-27 10:50

    try this

    SELECT fname,lname
    FROM werknemer w
    WHere w.snn not in (Select wa.ssn from werkt_aan wa)
    
    0 讨论(0)
  • 2021-01-27 10:50

    you'll need to use NOT IN and a subquery here

    0 讨论(0)
提交回复
热议问题