The HAVING Clause is used to filter data based on the GROUP function. This is similar to WHERE condition but it is used with GROUP BY function. Group By Function cannot be used in WHERE clause but can be used in HAVING clause.
The basic syntax is as follows:
SELECT column1, column2, ... column_n Aggregate_Function(Column) FROM tables WHERE conditions GROUP BY column1, column2, ... column_n HAVING condition; |
Employee | ||||
Emp_id | Name | Surname | Salary | Dept_id |
1 | Rajesh | Khanna | 45000 | 2 |
2 | Sneha | Gupta | 55000 | 4 |
3 | Sandeep | Nehte | 95000 | 1 |
4 | Kirti | Patil | 25000 | 3 |
5 | Nilesh | Jadhav | 45000 | 3 |
6 | Dipesh | Das | 35000 | 5 |
7 | Kailash | Rane | 62000 | 1 |
8 | Kavita | Deshpande | 48000 | 3 |
9 | Sanjay | Datt | 50000 | 3 |
10 | Shruti | Hasan | 78000 | 1 |
Parameters or Arguments
- A function such as SUM, COUNT, MIN, MAX, or AVG functions.
- The columns that are not encapsulated within an aggregate function and must be included in the GROUP BY clause.
The condition that is used to restrict the groups of returned rows. Only those groups whose condition evaluates to TRUE will be included in the result set.
Syntax For SQL:
SELECT Emp_id, Name, SUM(salary) FROM employee WHERE salary>45000 GROUP BY Name HAVING Sum(salary)>45000 |
After Execute:
Emp_id | Name | Salary | Dept_id |
3 | Sandeep | 95000 | 1 |
10 | Shruti | 78000 | 1 |
7 | Kailash | 62000 | 1 |
2 | Sneha | 55000 | 4 |
9 | Sanjay | 50000 | 3 |
8 | Kavita | 48000 | 3 |
1 | Rajesh | 45000 | 2 |
5 | Nilesh | 45000 | 3 |
No comments:
Post a Comment