0% found this document useful (0 votes)
76 views2 pages

MySql Having Clause

The document discusses the MySQL HAVING clause. The HAVING clause is used with the GROUP BY clause and returns rows where the HAVING condition is true. It allows filtering of the result of aggregate functions like SUM, COUNT, MIN, MAX, and AVG. The syntax includes the SELECT statement with aggregate functions, FROM, optional WHERE, GROUP BY, and mandatory HAVING clauses. An example uses the SUM function with HAVING to return names and total marks from the studentmarks table where the sum of marks is greater than 90 for the 'ece' branch.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
76 views2 pages

MySql Having Clause

The document discusses the MySQL HAVING clause. The HAVING clause is used with the GROUP BY clause and returns rows where the HAVING condition is true. It allows filtering of the result of aggregate functions like SUM, COUNT, MIN, MAX, and AVG. The syntax includes the SELECT statement with aggregate functions, FROM, optional WHERE, GROUP BY, and mandatory HAVING clauses. An example uses the SUM function with HAVING to return names and total marks from the studentmarks table where the sum of marks is greater than 90 for the 'ece' branch.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

MySql Having Clause

Contents
CONTENTS...................................................................................................................................................................... 1
MYSQL HAVING CLAUSE................................................................................................................................................. 2
SYNTAX:.....................................................................................................................................................................................2
HAVING CLAUSE WITH SUM FUNCTION:.......................................................................................................................................2

www.kenisys.com 1
MySql Having Clause

MySQL HAVING Clause


MySQL HAVING Clause is used with GROUP BY clause. It always returns the rows where condition is
TRUE.

Syntax:
SELECT expression1, expression2, ... expression_n,
aggregate_function (expression)
FROM tables
[WHERE conditions]
GROUP BY expression1, expression2, ... expression_n
HAVING condition;

Parameters
aggregate_function: It specifies any one of the aggregate function such as SUM, COUNT, MIN, MAX,
or AVG.

expression1, expression2, ... expression_n: It specifies the expressions that are not encapsulated
within an aggregate function and must be included in the GROUP BY clause.

WHERE conditions: It is optional. It specifies the conditions for the records to be selected.

HAVING condition: It is used to restrict the groups of returned rows. It shows only those groups in
result set whose conditions are TRUE.

HAVING Clause with SUM function:


Here, we use the SUM function with the HAVING Clause to return the name and sum of their working
hours.

Execute the following query:


SELECT name, SUM(marks) AS 'Total_marks'
FROM studentmarks
where branch='ece'
GROUP BY name
HAVING SUM(marks) > 90;

Output:
Total_mark
name
s
mounika 101

Simply, it can also be used with COUNT, MIN, MAX and AVG functions.

www.kenisys.com 2

You might also like