0% found this document useful (0 votes)
6 views

Group by

The GROUP BY clause in SQL is used to arrange identical data into groups, often in conjunction with aggregate functions like COUNT(), SUM(), AVG(), MAX(), and MIN(). It allows users to perform calculations on each group of data, such as counting the number of entries or calculating averages. Examples include grouping students by grade to count them, by department to find average age, and by city to determine highest marks.

Uploaded by

santhoshupsc5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views

Group by

The GROUP BY clause in SQL is used to arrange identical data into groups, often in conjunction with aggregate functions like COUNT(), SUM(), AVG(), MAX(), and MIN(). It allows users to perform calculations on each group of data, such as counting the number of entries or calculating averages. Examples include grouping students by grade to count them, by department to find average age, and by city to determine highest marks.

Uploaded by

santhoshupsc5
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Group by

What is GROUP BY?

• The GROUP BY clause in SQL is used to arrange identical data into groups. It is typically used with aggregate
functions (like COUNT(), SUM(), AVG(), MAX(), MIN()) to perform operations on each group of data.

• How does GROUP BY work?

1. Data Grouping: When you use GROUP BY, you specify one or more columns that you want to group the
data by. For example, you might want to group students by their grade, meaning all students in the same
grade will be grouped together.

2. Aggregate Functions: Once the data is grouped, you can use aggregate functions to perform calculations on
each group. For example, you can count how many students are in each grade, calculate the average age of
students in each department, or find the highest marks in each city.
Key Concepts

1. Aggregate Functions: These functions allow you to perform calculations on a set


of rows and return a single value for each group.
o COUNT(): Counts the number of rows in a group.
o SUM(): Adds up the values in a group.
o AVG(): Calculates the average value of a group.
o MAX(): Returns the maximum value in a group.
o MIN(): Returns the minimum value in a group.


GROUP BY Query with Example: students Table
• The GROUP BY clause in SQL is used to arrange identical data into groups, often used with aggregate functions like
COUNT(), SUM(), AVG(), MAX(), and MIN(). Let's walk through an example of using the GROUP BY clause with a
students table.

• Create a students1 table

CREATE TABLE students1 (

student_id INT ,

first_name VARCHAR(50),

last_name VARCHAR(50),

grade_level INT,

score DECIMAL(5, 2),

enrollment_date DATE

);
insert
student_id first_name last_name grade_level score enrollment_date

1 John Doe 10 85.50 2022-09-10

2 Jane Smith 10 90.00 2022-08-22

3 Mark Johnson 11 78.00 2021-11-15

4 Emily Davis 11 92.00 2022-01-18

5 Sarah Wilson 10 88.00 2022-04-01

6 Michael Brown 12 75.50 2020-12-05

7 Emma White 12 82.00 2021-03-21


Group by grade and count the number of students in
each grade

SELECT grade, COUNT(*) AS number_of_students


FROM student
GROUP BY grade;
This query will group students by their grade and count how many students are in each grade.
Group by department and calculate the average age of students in each department

SELECT department, AVG(age) AS average_age


FROM student
GROUP BY department;

This query groups students by their department and calculates the average age of students in each
department.
Group by city and find the highest marks in each city

SELECT city, MAX(marks) AS highest_marks

FROM student

GROUP BY city;

This query groups students by their city and finds the highest marks achieved by students in each city.

You might also like