Search code examples
joincommon-table-expressionderived-table

How to get employee name and id who have two phone numbers in second table


enter image description here

These are two tables 1. emp_table, 2. phone_table. How to get employee name and id who have two phone numbers in second table


Solution

  • SELECT t1.id,
           t1.name
    FROM emp_table t1
    INNER JOIN                   -- the join will remove employees who do not have
    (                            -- two phone numbers in phone_table
        SELECT Id
        FROM phone_table         -- this query identifies all employees
        GROUP BY Id              -- having two phone numbers in phone_table
        HAVING COUNT(*) = 2
    ) t2
        ON t1.id = t2.Id