Group by and Having a clause in SQL

Group by and Having clause in SQL:-

Group by and Having clauses in SQL is a very important topic of SQL.


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.

Group by and Having a clause in SQL

Query: SELECT bno, COUNT(sno) AS count, SUM(salary)
AS sum
FROM Employee
ORDER by bno;

Writing Queries using GROUP BY and other clauses:

Example: List the jobs and the 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 an average salary > 1000

Select job, sum(sal), max(sal), min(sal), avg(sal)
from employee
group by job
Having avg(sal) > 1000

for More Articles:-

Integrity Constraints in DBMS | SQL

Top 5 programming languages

If you have any questions regarding the database management system then comment your questions

Thank you

Leave a Comment