Having clause in SQL

 Having clause:

   Ø Having clause is used to filter the group of rows returned in the GROUP BY clause.

   Ø Mainly, Aggregate functions are not allowed in the where condition so SQL prefers HAVING CLAUSE for filtering.

Syntax:

  1. SELECT clm_List

    FROM Table_Name

    Where <Condition>

    [GROUP BY clm]

    HAVING <Condition>

    [ORDER BY clm];

Example:

 Table: Books

Book_Name

Order_Date

Orders

SQL

01-Dec-2000

2

DBMS

05-Sep-2000

3

PL/SQL

06-Oct-2000

4

SQL

09-Sep-2000

3

SQL

06-Oct-2001

2

DBMS

22-Jun-2001

3

PL/SQL

25-Jan-2002

3

SQL

20-jul-2002

4

DBMS

09-Nov-2003

4

  Having clause:
  1. SELECT Book_name, sum(Orders)

    FROM Books

    GROUP BY Book_Name

    HAVING sum (orders) > 5

    ORDER BY orders desc;

Output:

Book_Name

Sum(Orders)

PL/SQL

7

DBMS

10

SQL

11

 

 

 

Share:

No comments:

Post a Comment

Popular Posts

Search This Blog

Powered by Blogger.

Blog Archive

Service Support

Need our help to Learn or Post New Concepts Contact me

Blog Archive

Recent Posts

Service Support

Need our help to Learn or Post New Concepts Contact me