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.
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.
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.