Query options for counting

  • Hi.

    Is there a processing difference between these two queries? It looks like the first example is doing the count twice and the second query is doing the count once and then displaying the people with more than one order. On a large table is one way more efficient than the other.

    select name, count(orderID) as Num

    from orders

    group by name

    having count(orderID) > 1

    order by name

    or

    ;with cte as

    (select name, count(orderID) as Num

    from orders

    group by name)

    select * from cte

    where num > 1

    order by name

  • Execution plans look identical. SQL Server must be deciding the best way to handle it.

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply