SQL Group By Clause

Read this article to understand the Group By clause along with the examples. We have shared few examples and we have shared basic as well as advance level common interview questions also.

In this post, I will provide you a short overview of Group By clause with all its options. In addition, we have covered some questions which we receive from the audience. 

First, let’s talk about the theory of Group By clause and why we should use it.

Definition of Group By Clause

The Group By Clause or Keyword is used to Group the data based on values. This clause combines the data based on values and return the results along with aggregate functions. We can combine one or more aggregate functions on grouped data. When we want to summarise the data based of groups then we use GroupBy Clause.

Reasons to Use Group By Clause

As it’s name suggest we can use the Group By clause to Grouping the data. Below are the some key areas where we need to use this clause.

  1. Aggregate functions along with Group By allow you to summarise data by performing calculations on groups of rows or an entire table.
  2. Aggregate functions can be used to combine data from multiple rows into a single value.
  3. Group By along with aggregate functions are useful for calculating various statistics, such as minimum (MIN) and maximum (MAX) values, average (AVG), median, mode, and standard deviation (STDDEV), among others.
  4. Group by along with aggregate functions can be used to perform data validation checks on your grouped data.

Syntax

SELECT column1, column2, .., columnN, aggregate_function(column)..
FROM table_name
[WHERE condition]
GROUP BY column1, column2, ..., columnN
[HAVING condition]
[ORDER BY columns];

We can use any aggregate functions which is mentioned here along with Group By. Cannot use group by without any aggregate functions. We can use Where clause before Group By clause. If we have to apply any conditions on groupby data then we can use Having clause like Where clause after Group By clause. We can sort the aggregated data after groupby clause.

Example

Example 1:

Lets see the graphical representation of the Group By clause. We have “Customers” table, having 5 records with few columns. Print the number of records country wise.

customer_idfirst_namelast_nameagecountry
1JohnDoe31USA
2RobertLuna22USA
3DavidRobinson32UK
4JohnSinha22UK
5JamesDoe19INDIA
SELECT count(*), country
FROM Customers
GROUP BY country;
count(*)country
2
USA
2
UK
1
INDIA

Above example print the number of records per country. Lets sort the above records using ascending order.

Example 2:

customer_idfirst_namelast_nameagecountry
1JohnDoe31USA
2RobertLuna22USA
3DavidRobinson32UK
4JohnSinha22UK
5JamesDoe19INDIA
SELECT count(*), country
FROM Customers
GROUP BY country
Order By country ASC;
count(*)country
1
INDIA
2
UK
2
USA

Lets show only those country which has more than one records.

Example 3:

customer_idfirst_namelast_nameagecountry
1JohnDoe31USA
2RobertLuna22USA
3DavidRobinson32UK
4JohnSinha22UK
5JamesDoe19INDIA
SELECT count(*), country
FROM Customers
GROUP BY country
HAVING count(*) > 1
Order By country ASC;
count(*)country
2
UK
2
USA

I hope all above example clears the concept of Group By clause and it’s use.

Linking

Group By clause is used in Select statement to show grouped data together. We can use Where before Group By. We can you all aggregate functions with Group By. Please refer all the links for understanding the Group By in depth.

SQL Group By Clause with Latest SQL Interview Questions 2023

Basic Group By SQL Interview questions

  1. What is the purpose of the GROUP BY clause in SQL?
  2. What are some common aggregate functions used with the GROUP BY clause?
  3. How do you use the GROUP BY clause in a SQL query?
  4. Can you use WHERE and HAVING clauses with the GROUP BY clause in SQL?

Intermediate Group By SQL Interview questions

  1. What is the order of execution for clauses in a SQL query that includes GROUP BY?
  2. How can you filter the result set of a GROUP BY query based on the aggregated values?

Advanced Group By SQL Interview questions

  1. Can you use multiple columns in the GROUP BY clause to create nested groups in SQL?

Questions covered from comment section

In this section we will cover the new questions as well as answers. Please comment the questions into comment section so we will include it here. Below are few questions which we thought should be covered.

Que : What is the order of execution for clauses in a SQL query that includes GROUP BY?

Ans : Already we have covered this question in order by clause. still I am proving the high level sequence of execution of groupby clause.

  1. FROM: Specifies the table(s) from which to retrieve data.
  2. WHERE: Filters rows based on specified conditions.
  3. GROUP BY: Groups rows based on specified column(s).
  4. HAVING: Filters groups based on specified conditions.
  5. SELECT: Retrieves the columns and applies aggregate functions as needed.
  6. ORDER BY: Sorts the result set based on specified column(s).
  7. LIMIT/OFFSET: Specifies the subset of rows to retrieve (if applicable).

Quiz

Click on this link to solve few MCQ questions on Group By clause.

I hope you enjoyed this session. Happy offline learning… Lets meet in next session.

Leave a Comment