SELECT branch, COUNT(*) AS student_count,
ROUND(AVG(cgpa), 2) AS average_cgpa
FROM students
GROUP BY branch;
Grouping rule
Every selected expression must normally be aggregated, functionally dependent where the DBMS permits it, or listed in GROUP BY.
branch ✓ COUNT(*) ✓ name ✗
04 • FILTER AT THE CORRECT STAGE
WHERE Filters Rows; HAVING Filters Groups
1. FROM
Read candidate rows.
→2. WHERE
Remove individual rows before grouping.
→3. GROUP BY
Form groups from surviving rows.
→4. HAVING
Remove groups after aggregates exist.
ROW CONDITIONWHERE status = 'ACTIVE'
Inactive students never enter a group.
GROUP CONDITIONHAVING AVG(cgpa) >= 8.5
Only groups whose calculated average qualifies remain.
05 • CALCULATE MULTIPLE METRICS IN ONE PASS
CASE Turns Conditions into Aggregate Inputs
SELECT branch,
COUNT(*) AS total,
SUM(CASE WHEN cgpa >= 8.5 THEN 1 ELSE 0 END) AS strong_count,
ROUND(100.0 * SUM(CASE WHEN status = 'ACTIVE' THEN 1 ELSE 0 END)
/ COUNT(*), 1) AS active_percent
FROM students
GROUP BY branch;
Condition TRUECASE contributes 1Condition FALSECASE contributes 0SUMCounts the 1 valuesGuard divisionUse decimal arithmetic and protect zero denominators
06 • DIAGNOSE WRONG SUMMARIES
Four Common Grouping Mistakes
UNGROUPED COLUMNSELECT branch, name, COUNT(*) GROUP BY branch
name is neither grouped nor aggregated and has no single value per branch.
AGGREGATE IN WHEREWHERE AVG(cgpa) >= 8
The average does not exist at the WHERE stage; use HAVING.
WRONG COUNTCOUNT(cgpa)
This counts recorded CGPA values, not all students. Use COUNT(*) for rows.
INTEGER DIVISIONstrong_count / total
Some dialect/type combinations truncate the fraction. Force decimal arithmetic.
07 • BUILD A GROUPED REPORT
Interactive Aggregate Analytics Laboratory
Choose row filtering, grouping, aggregate metrics and HAVING. See the generated SQL, group membership and final report.
GENERATED SQL
FINAL RESULT
08 • COUNT WHAT YOU ACTUALLY MEAN
NULL and Aggregate Outcome Laboratory
Select a calculation to trace which values contribute.
09 • CHECK YOUR UNDERSTANDING
Ten Formative Concept Checks
1. A scalar function normally returns:
2. COUNT(*) counts:
3. COUNT(cgpa) excludes:
4. GROUP BY branch creates:
5. Which clause filters groups?
6. WHERE executes logically:
7. AVG(cgpa) normally ignores:
8. To count rows satisfying a condition, use:
9. SUM over no matching rows generally returns:
10. In SELECT with GROUP BY, an ordinary selected column should usually be:
Answered correctly: 0 of 10
10 • EXPLAIN & PREPARE
University and Interview Questions
2-MARK QUESTIONS
Scalar versus aggregate function?
COUNT(*) versus COUNT(column)?
Define grouping grain.
WHERE versus HAVING?
What does COALESCE do?
5-MARK / PRACTICAL
Build branch-wise student counts.
Filter groups by average CGPA.
Explain aggregate NULL behavior.
Write conditional aggregation.
INTERVIEW QUESTIONS
Why does an ungrouped column fail?
When should HAVING be avoided?
How can integer division break percentages?
What does AVG divide by?
How do empty inputs affect SUM?
Show the grouped-query answer framework
State one output row's meaning.
Filter unwanted source rows with WHERE.
Choose grouping columns that define that grain.
Select aggregates with correct NULL semantics.
Add conditional metrics when needed.
Filter calculated groups with HAVING.
Protect division and empty-set cases.
Verify one group manually from its member rows.
LEVEL 13 SUMMARY
You Can Now Explain Every Number in a Report
Scalar functions transform each row.
Aggregates summarize sets and usually ignore NULL expressions.
GROUP BY defines one result row per distinct group.
WHERE filters before grouping; HAVING filters after it.