title: SQL Aggregate Function Nesting
body: <p>I am learning SQL with PostgreSQL, and have run into a problem regarding nested Aggregate functions. </p>
  
  <p>I am trying to find the details of private owners with the maximum amount of properties, where I have two relations, privateowner and propertyforrent with propertyforrent having a foreign key, ownwerno.</p>
  
  <p>I suspect that my issue is where I am attempting to nest Aggregate Functions, but I cannot see a way around it.</p>
  
  <p>NOTE:- the database I am using has a typo in the attribute ownwerno in propertyforrent, where it should be ownerno.</p>
  
  <p>The code I am attempting to use is shown below~:-</p>
  
  <pre><code>SELECT o.fname, o.lname, telno
  FROM privateowner o
  WHERE o.ownerno = (SELECT p.ownwerno
             FROM propertyforrent p
             HAVING COUNT(p.ownwerno) = MAX(COUNT(o.ownerno)));
  </code></pre>
  
  <p>Its accompanying error is as follows:-</p>
  
  <pre><code>ERROR:  column "p.ownwerno" must appear in the GROUP BY clause or be used in a
  aggregate function
  LINE 3: WHERE o.ownerno = (SELECT p.ownwerno
                                    ^
  
  
  ********** Error **********
  
  ERROR: column "p.ownwerno" must appear in the GROUP BY clause or be used in an
  aggregate function
  SQL state: 42803
  Character: 78
  </code></pre>
  
  <p>Any insight would be wonderful.</p>