title: SQL select all fields from one table along with the sum of all values from another table matching a given condition
body: <p>I have two tables: <code>customers</code> and <code>orders</code>. </p>
  
  <p>Both <code>id</code> and <code>email</code> are unique in <code>customers</code> whereas <code>uid</code> and <code>price</code> can have duplicates in <code>orders</code>.</p>
  
  <p>I want to select all fields from <code>customers</code> where email = 'john@doe.com', along with the total price this user paid from <code>orders</code>. If john doe didn't order anything yet (i.e. there is 0 record in <code>orders</code> where <code>uid</code> matches user <code>id</code>), I still need to return the fields list for this user, but the sum of the prices paid must be zero.</p>
  
  <p>Here is what I tried:</p>
  
  <pre><code>SELECT t1.id, t1.email, 
         t2.uid, (SELECT SUM(t2.price)) AS total_price
  FROM customers t1, orders t2
  WHERE t1.email = '$email' AND t2.uid = t1.id
  </code></pre>
  
  <p>Problem is this query only works if there is at least one record in <code>orders</code> because the check for <code>t2.uid = t1.id</code> is in the <code>where</code> clause.</p>
  
  <p>If there is no records in <code>orders</code> having the same uid as the customer id, the query will return NULL, which is not what I want.</p>
  
  <p>How can I <strong>always</strong> return all fields from <code>customers</code> along with the sum of all prices paid in <code>orders</code> even if there is no records in <code>orders</code> matching <code>customers.id</code> ?</p>
  
  <p>Here is an example showing the desired output:</p>
  
  <p><a href="https://i.stack.imgur.com/sQkiq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sQkiq.png" alt="enter image description here"></a></p>