SQL Having Keyword

Table Of Contents:

  1. What Is SQL having Keyword?
  2. Syntax Of SQL Having?
  3. Examples Of SQL Having.

(1) What Is SQL Having Keyword?

  • SQL HAVING clause is similar to the WHERE clause; they are both used to filter rows in a table based on conditions.
  • However, the HAVING clause was included in SQL 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.
  • It can be used with aggregate functions, whereas the WHERE clause cannot.

(2) Syntax Of SQL Having Keyword?

SELECT column1, column2, aggregate_function(column)
FROM table_name
GROUP BY column1, column2
HAVING condition;

(3) Examples Of SQL Having Keyword?

Example-1

SELECT NAME, SUM(SALARY) as total_salary
FROM CUSTOMERS
GROUP BY NAME
HAVING SUM(SALARY) < 4540
ORDER BY NAME

Example-2

SELECT AGE
FROM CUSTOMERS
GROUP BY age
HAVING COUNT(age) >= 3

Leave a Reply

Your email address will not be published. Required fields are marked *