I understand the logic behind listing #3, but I was surprised to see, and uncertain on how it works, that it is not necessary to use another GROUP BY clause in the subquery:
SELECT Outer_H.[CustomerID]
, SUM(Outer_H.[SubTotal]) AS TotalPurchase
, SUM(Outer_H.[SubTotal]) * .10 AS Rebate
FROM [Sales].[SalesOrderHeader] AS Outer_H
WHERE YEAR(Outer_H.[OrderDate]) = '2008'
GROUP BY Outer_H.[CustomerID]
HAVING (SELECT SUM(Inner_H.[SubTotal]) FROM [Sales].[SalesOrderHeader] AS Inner_H
WHERE Inner_H.[CustomerID] = Outer_H.[CustomerID]
AND YEAR(Inner_H.[OrderDate]) = '2008' GROUP BY CLAUSE NOT REQUIRED HERE) > 150000
ORDER BY Rebate DESC;
How is SQL able to execute the aggregate SUM() function in the inner query w/out a GROUP BY clause applied? Does it automatically apply the GROUP BY clause from the outer query to the inner query?
Thanks.