SlideShare a Scribd company logo
1 of 16
MySQL HAVING
Clause
The MySQL HAVING Clause
In MySQL, the HAVING clause is used in conjunction with the GROUP BY clause to
filter the results of a query based on the aggregated values of grouped rows. It is
similar to the WHERE clause, but it is specifically designed to work with aggregate
functions such as SUM, COUNT, AVG, etc.
The HAVING clause was added to SQL because the WHERE keyword cannot be
used with aggregate functions.
HAVING Syntax
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);
MySQL HAVING Examples
The following SQL statement lists the number of customers in each country. Only
include countries with more than 5 customers:
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;
The following SQL statement lists the number of customers in each
country, sorted high to low (Only include countries with more than 5
customers):
Example
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5
ORDER BY COUNT(CustomerID) DESC;
Below is a selection from the "Orders" table
And a selection from the "Employees" table:
More HAVING Examples
The following SQL statement lists the employees that have registered
more than 10 orders:
Example
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders
FROM (Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID)
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 10;
The following SQL statement lists if the employees "Davolio" or "Fuller"
have registered more than 25 orders:
Example
SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders
FROM Orders
INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID
WHERE LastName = 'Davolio' OR LastName = 'Fuller'
GROUP BY LastName
HAVING COUNT(Orders.OrderID) > 25;
The MySQL EXISTS Operator
In MySQL, the EXISTS operator is used in conjunction with a subquery to test
whether the result of the subquery is not empty (i.e., it returns at least one row). The
EXISTS operator is often used with correlated subqueries, where the subquery
refers to a column from the outer query.
The EXISTS operator is used to test for the existence of any record in a subquery.
The EXISTS operator returns TRUE if the subquery returns one or more records.
EXISTS Syntax
SELECT column_name(s)
FROM table_name
WHERE EXISTS
(SELECT column_name FROM table_name WHERE condition);
Below is a selection from the "Products" table
And a selection from the "Suppliers" table:
MySQL EXISTS Examples
The following SQL statement returns TRUE and lists the suppliers with a product
price less than 20:
Example
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.Suppl
ierID = Suppliers.supplierID AND Price < 20);
The following SQL statement returns TRUE and lists the suppliers with
a product price equal to 22:
Example
SELECT SupplierName
FROM Suppliers
WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.Suppl
ierID = Suppliers.supplierID AND Price = 22);
The MySQL ANY and ALL Operators
The ANY and ALL operators in MySQL are used in conjunction with subqueries and
comparison operators to compare a value to a set of values returned by a subquery.
These operators are typically used in the context of a WHERE or HAVING clause.
The ANY and ALL operators allow you to perform a comparison between a single
column value and a range of other values.
The ANY Operator
The ANY and ALL operators in MySQL are used in conjunction with subqueries and
comparison operators to compare a value to a set of values returned by a subquery.
These operators are typically used in the context of a WHERE or HAVING clause.
The ANY and ALL operators allow you to perform a comparison between a single
column value and a range of other values.

More Related Content

Similar to ADV Powepoint 6 Lec.pptx

retrieving data using SQL statements
retrieving data using SQL statementsretrieving data using SQL statements
retrieving data using SQL statements
Arun Nair
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
Belle Wx
 

Similar to ADV Powepoint 6 Lec.pptx (20)

Module03
Module03Module03
Module03
 
Sql clauses by Manan Pasricha
Sql clauses by Manan PasrichaSql clauses by Manan Pasricha
Sql clauses by Manan Pasricha
 
Nested queries in database
Nested queries in databaseNested queries in database
Nested queries in database
 
Les06
Les06Les06
Les06
 
Advanced Sql Training
Advanced Sql TrainingAdvanced Sql Training
Advanced Sql Training
 
Data Manipulation Language.pptx
Data Manipulation Language.pptxData Manipulation Language.pptx
Data Manipulation Language.pptx
 
Module 3.1.pptx
Module 3.1.pptxModule 3.1.pptx
Module 3.1.pptx
 
5. Group Functions
5. Group Functions5. Group Functions
5. Group Functions
 
Structured query language(sql)
Structured query language(sql)Structured query language(sql)
Structured query language(sql)
 
Sql
SqlSql
Sql
 
Sql functions
Sql functionsSql functions
Sql functions
 
Sql ch 5
Sql ch 5Sql ch 5
Sql ch 5
 
Ejb5
Ejb5Ejb5
Ejb5
 
Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2Bt0075 rdbms with mysql 2
Bt0075 rdbms with mysql 2
 
Oracle SQL Part 3
Oracle SQL Part 3Oracle SQL Part 3
Oracle SQL Part 3
 
retrieving data using SQL statements
retrieving data using SQL statementsretrieving data using SQL statements
retrieving data using SQL statements
 
Creating database using sql commands
Creating database using sql commandsCreating database using sql commands
Creating database using sql commands
 
