title: What does the GROUP BY phase in logical query processing in SQL return?
body: <p>In the book Querying MS SQL Server 2012 (Training Kit) for exam 70-461 it says about <code>GROUP BY</code> phase of a query in logical processing :</p>
  
  <blockquote>
    <p><strong>The final result of this query has one row representing each group</strong>
    (unless filtered out). Therefore, expressions in all phases that take
    place after the current grouping phase are somewhat limited. All
    expressions processed in subsequent phases must guarantee a single
    value per group. If you refer to an element from the GROUP BY list
    (for example, country), you already have such a guarantee, so such a
    reference is allowed. However, if you want to refer to an element that
    is not part of your GROUP BY list (for example, empid), it must be
    contained within an aggregate function like MAX or SUM. That’s because
    multiple values are possible in the element within a single group, and
    the only way to guarantee that just one will be returned is to
    aggregate the values.</p>
  </blockquote>
  
  <p>The author then mentions the <code>HAVING</code> step where he uses <code>COUNT(*) &gt; 1</code>. My question is if the <code>GROUP BY</code> only has a result of 1 row per group, how is the <code>HAVING</code> phase using that single group row to filter out any groups that have more than 1 row... which it does as half of them remain? So am I missing something here. Is there some sort of hidden COUNT column attached to each group? </p>
  
  <p>The query is:</p>
  
  <pre><code>SELECT country, YEAR(hiredate) AS yearhired, COUNT(*) AS numemployees
  FROM HR.Employees
  WHERE hiredate &gt;= '20030101'
  GROUP BY country, YEAR(hiredate)
  HAVING COUNT(*) &gt; 1
  ORDER BY country , yearhired DESC;
  </code></pre>
  
  <p>Please englighten.</p>