Group by and Having clause in SQL:-
Group by and Having clause in SQL is very important topic of SQL.
USE OF HAVING Clause
HAVING clause: It is designed to be used with GROUP BY So that it can restrict the groups that appear in the final result table.
Example: For each branch office with more than one member of staff, find the number of staff working in each branch and the sum of their salaries.
Query: SELECT bno, COUNT(sno) AS count, SUM(salary)
GROUP BY bno
HAVING COUNT(sno) > 1
ORDER by bno;
Writing Queries using GROUP BY and other clauses:
Example: List the jobs and number of employees in each job. The result should be in the descending order of the number of employees.
select job, count () from emp group by job order by count () desc;
Example: List the total salary, maximum and minimum salary and average salary of the employee’s jobs, and display only those rows having average salary > 1000
Select job, sum(sal), max(sal), min(sal), avg(sal)
group by job
Having avg(sal) > 1000
for More Articles:-
If you have any questions regarding database management system then comment your questions