Thursday, January 27, 2011

HAVING CLAUSE

    Having clause is used to restrict group-level rows. It can not be used without GROUP BY clause to be used.

    Example:

    clip_image001

    We can use WHERE and HAVING clause in the same statement in case we want to do double restriction with the following logic:

    1. WHERE clause will apply some restrict condition for each row in the table
    2. GROUP BY clause will create groups only from the rows satisfying the where condition
    3. HAVING clause will apply restrict condition on the groups produced by GROUP BY clause.

    For example:

    clip_image002

    clip_image003

    It is important to remember that HAVING clause must contain a group function or expression that uses such, otherwise we will get an error, for example:

    clip_image004

No comments:

Post a Comment