Objectives:
- Learn how to use the
GROUP BY
clause to group rows that have the same values in specified columns - Understand aggregate functions such as
COUNT()
,SUM()
,AVG()
,MIN()
, andMAX()
- Practice writing queries with
GROUP BY
and aggregate functions
1. Introduction to GROUP BY Clause
Definition:
- The
GROUP BY
clause groups rows that have the same values in specified columns into summary rows.
Syntax Overview:
- Group rows by one or more columns.
SELECT column1, aggregate_function(column2)
FROM table_name
GROUP BY column1;
Example:
SELECT CourseID, COUNT(StudentID) AS NumberOfStudents
FROM Enrollments
GROUP BY CourseID;
2. Using COUNT() Function
Syntax:
COUNT()
is used to count the number of rows in each group.
SELECT column_name, COUNT(*)
FROM table_name
GROUP BY column_name;
Example:
SELECT CourseID, COUNT(*) AS NumberOfEnrollments
FROM Enrollments
GROUP BY CourseID;
Exercise:
- Write SQL queries to:
- Count the number of students in each course.
- Count the number of students enrolled in each course.
3. Using SUM() Function
Syntax:
SUM()
is used to calculate the total sum of a numeric column.
SELECT column_name, SUM(column_name)
FROM table_name
GROUP BY column_name;
Example:
SELECT CourseID, SUM(Fee) AS TotalFees
FROM Enrollments
GROUP BY CourseID;
Exercise:
- Write SQL queries to:
- Calculate the total fees for each course.
- Sum the enrollment fees for all courses.
4. Using AVG() Function
Syntax:
AVG()
calculates the average value of a numeric column.
SELECT column_name, AVG(column_name)
FROM table_name
GROUP BY column_name;
Example:
SELECT CourseID, AVG(Fee) AS AverageFee
FROM Enrollments
GROUP BY CourseID;
Exercise:
- Write SQL queries to:
- Find the average fee for each course.
- Calculate the average enrollment fee across all courses.
5. Using MIN() and MAX() Functions
Syntax:
MIN()
returns the minimum value of a numeric column.MAX()
returns the maximum value of a numeric column.
SELECT column_name, MIN(column_name), MAX(column_name)
FROM table_name
GROUP BY column_name;
Example:
SELECT CourseID, MIN(Fee) AS MinFee, MAX(Fee) AS MaxFee
FROM Enrollments
GROUP BY CourseID;
Exercise:
- Write SQL queries to:
- Find the minimum and maximum fees for each course.
- Get the minimum and maximum enrollment fees for all courses.
6. Practical Exercise: GROUP BY with Aggregate Functions
- Task:
- Use the
Students
,Courses
, andEnrollments
tables. - Insert sample data into
Students
,Courses
, andEnrollments
tables:
INSERT INTO Courses (CourseID, CourseName, Fee)
VALUES
(1, 'Math', 500),
(2, 'Science', 600),
(3, 'History', 450);
INSERT INTO Enrollments (EnrollmentID, StudentID, CourseID, Fee)
VALUES
(1, 1, 1, 500),
(2, 2, 2, 600),
(3, 3, 1, 450),
(4, 4, 3, 400);
- Exercises:
- List the number of students enrolled in each course using
COUNT()
. - Calculate the total fee for each course using
SUM()
. - Find the average fee for each course using
AVG()
. - Determine the minimum and maximum fees for each course using
MIN()
andMAX()
.
Exercises
- Using COUNT():
- Exercise: Write SQL queries to count the number of students in each course.
- Using SUM():
- Exercise: Write SQL queries to calculate the total fee for each course.
- Using AVG():
- Exercise: Write SQL queries to find the average fee for each course.
- Using MIN() and MAX():
- Exercise: Write SQL queries to determine the minimum and maximum fees for each course.
Homework:
- Practice Questions:
- Write SQL queries to:
- Count the number of students in each course.
- Find the total fee for each course.
- Calculate the average fee for each course.
- Get the minimum and maximum fees for each course.