title: MySQL Join 3 Tables with Count
body: <p>I have the following tables:</p>
  
  <p><strong>users</strong></p>
  
  <ul>
  <li>id</li>
  <li>username</li>
  <li>first_name</li>
  <li>email</li>
  </ul>
  
  <p><strong>students</strong></p>
  
  <ul>
  <li>id</li>
  <li>user_id</li>
  </ul>
  
  <p><strong>payments</strong></p>
  
  <ul>
  <li>id</li>
  <li>student_id</li>
  <li>is_paid</li>
  <li>date</li>
  </ul>
  
  <p>And I have the following query:</p>
  
  <pre><code>SELECT users.username, users.first_name, users.email,
      COUNT(payments.id) as pending_payments
  FROM users
  LEFT JOIN students ON users.id = students.user_id 
  LEFT JOIN payments ON payments.student_id = students.id AND payments.is_paid = 0
  WHERE users.username LIKE 'testuser';
  </code></pre>
  
  <p>When the username exists and there are not pending payments, it returns:</p>
  
  <pre><code>| username | first_name | email                | pending_payments |
  
  | testuser | test       | testuser@example.com |        0         |
  </code></pre>
  
  <p>When the username exists and there are 2 pending payments, it returns:</p>
  
  <pre><code>| username | first_name | email                 | pending_payments |
  
  | testuser2| test2      | testuser@2example.com |        2         |
  </code></pre>
  
  <p>But when the username does not exists, it returns:</p>
  
  <pre><code>| username | first_name | email              | pending_payments   |
  
  | NULL     | NULL       | NULL               |         0          |
  </code></pre>
  
  <p>Instead, when the username does not exists, I want as result:</p>
  
  <p>Empty set.</p>
  
  <p>How can I modify my query to get the expected results?
  (keep the behavior when user exists, but returns Empty set when does not exists, instead of a row will NULL values).</p>
  
  <p>See SQLfiddle: <a href="http://sqlfiddle.com/#!9/aab424/11" rel="nofollow noreferrer">http://sqlfiddle.com/#!9/aab424/11</a></p>
  
  <p><strong>EDIT</strong>
  <strong>A working solution was provided</strong>:
  <a href="http://sqlfiddle.com/#!9/aab424/72" rel="nofollow noreferrer">http://sqlfiddle.com/#!9/aab424/72</a></p>