100% found this document useful (1 vote)
2K views

Having Clause

Uploaded by

Pallavi Pasnur
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
100% found this document useful (1 vote)
2K views

Having Clause

Uploaded by

Pallavi Pasnur
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/ 6

FILTERING : HAVING CLAUSE

"Having Clause is used to Filter the Group “

Syntax:
SELECT group_by_expression/group_function
FROM table name
[WHERE <filter_condition>]
GROUP BY column_name/expression
HAVING <group_filter_condition>

ORDER OF EXECUTION:

1-FROM
2-WHERE (if used) [ROW-BY-ROW]
3-GROUP BY(if used) [ROW-BY-ROW]
4-HAVING (if used) [GROUP-BY-GROUP]
5-SELECT [GROUP-BY-GROUP]
Example:
WAQTD to find number of employees working in each Dept if there are at least 3 employees
in each dept.
SELECT DEPTNO, COUNT(*)
FROM EMP
GROUP BY DEPTNO
HAVING COUNT(*)>=3 ;
Questions :
1. WAQTD the designations in which there are at lest 2 employees Present.
SELECT JOB, COUNT(*)
FROM EMP
GROUP BY JOB
HAVING COUNT(*)>=2;

2. WAQTD the names that are repeated.


SELECT ENAME, COUNT(*)
FROM EMP
GROUP BY ENAME
HAVING COUNT(*) > 1;

3. WAQTD names that are repeated exactly twice.


SELECT ENAME, COUNT(*)
FROM EMP
GROUP BY ENAME
HAVING COUNT(*) = 2;

4. WAQTD the salary that is repeated.


SELECT SAL, COUNT(*)
FROM EMP
GROUP BY SAL
HAVING COUNT(*) > 1;
5. WAQTD number of employees working in each dept having At least 2 emp's Character 'A' or 'S' in their names.
SELECT DEPTNO, COUNT(*)
FROM EMP
WHERE ENAME LIKE '%A%' OR ENAME LIKE "%S%'
GROUP BY DEPTNO
HAVING COUNT(*)>=2;

6. WAQTD job and total salary of each job, if the total salary Of each job is greater than 3450.
SELECT JOB, SUM(SAL)
FROM EMP
GROUP BY JOB
HAVING SUM(SAL) > 3450;

7. WAQTD job and total salary of the employees if the employees Are earning more than 1500.
SELECT JOB, SUM(SAL)
FROM EMP
WHERE SAL > 1500
GROUP BY JOB;

8. WAQTD Job wise maximum salary if the maximum salary Of each job exceeds 2000.
SELECT JOB, MAX(SAL)
FROM EMP
GROUP BY JOB
HAVING MAX(SAL) > 2000;
9. WAQTD number of emp earning sal more than 1200 in each job and the total sal needed to pay emp of
each job must exceeds 3800.
SELECT JOB, COUNT(*), SUM(SAL)
FROM EMP
WHERE SAL > 1200
GROUP BY JOB
HAVING SUM(SAL) > 3800;

NOTE:
Differentiate between Where and Having .
WHERE HAVING

➢ Where clause is used to filter the ➢ Having clause is used to filter the
records. groups.
➢ Where clause executes row by ➢ Having clause executes group by
row by now. group
➢ In where clause we cannot use ➢ Can use MRF().
MRF().
➢ Where clause executes before ➢ Having clause executes group by
group by clause. clause.
ASSIGNMENT QUESTIONS ON HAVING CLAUSE

1.WAQTD DNO AND NUMBER OF EMP WORKING IN EACH DEPT IF THERE ARE
ATLEAST 2 CLERKS IN EACH DEPT
2.WAQTD DNO AND TOTAL SAALARYNEEDED TO PAY ALL EMP IN EACH DEPT IF
THERE ARE ATLEAST 4 EMP IN EACH DEPT
3.WAQTD NUMBER OF EMP EARNING SAL MORE THAN 1200 IN EACH JOB AND THE
TOTAL SAL NEEDED TO PAY EMP OF EACH JOB MUST EXCEES 3800
4.WAQTD DEPTNO AND NUMBER OF EMP WORKING ONLY IF THERE ARE 2 EMP
WORKING IN EACH DEPT AS MANAGER.
5.WAQTD JOB AND MAX SAL OF EMP IN EACH JOB IF THE MAX SAL EXCEEDS 2600
6.WAQTD THE SALARIES WHICH ARE REPEATED IN EMP TABLE
7.WAQTD THE HIREDATE WHICH ARE DUPLICATED IN EMP TABLE
8. WAQTD AVG SALARY OF EACH DEPT IF AVG SAL IS LESS THAN 3000
9. WAQTD DEPTNO IF THERE ARE ATLEAST 3 EMP IN EACH DEPT WHOS NAME
HAS CHAR 'A' OR 'S'.
10. WAQTD MIN AND MAX SALARIES OF EACH JOB IF MIN SAL IS MORE THAN 1000
AND MAX SAL IS LESS THAN 5000.

You might also like