title: PostgreSQL - How to remove duplicates when doing LEFT OUTER JOIN with WHERE clause?
body: <p>I have 2 tables:</p>
  
  <pre><code>users table
  +--------+---------+
  | id     | integer |
  +--------+---------+
  | phone  | string  |
  +--------+---------+
  | active | boolean |
  +--------+---------+
  
  statuses table
  +---------+---------+
  | id      | integer |
  +---------+---------+
  | user_id | integer |
  +---------+---------+
  | step_1  | boolean |
  +---------+---------+
  | step_2  | boolean |
  +---------+---------+
  </code></pre>
  
  <p>I'm doing <code>LEFT OUTER JOIN</code> <code>statuses</code> table on <code>users</code> table with <code>WHERE</code> clause like this:</p>
  
  <pre><code>SELECT users.id, statuses.step_1, statuses.step_2
  FROM users 
  LEFT OUTER JOIN statuses ON users.id = statuses.user_id
  WHERE (users.active='f')
  ORDER BY users.id DESC
  </code></pre>
  
  <p><strong>My problem</strong></p>
  
  <p>There are some users that have same phone number inside the <code>users</code> table and I want remove the duplicate users based on the phone number.</p>
  
  <p>I don't want to delete them from database. But just want to exclude them for this query only.</p>
  
  <p>For example, say John (ID: 1) and Sara (ID: 2) shared same phone number (+6012-3456789), removing one of them, either John or Sara is fine for me.</p>
  
  <p><strong>What I've tried but did not work?</strong></p>
  
  <p>First:</p>
  
  <pre><code>SELECT DISTINCT users.phone
  FROM users 
  LEFT OUTER JOIN statuses ON users.id = statuses.user_id
  WHERE (users.active='f')
  ORDER BY users.id DESC
  </code></pre>
  
  <p>Second: </p>
  
  <pre><code>SELECT users.phone, COUNT(*)
  FROM users
  LEFT OUTER JOIN statuses ON users.id = statuses.user_id    
  WHERE (users.active='f')
  GROUP BY phone
  HAVING COUNT(users.phone) &gt; 1
  </code></pre>