0% found this document useful (0 votes)
5 views5 pages

(F) .HAVING Clause in SQL

The HAVING clause in SQL is used to filter results after aggregation, as opposed to the WHERE clause which filters individual rows. The document provides examples of creating tables for employees and students, inserting data, and using the HAVING clause to filter grouped results based on aggregate conditions. It demonstrates how to retrieve departments with total salaries over 50,000 and students with percentages above 95%.

Uploaded by

sharmasudip010
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)
5 views5 pages

(F) .HAVING Clause in SQL

The HAVING clause in SQL is used to filter results after aggregation, as opposed to the WHERE clause which filters individual rows. The document provides examples of creating tables for employees and students, inserting data, and using the HAVING clause to filter grouped results based on aggregate conditions. It demonstrates how to retrieve departments with total salaries over 50,000 and students with percentages above 95%.

Uploaded by

sharmasudip010
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
You are on page 1/ 5

HAVING Clause in SQL

The HAVING clause was added to SQL because the WHERE keyword
cannot be used with aggregate functions.

Note: that aggregate functions cannot be used in the WHERE clause. Aggregate functions
act on data that has been grouped using the GROUP BY clause, whereas the WHERE
clause filters individual rows before they are grouped.

Creating table

CREATE TABLE Employee1(


EmployeeId int,
Name Varchar(20),
Gender Varchar(20),
Salary int,
Department Varchar(20),
Experience Varchar(20)
);

Add value into the table:

INSERT INTO Employee VALUES (1, 'Rachit', 'M', 50000, 'Engineering', '6 year')
INSERT INTO Employee VALUES (2, 'Shobit', 'M', 37000, 'HR', '3 year')
INSERT INTO Employee VALUES (3, 'Isha', 'F', 56000, 'Sales', '7 year')
INSERT INTO Employee VALUES (4, 'Devi', 'F', 43000, 'Management', '4 year')
INSERT INTO Employee VALUES (5, 'Akhil', 'M', 90000, 'Engineering', '15 year')
The final table is:

SELECT * FROM Employee;

To Know the sum of salaries

SELECT Department, sum(Salary) as Salary


FROM employee1
GROUP BY department;
if we need to display the departments where the sum of salaries is 50,000 or more.
In this condition, we will use HAVING Clause.

SELECT Department, sum(Salary) as Salary


FROM employee
GROUP BY department
HAVING SUM(Salary) >= 50000;
Example 2:

Suppose, a teacher wants to announce the toppers in class. For this, she decides to
reward every student who scored more than 95%. We need to group by name and
their percentage and find out who scored more than 95% in that year. So for this
first, we will create a table named “Student” .After creating a table we will execute
the query.

Creating table

CREATE TABLE Student(


student Varchar(20),
percentage int
);
Add value into the table:

INSERT INTO Student VALUES ('Isha Patel', 98);


INSERT INTO Student VALUES ('Harsh Das', 94);
INSERT INTO Student VALUES ('Rachit Sha', 93);
INSERT INTO Student VALUES ('Sumedha', 98);
INSERT INTO Student VALUES ('Rahat Ali', 98);

The final table is:

SELECT * FROM Student;


Execute Query

SELECT student, percentage


FROM Student
GROUP BY student, percentage
HAVING percentage > 95;

You might also like