SQL Query Processing

  • Is this the correct way that a query is processed?

    from

    where

    group by

    having

    select

    order by

    into

    If this is correct, what is the explanation of a query like

    select col1,count(*)

    from #table

    where date>=getdate()

    group by col1

    having count(*)>2

    if the having is processed before the select - how does it know how the count(*) is being aggregated?

  • The having clause is evaluated after the group by clause. A group by is required to use a having. It is unassociated with the select portion of the statement. You don't have to select count(*) to use having count(*) > x. The having is essentially a where clause on the grouping, and the select is returning what you want to the screen. One does not rely on the other.

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

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