title: MYSQL - Select Unique Common Columns between two tables - Most Efficient Query
body: <p>I have two tables:</p>
  
  <p><strong>db_contacts</strong></p>
  
  <pre><code>Phone | Name | Last_Name
  --------------------
  111   | Foo  | Foo
  222   | Bar  | Bar
  333   | John | Smith
  444   | Tomy | Smith
  </code></pre>
  
  <p><strong>users_contacts</strong></p>
  
  <pre><code>User_ID | Phone
  --------------------
  1       | 123
  1       | 111
  2       | 222
  2       | 333
  3       | 111
  3       | 333
  4       | 444
  </code></pre>
  
  <p><strong>Notice from above that:</strong></p>
  
  <ul>
  <li>User with ID 2 is the only one that have the phone number 222</li>
  <li>User with ID 4 is the only one that have the phone number 444</li>
  </ul>
  
  <p>I need to obtain these results with a MySQL query.</p>
  
  <p>In other words: How can I select all the users that have a unique phone number in condition that this number exists in the db_contacts.</p>
  
  <p>I need my end result to be something like that:</p>
  
  <pre><code>User_ID | Phone | Name | Last_Name
  ------------------------------------
  2       | 222   | Bar  | Bar
  4       | 444   | Tomy | Smith
  </code></pre>
  
  <p><strong>PS: There is no Foreign key between the Phone columns, as a User can have a phone that is not in the db_contacts.</strong></p>
  
  <p>In real life, db_contacts contains about 1 million records and users_contacts about 5 million records.</p>
  
  <p><strong>What I tried and failed and taking a lot of time to execute:</strong></p>
  
  <pre><code>SELECT * 
  FROM users_contacts 
  WHERE users_contacts.phone IN (
      SELECT users_contacts.phone 
      FROM `users_contacts`
      JOIN db_contacts ON db_contacts.phone = users_contacts.phone
      GROUP BY users_contacts.phone
      HAVING COUNT(users_contacts.phone) = 1
  )
  </code></pre>
  
  <h2>Update:</h2>
  
  <p>Thank you for your replies, I have provided my solution that fits my case perfectly.</p>