• Agree with the others; listings 5 and 6 have been switched. Which led to a lot of confusion, as I'm looking at this:

    To demonstrate using a subquery in a function call, suppose that you have the requirement to display the number of days between the OrderDate and the maximum OrderDate for each Sales.SalesOrderHeader records. The code in Listing 6 meets this requirement.

    SELECT count(*), OrderDate

    FROM [Sales].[SalesOrderHeader]

    GROUP BY OrderDate

    HAVING count(*) >

    (SELECT count(*)

    FROM [Sales].[SalesOrderHeader]

    WHERE OrderDate = '2006-05-01 00:00:00.000');

    ...and thinking... "Wait. That makes no sense at all. Where does it grab any number of days? It looks like this... grabs all the order dates and the number of orders on those dates, where the number of orders is greater than the number of orders on 5/1/2006. Which isn't even remotely what it says it's supposed to do..."

    If I'd been a total newbie, this would have been misleading. As it was, it was just confusing.