EZ TuitionsAll tuitions

MySQL WHERE vs HAVING: Filter Rows, Then Groups

WHERE filters input rows before grouping. HAVING filters the resulting groups. On the supplied data, section A averages 70 across all its rows, but 85 after WHERE Score >= 60. Those are different questions, not two ways to request the same answer.

This read-only exercise asks you to trace six invented quiz-score rows. The sequence below is a reasoning model for these queries, not a claim about the database optimizer's physical execution plan.

Start from the complete supplied relation

PracticeScores has a unique integer AttemptID, non-NULL text Section and non-NULL integer Score. Each row is one quiz attempt, not necessarily a different student. There are no missing values or joins in this exercise.

PracticeScores: all six original rows
AttemptIDSectionScore
301A40
302A80
303A90
304B60
305B70
306C30

Trace rows, groups, then the group condition

  1. Apply WHERE if present. Write the retained AttemptIDs; do not calculate the average from removed rows.
  2. Group the retained rows by Section. Calculate COUNT(*) and AVG(Score) separately for each group.
  3. Apply HAVING to those group summaries. Retain or remove a whole group, not selected scores within it.
  4. Read the three output columns: Section, Attempts and MeanScore. ORDER BY Section states the display order.

Only Section and aggregate expressions appear in the select list. An ungrouped AttemptID is deliberately not selected. MySQL 8.4 is the documentation reference, not a curriculum-mandated server version.

Predict and check four grouped results

Whole sections with an average of at least 65

SELECT Section, COUNT(*) AS Attempts,
       AVG(Score) AS MeanScore
FROM PracticeScores
GROUP BY Section
HAVING AVG(Score) >= 65
ORDER BY Section;

After WHERE, retained AttemptIDs: 301, 302, 303, 304, 305, 306.

Expected columns: Section | Attempts | MeanScore

A | 3 | 70
B | 2 | 65

Group all six rows first. Section A has (40 + 80 + 90) / 3 = 70; B has (60 + 70) / 2 = 65; C has 30. HAVING removes C, not individual scores below 65. The 40 in A and the 60 in B still contribute to the displayed averages.

Scores of at least 60, then groups averaging at least 65

SELECT Section, COUNT(*) AS Attempts,
       AVG(Score) AS MeanScore
FROM PracticeScores
WHERE Score >= 60
GROUP BY Section
HAVING AVG(Score) >= 65
ORDER BY Section;

After WHERE, retained AttemptIDs: 302, 303, 304, 305.

Expected columns: Section | Attempts | MeanScore

A | 2 | 85
B | 2 | 65

WHERE removes 301 and 306 before grouping. A is now (80 + 90) / 2 = 85, not 70. B remains 65. There is no C group because no C row survived WHERE. HAVING then keeps both remaining groups.

After the same row filter, keep groups with at least two rows

SELECT Section, COUNT(*) AS Attempts,
       AVG(Score) AS MeanScore
FROM PracticeScores
WHERE Score >= 60
GROUP BY Section
HAVING COUNT(*) >= 2
ORDER BY Section;

After WHERE, retained AttemptIDs: 302, 303, 304, 305.

Expected columns: Section | Attempts | MeanScore

A | 2 | 85
B | 2 | 65

COUNT(*) applies to each group after WHERE. A and B each contain two retained rows. It does not count how many rows were present in the original complete relation. Equal output here does not make the two HAVING conditions interchangeable on other data.

After the row filter, ask for at least three rows per group

SELECT Section, COUNT(*) AS Attempts,
       AVG(Score) AS MeanScore
FROM PracticeScores
WHERE Score >= 60
GROUP BY Section
HAVING COUNT(*) >= 3
ORDER BY Section;

After WHERE, retained AttemptIDs: 302, 303, 304, 305.

Expected columns: Section | Attempts | MeanScore

(no result rows)

A and B each have two retained rows, so neither satisfies COUNT(*) >= 3. This grouped query returns no result rows. Do not invent a row with count zero, and do not restore A's removed score of 40 to reach three.

Reject the tempting wrong rewrite

WHERE Score >= 65 asks which individual attempts meet a threshold. HAVING AVG(Score) >= 65 asks which section averages meet it. In the first query, removing A's 40 before averaging would silently change its intended whole-section average.

Likewise, WHERE AVG(Score) >= 65 is not a valid replacement for the aggregate HAVING condition in this query block. An aggregate alias such as MeanScore is not available for WHERE to test here. Write the aggregate expression in HAVING when explaining the group condition.

If counting retained rows is still unclear, use the separate COUNT(*) versus COUNT(column) supplied-table exercise. Its ungrouped one-output-row examples do not imply that this grouped query must return a row when no group qualifies.

School relevance and limits

CBSE 2026-27 Informatics Practices 065, Class XII Unit 2, includes aggregate functions and querying with Group by, Having and Order by. This original exercise supports that method. It is not an official exam question, reproduced textbook table or answer-key promise.

This page does not cover NULL handling, joins, window functions, installation, data modification or performance tuning. The numeric outputs are derived from the supplied rows and MySQL documentation. No native MySQL execution was observed.

Frequently asked questions

Can I put AVG(Score) >= 65 in WHERE?

Not as an aggregate condition in this same query block. WHERE selects individual input rows and does not accept aggregate functions. Use HAVING AVG(Score) >= 65 after grouping for this group-average condition.

Does HAVING AVG(Score) >= 65 remove every score below 65?

No. In the first query A keeps its 40 and B keeps its 60. HAVING tests each completed group average. A row condition such as WHERE Score >= 65 would change the rows used to calculate that average.

Must HAVING always appear with GROUP BY?

No. MySQL also supports aggregate queries without an explicit GROUP BY, where the rows form one implicit group. This exercise uses explicit section groups; it does not teach an always-with-GROUP-BY rule.

Does GROUP BY guarantee alphabetical output?

Do not rely on GROUP BY to order the result. Every query here ends with ORDER BY Section so the expected rows are shown in a stated order.

Were these queries run on a MySQL server?

No. The rows and expected outputs are original documentation-derived practice. No server session, application UI or native execution was observed. Displayed averages are numeric values, not a promise about client decimal formatting.

Sources and limits

Primary sources checked 27 November 2026. All table rows, queries and explanations are original EZ Tuitions practice.