title: SQL: list users with their phone numbers and email addresses
body: <p>I want to make a query in PostgreSQL which lists my users and their email addresses and phone numbers (separated by commas), like this:</p>
  
  <pre><code>| user1 | email1@mail.com, email2@mail.com | +3612123123, +3623234234 |
  </code></pre>
  
  <p>The tables are:</p>
  
  <pre><code>user - stores the user's data
  
  user_email - stores the user's email addresses
  
  user_phone - stores the user's phone numbers
  </code></pre>
  
  <p>I tried the obvius:</p>
  
  <pre><code>SELECT user.id, user.name
  (
    SELECT array_agg(user_email.email)
    FROM user_email
    WHERE user_email.user_id = user.id
  ) AS EmailAddresses,
  (
    SELECT array_agg(user_phone.phone)
    FROM user_phone
    WHERE user_phone.user_id = user.id
  ) AS PhoneNumbers
  FROM user
  ORDER BY user.id
  </code></pre>
  
  <p>But this lead to ridiculous query times (34sec).
  Than I tried:</p>
  
  <pre><code>SELECT user.id, user.name, array_agg(user_email.email), array_agg(user_phone.phone)
  FROM user
  LEFT JOIN user_email ON user_email.user_id = user.id
  LEFT JOIN user_phone ON user_phone.user_id = user.id
  GROUP BY user.id
  ORDER BY user.id
  </code></pre>
  
  <p>This results very good query times (around 100ms).
  But this way it's lists every combination of phone numbers and email addresses. So if I have 3 email address and 3 phone number, then it lists 9 email address (triples every single address) and 9 phone numbers too.</p>
  
  <p>Is there an efficient way to do what I want?</p>