SQL report
SQL reportSQL report
SQL report
 
Review of SQL
Review of SQLReview of SQL
Review of SQL
 
SQL.pptx
SQL.pptxSQL.pptx
SQL.pptx
 

More from ArjayBalberan1

MYSQL DATABASE MYSQL DATABASE MYSQL DATABASECSS.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASECSS.pptxMYSQL DATABASE MYSQL DATABASE MYSQL DATABASECSS.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASECSS.pptx
ArjayBalberan1
 
MYSQL DATABASE MYSQL DATABASE merge-sort-grp4.pptx
MYSQL DATABASE MYSQL DATABASE merge-sort-grp4.pptxMYSQL DATABASE MYSQL DATABASE merge-sort-grp4.pptx
MYSQL DATABASE MYSQL DATABASE merge-sort-grp4.pptx
ArjayBalberan1
 
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptxMYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
ArjayBalberan1
 

More from ArjayBalberan1 (20)

MYSQL DATABASE MYSQL DATABASE MYSQL DATABASECSS.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASECSS.pptxMYSQL DATABASE MYSQL DATABASE MYSQL DATABASECSS.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASECSS.pptx
 
Introduction to Algorithms Introduction to Algorithms.pptx
Introduction to Algorithms Introduction to Algorithms.pptxIntroduction to Algorithms Introduction to Algorithms.pptx
Introduction to Algorithms Introduction to Algorithms.pptx
 
MYSQL DATABASE MYSQL DATABASEGroup-1.pptx
MYSQL DATABASE MYSQL DATABASEGroup-1.pptxMYSQL DATABASE MYSQL DATABASEGroup-1.pptx
MYSQL DATABASE MYSQL DATABASEGroup-1.pptx
 
Appdev appdev appdev app devAPPDEV 1.2.pptx
Appdev appdev appdev app devAPPDEV 1.2.pptxAppdev appdev appdev app devAPPDEV 1.2.pptx
Appdev appdev appdev app devAPPDEV 1.2.pptx
 
Rizals-Family-Childhood-Early-Education.pptx
Rizals-Family-Childhood-Early-Education.pptxRizals-Family-Childhood-Early-Education.pptx
Rizals-Family-Childhood-Early-Education.pptx
 
MYSQL DATABASE Operating System Part2 (1).pptx
MYSQL DATABASE Operating System Part2 (1).pptxMYSQL DATABASE Operating System Part2 (1).pptx
MYSQL DATABASE Operating System Part2 (1).pptx
 
MYSQL DATABASE APP DEV POWERPOINT 1.pptx
MYSQL DATABASE APP DEV POWERPOINT 1.pptxMYSQL DATABASE APP DEV POWERPOINT 1.pptx
MYSQL DATABASE APP DEV POWERPOINT 1.pptx
 
MYSQL DATABASE INTRODUCTION TO JAVASCRIPT.pptx
MYSQL DATABASE INTRODUCTION TO JAVASCRIPT.pptxMYSQL DATABASE INTRODUCTION TO JAVASCRIPT.pptx
MYSQL DATABASE INTRODUCTION TO JAVASCRIPT.pptx
 
NETWORK-TOPOLOGIES-VARIOUS-TOPOLOGIES.pptx
NETWORK-TOPOLOGIES-VARIOUS-TOPOLOGIES.pptxNETWORK-TOPOLOGIES-VARIOUS-TOPOLOGIES.pptx
NETWORK-TOPOLOGIES-VARIOUS-TOPOLOGIES.pptx
 
MYSQL DATABASE MYSQL DATABASEINSERTION-SORT.pptx
MYSQL DATABASE MYSQL DATABASEINSERTION-SORT.pptxMYSQL DATABASE MYSQL DATABASEINSERTION-SORT.pptx
MYSQL DATABASE MYSQL DATABASEINSERTION-SORT.pptx
 
MYSQL DATABASE MYSQL DATABASE merge-sort-grp4.pptx
MYSQL DATABASE MYSQL DATABASE merge-sort-grp4.pptxMYSQL DATABASE MYSQL DATABASE merge-sort-grp4.pptx
MYSQL DATABASE MYSQL DATABASE merge-sort-grp4.pptx
 
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptxMYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
MYSQL DATABASE MYSQL DATABASE MYSQL DATABASE BUBLESORT.pptx
 
MYSQL DATABASE MYSQL DATABASEquick-sort.pptx
MYSQL DATABASE MYSQL DATABASEquick-sort.pptxMYSQL DATABASE MYSQL DATABASEquick-sort.pptx
MYSQL DATABASE MYSQL DATABASEquick-sort.pptx
 
Selection-sort-in-algorithm and complexity.pptx
Selection-sort-in-algorithm and complexity.pptxSelection-sort-in-algorithm and complexity.pptx
Selection-sort-in-algorithm and complexity.pptx
 
