title: PostgreSQL SELECT null values from two tables
body: <p>I have problem to solve.</p>
  
  <p>I have two tables, institutions and documents, they joins throught documents owner_id and institution id:</p>
  
  <p>institution</p>
  
  <pre><code> id | name
  ----+-----
    1 | a
    2 | b
    3 | c
    4 | d
  </code></pre>
  
  <p>and </p>
  
  <p>documents</p>
  
  <pre><code>   id | owner | value
    ----+-------+------
      1 |     1 | xxx
      2 |     1 | yyy
      3 |     1 | yyy
      4 |     3 | xxx
      5 |     3 | xxx
      6 |     4 | yyy
  </code></pre>
  
  <p>And I need to count, how many values has each name, this kind of result:</p>
  
  <pre><code> name | count(total) | count(xxx) | count(yyy)
  ------+--------------+------------+------------
   a    |            3 |          1 |          2
   b    |            0 |          0 |          0
   c    |            2 |          2 |          0
   d    |            1 |          0 |          1
  </code></pre>
  
  <p>I have tried this query:</p>
  
  <pre><code>SELECT 
      a.name,
      (a.xxx + b.yyy) as total,
      a.xxx,
      b.yyy
  FROM
      (SELECT count(documents.id) as xxx,
          institution.name
      FROM 
          documents, institution
      WHERE 
          documents.owner_id = institution.id and 
          documents.value = 'xxx'
      GROUP BY 
          institution.name) as a,
      (SELECT count(documents.id) as yyy,
          institution.name
      FROM 
          documents, 
          institution
      WHERE 
          documents.owner_id = institution.id and 
          documents.value = 'yyy'
      GROUP BY 
          institution.name) as b
  WHERE 
  a.name = b.name
  ORDER BY 
  a.name
  </code></pre>
  
  <p>But it returns only those rows, where xxx and yyy is not epmty, this kind of:</p>
  
  <pre><code> name | count(total) | count(xxx) | count(yyy)
  ------+--------------+------------+------------
   a    |            3 |          1 |          2
  </code></pre>
  
  <p>I'm missing these rows:</p>
  
  <pre><code> name | count(total) | count(xxx) | count(yyy)
  ------+--------------+------------+------------
   b    |            0 |          0 |          0
   c    |            2 |          2 |          0
   d    |            1 |          0 |          1
  </code></pre>
  
  <p>because it contains empty data. 
  Thanks in advance.</p>
  
  <p>Julius</p>