GROUP BY and HAVING Clause in SQL Article DataCamp
GROUP BY and HAVING Clause in SQL Article DataCamp
Avinash Navlani
January 30th, 2019
SQL +1
An important component for Analyst to summarize the data such as sales, pro t, cost, and
salary. Data Summarization is very helpful for Analyst to create a visualization, conclude
ndings, and report writing. In SQL, GROUP BY Clause is one of the tools to summarize or
aggregate the data series. For example, sum up the daily sales and combine in a single
quarter and show it to the senior management. Similarly, if you want to count how many
employees in each department of the company. It groups the databases on the basis of one
or more column and aggregates the results.
After Grouping the data, you can lter the grouped record using HAVING Clause. HAVING
Clause returns the grouped records which match the given condition. You can also sort the
grouped records using ORDER BY. ORDER BY used after GROUP BY on aggregated column.
In this tutorial, you are going to learn GROUP BY Clause in detail with relevant examples.
Here is the list of topics that you will learn in this tutorial:
Group By Clause
Having Clause
Aggregate Functions
Want to leave a comment?
https://www.datacamp.com/community/tutorials/group-by-having-clause-sql 1/10
8/30/2019 GROUP BY and HAVING Clause in SQL (article) - DataCamp
Conclusion
Group By Clause
The GROUP BY Clause is utilized in SQL with the SELECT statement to organize similar data
into groups. It combines the multiple records in single or more columns using some
functions. Generally, these functions are aggregate functions such as min(),max(),avg(),
count(), and sum() to combine into single or multiple columns. It uses the split-apply-
combine strategy for data analysis.
In the apply phase , It applies the aggregate function and generates a single value.
In the combiner phase , It combines the groups with single values into a single value.
https://www.datacamp.com/community/tutorials/group-by-having-clause-sql 2/10
8/30/2019 GROUP BY and HAVING Clause in SQL (article) - DataCamp
Image source
Points to Remember:
GROUP BY aggregates the results on the basis of selected column: COUNT, MAX, MIN,
SUM, AVG, etc.
https://www.datacamp.com/community/tutorials/group-by-having-clause-sql 3/10
8/30/2019 GROUP BY and HAVING Clause in SQL (article) - DataCamp
In above example, Table is grouped based on the DeptID column and Salary is aggregated
department-wise.
Having Clause
HAVING Clause utilized in SQL as a conditional Clause with GROUP BY Clause. This
conditional clause returns rows where aggregate function results matched with given
conditions only. It added in the SQL because WHERE Clause cannot be combined with
aggregate results, so it has a different purpose. The primary purpose of the WHERE Clause is
to deal with non-aggregated or individual records.
HAVING Clause restricts the data on the group records rather than individual records.
https://www.datacamp.com/community/tutorials/group-by-having-clause-sql 4/10
8/30/2019 GROUP BY and HAVING Clause in SQL (article) - DataCamp
In above example, Table is grouped based on DeptID column and these grouped rows ltered
using HAVING Clause with condition AVG(Salary) > 3000.
Aggregate Functions
Aggregate functions used to combine the result of a group into a single such as COUNT,
MAX, MIN, AVG, SUM, STDDEV, and VARIANCE. These functions also known as multiple-row
functions.
WHERE Clause lters the records tuple by tuple while HAVING Clause lters the whole
group.
Want to leave a comment?
A query may have both the clauses( WHERE and HAVING Clause).
https://www.datacamp.com/community/tutorials/group-by-having-clause-sql 5/10
8/30/2019 GROUP BY and HAVING Clause in SQL (article) - DataCamp
WHERE Clause restricts records before GROUP BY Clause, whereas HAVING Clause
restricts groups after GROUP BY Clause are performed.
WHERE Clause can be utilized with SELECT, UPDATE, DELETE, and INSERT, whereas
HAVING can be utilized only with SELECT statement.
Image Source
https://www.datacamp.com/community/tutorials/group-by-having-clause-sql 6/10
8/30/2019 GROUP BY and HAVING Clause in SQL (article) - DataCamp
In above example, Employee and Department are joined using the common column DeptID.
In the above example, JOIN and GROUP BY both clauses used together in a single query.
After joining both tables(Employee and Department), joined table grouped by Department
name.
https://www.datacamp.com/community/tutorials/group-by-having-clause-sql 7/10
8/30/2019 GROUP BY and HAVING Clause in SQL (article) - DataCamp
DISTINCT returns the unique values present in the column while GROUP BY returns
unique/distinct items with the aggregate resultant column. In the following example you can
see the DISTINCT values in the dept table.
ORDER BY returns sorted items in ascending and descending order while GROUP BY returns
unique items with the aggregate resultant column. In the following example, you can see the
ORDER BY or sorted salary table.
https://www.datacamp.com/community/tutorials/group-by-having-clause-sql 8/10
8/30/2019 GROUP BY and HAVING Clause in SQL (article) - DataCamp
Write SQL queries for the following statements and share your answers in comments:
Source: This Assignment is inspired from the book "Oracle 11g SQL" by John Casteel.
Conclusion
Congratulations, you have made it to the end of this tutorial!
In this tutorial, you have covered a lot of details about the GROUP BY and HAVING Clause.
You have learned what the GROUP BY and HAVING Clause are with examples, Comparison
between HAVING and WHERE Clause in SQL, GROUP BY with JOIN, and GROUP BY
Comparison with DISTINCT and ORDER BY. In the last section, you have a Hands-on practice
assignment to assess your knowledge.
Hopefully, you can now utilize GROUP BY and HAVING Clause concept to analyze your own
datasets. Thanks for reading this tutorial!
If you are interested in learning more about SQL, take DataCamp's Intermediate SQL course.
29 2
COMMENTS
abdul mobeen
Want to leave
06/02/2019 02:24 a
AMcomment?
Best explanation.
https://www.datacamp.com/community/tutorials/group-by-having-clause-sql 9/10
8/30/2019 GROUP BY and HAVING Clause in SQL (article) - DataCamp
3 R E P LY
Vivek Kumar
04/07/2019 03:31 PM
I was so con dent in this game performance, yes it is surly make my day in exciting. play freecell
on your gadget in online and create highest score in this game.
1 R E P LY
Subscribe to RSS
https://www.datacamp.com/community/tutorials/group-by-having-clause-sql 10/10