GROUP BY forms a group for each distinct grouping value; an aggregate then summarizes each group. NULL grouping values form a group together. WHERE filters input rows; HAVING filters the groups produced from those rows.
SELECT shelf_id, COUNT(*) AS book_count, SUM(pages) AS page_total
FROM books WHERE published = 1
GROUP BY shelf_id HAVING COUNT(*) >= 2 ORDER BY shelf_id;
Only shelf 1 appears, with two books and 200 pages. Clay Roads is removed before grouping because it is unpublished; the NULL-shelf group contains only one book and fails HAVING.
FROM rows → WHERE rows → GROUP BY → HAVING groups → SELECT → ORDER BY → LIMIT
This is a helpful logical model, not a promise about the physical execution plan. Select grouping columns and aggregates rather than depending on SQLite’s permissive bare-column behavior.