0% found this document useful (0 votes)
10 views3 pages

Python Tutorial

The SQL HAVING clause is used to filter grouped data after applying the GROUP BY clause, often in conjunction with aggregate functions like SUM(), COUNT(), or AVG(). It allows for displaying only those groups that meet specific conditions, as demonstrated in examples calculating total and average salaries. The HAVING clause can be used without GROUP BY, treating the entire table as a single group, but this may vary by database implementation.

Uploaded by

rauta1934
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views3 pages

Python Tutorial

The SQL HAVING clause is used to filter grouped data after applying the GROUP BY clause, often in conjunction with aggregate functions like SUM(), COUNT(), or AVG(). It allows for displaying only those groups that meet specific conditions, as demonstrated in examples calculating total and average salaries. The HAVING clause can be used without GROUP BY, treating the entire table as a single group, but this may vary by database implementation.

Uploaded by

rauta1934
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

SQL HAVING Clause

The SQL HAVING clause filters the results of grouped data after using the GROUP BY clause. It
is used with aggregate functions such as SUM(), COUNT(), or AVG() to display only those
groups that meet specific conditions.

Example: First, we will create a demo SQL database and table, on which we will use the
HAVING Clause command.

Query:

SELECT Department, COUNT(EmpID) AS Employee_Count

FROM Employees

GROUP BY Department

HAVING COUNT(EmpID) > 1;

Output:

Syntax:

SELECT column_name, AGGREGATE_FUNCTION(column_name)

FROM table_name

GROUP BY column_name

HAVING condition;

Note: When HAVING is used without GROUP BY, the entire table is treated as a single group.
This works only with aggregate functions.

Examples of HAVING Clause


First, we create the Employee table and insert sample data to demonstrate the HAVING
clause.

Employee Table

Example 1: Filter Total Salary

In this example, we calculate the total salary of all employees and display it only if it meets
the specified condition.

Query:

SELECT SUM(Salary) AS Total_Salary

FROM Employee

GROUP BY (SELECT 1)

HAVING SUM(Salary) >= 250000;

Output:

• SUM(Salary) adds up salaries of all employees.

• GROUP BY (SELECT 1) treats the entire table as one single group.

• HAVING SUM(Salary) >= 250000 filters the result and shows it only when the condition
is met.

Note: GROUP BY (SELECT 1) is database-specific and may not work in all SQL databases. So
alternatively, you can omit GROUP BY and use HAVING directly with aggregate functions.

Example 2: Filter Average Salary

In this example, we calculate the average salary of all employees and display it only if the
average exceeds 55,000.
Query:

SELECT Department, AVG(Salary) AS AverageSalary

FROM Employee

GROUP BY Department

HAVING AVG(Salary) > 55000;

Output:

You might also like