Week 7 Github - SI- Architecture.pptx
Week 7 Github - SI-  Architecture.pptxWeek 7 Github - SI-  Architecture.pptx
Week 7 Github - SI- Architecture.pptx
 
HTML-Lab.pptx
HTML-Lab.pptxHTML-Lab.pptx
HTML-Lab.pptx
 
tableslist.pptx
tableslist.pptxtableslist.pptx
tableslist.pptx
 
Week Topic Code Access vs Event Based.pptx
Week Topic Code Access vs Event Based.pptxWeek Topic Code Access vs Event Based.pptx
Week Topic Code Access vs Event Based.pptx
 
MODULE-01.pptx
MODULE-01.pptxMODULE-01.pptx
MODULE-01.pptx
 
APP DEV POWERPOINT 2.pptx
APP DEV POWERPOINT 2.pptxAPP DEV POWERPOINT 2.pptx
APP DEV POWERPOINT 2.pptx
 

Recently uploaded

Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 

Recently uploaded (20)

Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Cyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdfCyberprint. Dark Pink Apt Group [EN].pdf
Cyberprint. Dark Pink Apt Group [EN].pdf
 
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUKSpring Boot vs Quarkus the ultimate battle - DevoxxUK
Spring Boot vs Quarkus the ultimate battle - DevoxxUK
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 

ADV Powepoint 6 Lec.pptx

  • 2. The MySQL HAVING Clause In MySQL, the HAVING clause is used in conjunction with the GROUP BY clause to filter the results of a query based on the aggregated values of grouped rows. It is similar to the WHERE clause, but it is specifically designed to work with aggregate functions such as SUM, COUNT, AVG, etc. The HAVING clause was added to SQL because the WHERE keyword cannot be used with aggregate functions.
  • 3. HAVING Syntax SELECT column_name(s) FROM table_name WHERE condition GROUP BY column_name(s) HAVING condition ORDER BY column_name(s);
  • 4.
  • 5. MySQL HAVING Examples The following SQL statement lists the number of customers in each country. Only include countries with more than 5 customers: Example SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5;
  • 6. The following SQL statement lists the number of customers in each country, sorted high to low (Only include countries with more than 5 customers): Example SELECT COUNT(CustomerID), Country FROM Customers GROUP BY Country HAVING COUNT(CustomerID) > 5 ORDER BY COUNT(CustomerID) DESC;
  • 7. Below is a selection from the "Orders" table And a selection from the "Employees" table:
  • 8. More HAVING Examples The following SQL statement lists the employees that have registered more than 10 orders: Example SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM (Orders INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID) GROUP BY LastName HAVING COUNT(Orders.OrderID) > 10;
  • 9. The following SQL statement lists if the employees "Davolio" or "Fuller" have registered more than 25 orders: Example SELECT Employees.LastName, COUNT(Orders.OrderID) AS NumberOfOrders FROM Orders INNER JOIN Employees ON Orders.EmployeeID = Employees.EmployeeID WHERE LastName = 'Davolio' OR LastName = 'Fuller' GROUP BY LastName HAVING COUNT(Orders.OrderID) > 25;
  • 10. The MySQL EXISTS Operator In MySQL, the EXISTS operator is used in conjunction with a subquery to test whether the result of the subquery is not empty (i.e., it returns at least one row). The EXISTS operator is often used with correlated subqueries, where the subquery refers to a column from the outer query. The EXISTS operator is used to test for the existence of any record in a subquery. The EXISTS operator returns TRUE if the subquery returns one or more records.
  • 11. EXISTS Syntax SELECT column_name(s) FROM table_name WHERE EXISTS (SELECT column_name FROM table_name WHERE condition);
  • 12. Below is a selection from the "Products" table And a selection from the "Suppliers" table:
  • 13. MySQL EXISTS Examples The following SQL statement returns TRUE and lists the suppliers with a product price less than 20: Example SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.Suppl ierID = Suppliers.supplierID AND Price < 20);
  • 14. The following SQL statement returns TRUE and lists the suppliers with a product price equal to 22: Example SELECT SupplierName FROM Suppliers WHERE EXISTS (SELECT ProductName FROM Products WHERE Products.Suppl ierID = Suppliers.supplierID AND Price = 22);
  • 15. The MySQL ANY and ALL Operators The ANY and ALL operators in MySQL are used in conjunction with subqueries and comparison operators to compare a value to a set of values returned by a subquery. These operators are typically used in the context of a WHERE or HAVING clause. The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values.
  • 16. The ANY Operator The ANY and ALL operators in MySQL are used in conjunction with subqueries and comparison operators to compare a value to a set of values returned by a subquery. These operators are typically used in the context of a WHERE or HAVING clause. The ANY and ALL operators allow you to perform a comparison between a single column value and a range of other values.