0% found this document useful (0 votes)
36 views9 pages

Having Clause

The document provides an overview of the HAVING clause in SQL, which is used to filter grouped rows after a GROUP BY clause. It explains the syntax, order of execution, and differences between the HAVING and WHERE clauses, emphasizing that HAVING can utilize aggregate functions. Additionally, it includes example queries demonstrating the practical application of the HAVING clause in filtering data based on group conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views9 pages

Having Clause

The document provides an overview of the HAVING clause in SQL, which is used to filter grouped rows after a GROUP BY clause. It explains the syntax, order of execution, and differences between the HAVING and WHERE clauses, emphasizing that HAVING can utilize aggregate functions. Additionally, it includes example queries demonstrating the practical application of the HAVING clause in filtering data based on group conditions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

HAVING CLAUSE

CONTENTS

• Introduction
• Syntax and Order of execution of having clause
• Why/When we are using having clause
• Difference between where and having clause
• Queries of Having clause
INTRODUCTION

Def: Having Clause is used to Filter the Group

• Having clause execute group by group.


• Having clause always use after group by clause.
• In Having clause, we write group filter condition.
• Having clause cannot we used without the group by clause.
• In Having clause, we can use multi row function(MRF) as condition.
SYNTAX AND ORDER OF EXECUTION

• Syntax:
SELECT group_by_expression / group_function

FROM table_name
[WHERE <filter_condition>]
GROUP BY column_name/expression
HAVING <group_filter_condition
Order by column_name / group_function asc/desc;
ORDER OF EXECUTION

1. FROM
2. WHERE(if used) [ROW-BY-ROW]
3. GROUP BY(if used) [ROW-BY-ROW]
4. HAVING (if used ) [GROUP-BY-GROUP]
5. SELECT [GROUP-BY-GROUP]
6. ORDER BY(if used) [ROW-BY-ROW]
WHY / WHEN WE ARE USING HAVING CLAUSE

• The SQL HAVING clause is similar to the WHERE clause; both are used to filter rows in
a table based on condition . However, the HAVING clause is used to filter grouped rows
instead of single rows. These rows are grouped together by the GROUP BY clause, so,
the HAVING clause must always be followed by the GROUP BY clause.
• Moreover, the HAVING clause can be used with aggregate functions such as COUNT(),
SUM(), AVG(), etc., whereas the WHERE clause cannot be used with them.
If suppose:
Write a query the total salary, average salary of all employees with average salary is greater then 60000 in
each department.
1. select sum(sal), avg(sal)
Don’t write this format Boz
from emp
where clause is not applicable
where avg(sal)>60000 to use Aggregation functions
group by dept-id;

2. select sum(sal),avg(sal)
from emp
group by dept-id
having avg(sal)>60000;
DIFFERENCE B/W WHERE AND HAVING CLAUSE

Where clause Having clause


1.Where clause is used to filter individual 1.Having clause is used to filter groups of
rows. rows.
2.Where clause cannot contain aggregate 2.Having clause can contain aggregate
functions. functions.
3.Where clause is used before group by 3. Having clause is used after group by
clause. clause.
4. Where clause can be used with or without 4. Having clause can be used with group by
group by clause. clause.
5. Where clause is used with single row 5. Having clause is used with multiple row
function like Upper, Lower etc.. function like sum, count etc..
QUERIES OF HAVING CLAUSE

• Write a query the dep-id, total salary, average salary of all employees with average salary
is greater then 60000 in each department and order by dep-id in desc order.
select dep-id, sum(sal), avg(sal)
from emp
group by dep-id
having avg(sal)>60000
order by dep-id desc;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy