HAVING filters after aggregate facts exist.

highlighted = computed this step

HAVING filters groups

HAVING decides group by group after aggregate facts are ready.

HAVING sees groups\text{HAVING sees groups}

Count group decisions

HAVING keeps 1 group and drops 2 groups in this threshold example.

kept groups=1\text{kept groups}=1

HAVING examples are tiny finite group-filter transforms; SQL dialect completeness, optimizer behavior, indexing, and product claims are out of scope.

HAVING filters groups after aggregation: base rowsidregionstatusqty1eastopen22eastopen33westopen14westclosed45northNULL26northopen1 WHERE before groupingsourceinputwhereTruthgroupedRole0[1, east, open, 2]TRUEgrouped1[2, east, open, 3]TRUEgrouped2[3, west, open, 1]TRUEgrouped3[4, west, closed, 4]FALSEdropped_before_grouping4[5, north, NULL, 2]UNKNOWNdropped_before_grouping5[6, north, open, 1]TRUEgrouped GROUP BY bucketskeysourceRowsrowCount[east][0, 1]2[west][2]1[north][5]1 Aggregate facts per groupkeysourceRowsaggregates[east][0, 1]{n:2, total_qty:5}[west][2]{n:1, total_qty:1}[north][5]{n:1, total_qty:1} HAVING result per groupkeypredicatetruthoutputRole[east]total_qty ge 2TRUEkept[west]total_qty ge 2FALSEdropped[north]total_qty ge 2FALSEdropped HAVING output groupsregionntotal_qtyeast25 HAVING factsfactvalueinputRowCount6whereKeptRows4whereDroppedRows2groupCount3aggregateCount2outputGroupCount1havingTrueCount1havingFalseCount2havingUnknownCount0groupOrderfirst_seen_after_wherepipelineWHERE_before_GROUP_BY_then_HAVING

Output rows are groups

The final output rows are grouped rows, not original source rows.

group output\text{group output}

Summary

HAVING is the group boundary.

group filter after aggregate\text{group filter after aggregate}