Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 16

17- SQL

(GROUP BY
& HAVING
Clause)
GROUP BY & HAVING Clause
The SQL GROUP BY clause is used in collaboration with the SELECT statement and
aggregate functions to arrange identical rows or column(s) values into groups.

//OR//

The GROUP BY clause is used in conjunction with the SELECT statement and
Aggregate functions to group rows together by common column(s) values.

//OR//

The GROUP BY statement groups rows that have the same values into summary
rows. Summary rows show a summary (count, max, min, sum, avg) of the values
of a table’s column(s) using one or more summarization/aggregation functions. In
other words, this means that the GROUP BY statement is used in combination
with aggregate functions to group the result-set by one or more aggregate
columns (also called summary columns).

Syntax of GROUP BY clause

SELECT column_name(s)
FROM table_name
WHERE [conditions]
GROUP BY column_name(s)
ORDER BY column_name(s);
Important notes:
 The WHERE clause & ORDER BY clause are Optional in the syntax above.
 The GROUP BY clause must come after the WHERE clause and must come
before the ORDER BY clause if one is used.
 A result-set is not grouped without an aggregate column. The resulting
rows from a GROUP BY clause without aggregate values are not summary
rows. GROUP BY clause is therefore mainly used in association with
aggregate functions.
SQL Practical:
We have got the following “Customers” Table.

We want to find number of customers from each province. Following is the


“query” for the problem.
Now, we want to find the number of customers from each city along with the
name of the province listed for each city. Following is the “query” for the
problem.

Now, we want to know the number of customers from each city that have age
less than and equal to 40.
Now we want to order our result-set according to the cities with most customers.
Where the “number of customers” are same, it will order the records according to
the “City Name” in ascending order.

In the first query above, we have grouped our result-set according to the number
of customers in each province using the COUNT() aggregate function. Whereas, in
the rest of the queries, we have grouped our result-set according to the number
of customers in each city using the COUNT() aggregate function. Similarly we can
group any table’s data using the GROUP BY clause in alliance with the aggregate
functions (COUNT(), MAX(), MIN(), SUM(), AVG()).
Notes:
Definition of the table used to query the above problems

Links
Google Search: Group By clause in SQL
https://www.tutorialspoint.com/sql/sql-group-by.htm
https://www.w3schools.com/sql/sql_groupby.asp
https://www.geeksforgeeks.org/sql-group-by/
https://www.sqlservertutorial.net/sql-server-basics/sql-server-group-by/
HAVING clause is used to apply conditions on the result-set obtained from the
GROUP BY clause. The HAVING clause was added to SQL because the WHERE
keyword cannot be used with aggregate functions. The WHERE clause places
conditions on the table columns, whereas the HAVING clause places conditions on
the columns in the result-set obtained from the GROUP BY clause.
Syntax for HAVING clause
SELECT column_name(s)
FROM table_name
WHERE [conditions]
GROUP BY column_name(s)
HAVING [conditions]
ORDER BY column_name(s);
Important notes:
 The HAVING clause must come after the GROUP BY clause in the query and must come
before the ORDER BY clause if used.
 Similar to WHERE clause, HAVING clause is used to apply conditions. But it works with
groups.
 The HAVING clause enables you to filter a group. The HAVING Clause enables you to
specify conditions that filter which group results appear in the results.

SQL Practical:
Continuing from the above example, we now want to filter out cities from the
group that have number of customers greater than 1.
Links:
Google Search: Having clause in SQL
https://www.w3schools.com/sql/sql_having.asp
https://www.tutorialspoint.com/sql/sql-having-clause.htm
https://www.geeksforgeeks.org/sql-having-clause-with-examples/
https://www.sqlservertutorial.net/sql-server-basics/sql-server-having/

We can also group the result-set in the following ways

//OR//
Another Practical Example
Employees Table:

The above table shows a list of random employees. The table lists the number of
jobs worked by each employee in the year and the different professions worked
in for the year. The table also lists the city of the job and the salary of the job
worked in. NOTE: There may be different jobs of same profession in the same city.
So don’t get confused by considering them the same job. Each row in the table
represents a different job.
Query1:

Query2:
Query3:

Notes:
Definition of the table used for the above queries.
GROUP BY & HAVING Clause with Multiple Tables

Suppose that an international organization has launched a “Skills Enhancing


Program” in Pakistan in which it offers multiple courses in multiple cities of
Pakistan. In the above three tables, Courses table shows the courses offered by
the organization, Cities table shows the cities where the courses are offered,
Student table shows the students who have registered for the program along with
the course each student has registered in and the city each student belongs to.
SQL Practical
Query1:

We have grouped our result-set according to the number of students registered in each course.
The GROUP BY clause is applied on the result-set obtained from the Right Outer Join between
Student table and Courses table.

Applying filter using WHERE clause on the result-set obtained from the Right Outer Join
between Student table and Courses table. The GROUP BY clause is then applied on the filtered
result-set.
Applying HAVING clause on the result-set (group) obtained from the GROUP BY clause.

Applying ORDER BY clause on the filtered result-set (filtered group) obtained from the HAVING
clause.
Query2:

We have grouped our result-set according to the number of students registered in each city.
Query3:
There may be cities where all courses are not being taught because the students
may not register in some courses. So we now want to list all the different courses
being taught in each city along with the number of students registered for each
course in the city.

The above result-set is grouped according to the number of students registered


for each different course being taught in the city.

It should be clear by now that GROUP BY comes after the JOIN clause in the
syntax.
Notes:
Definitions of the tables created.

Important Link:
Group By & Having clause Explained: https://www.sqlshack.com/an-overview-of-
the-sql-group-by-clause/

You might also like