⬅ Previous Topic
ORDER BY Clause in SQLNext Topic ⮕
HAVING Clause in SQL⬅ Previous Topic
ORDER BY Clause in SQLNext Topic ⮕
HAVING Clause in SQLImagine your school wants to know how many students are in each class, or the average marks scored by subject. The GROUP BY
clause is how you group similar data points together and summarize them — turning rows into meaningful insights.
SELECT column1, aggregate_function(column2)
FROM table_name
WHERE condition
GROUP BY column1;
Let’s consider the following table:
CREATE TABLE student_marks (
roll_no INT,
name VARCHAR(50),
class VARCHAR(10),
subject VARCHAR(30),
marks INT
);
INSERT INTO student_marks VALUES
(1, 'Aarav Sharma', '10A', 'Maths', 85),
(2, 'Diya Iyer', '9B', 'Maths', 90),
(3, 'Rohit Menon', '10A', 'Maths', 78),
(4, 'Sneha Patil', '8C', 'Maths', 88),
(5, 'Mehul Agarwal', '9B', 'Maths', 92),
(1, 'Aarav Sharma', '10A', 'Science', 91),
(2, 'Diya Iyer', '9B', 'Science', 87),
(3, 'Rohit Menon', '10A', 'Science', 85),
(4, 'Sneha Patil', '8C', 'Science', 89),
(5, 'Mehul Agarwal', '9B', 'Science', 93);
Let’s count how many marks entries exist per class:
SELECT class, COUNT(*) AS total_entries
FROM student_marks
GROUP BY class;
class | total_entries
------+---------------
10A | 4
9B | 4
8C | 2
To calculate the average marks for each subject:
SELECT subject, AVG(marks) AS avg_marks
FROM student_marks
GROUP BY subject;
subject | avg_marks
--------+-----------
Maths | 86.6
Science | 89.0
Let’s find the top marks in each class (across all subjects):
SELECT class, MAX(marks) AS top_score
FROM student_marks
GROUP BY class;
Now, let’s see average marks per subject per class:
SELECT class, subject, AVG(marks) AS avg_marks
FROM student_marks
GROUP BY class, subject;
class | subject | avg_marks
------+---------+-----------
10A | Maths | 81.5
10A | Science | 88.0
9B | Maths | 91.0
9B | Science | 90.0
8C | Maths | 88.0
8C | Science | 89.0
What if we only want to group Maths marks?
SELECT class, AVG(marks) AS avg_maths
FROM student_marks
WHERE subject = 'Maths'
GROUP BY class;
WHERE
filters before grouping. HAVING
filters after. For example:
Show only subjects with average marks greater than 88:
SELECT subject, AVG(marks) AS avg_marks
FROM student_marks
GROUP BY subject
HAVING AVG(marks) > 88;
subject | avg_marks
--------+-----------
Science | 89.0
GROUP BY
is your tool for summaries. It transforms lists into reports, raw data into insights. Whether you're calculating subject-wise averages or class-level toppers, this clause does the heavy lifting with just a few lines.
Now that you can group and summarize, it’s time to narrow down the output — using the LIMIT clause to fetch only top records, like the top 3 scorers in a subject.
SELECT class, COUNT(*) FROM students GROUP BY class;
⬅ Previous Topic
ORDER BY Clause in SQLNext Topic ⮕
HAVING Clause in SQLYou can support this website with a contribution of your choice.
When making a contribution, mention your name, and programguru.org in the message. Your name shall be displayed in the sponsors list.