How Grouping happens under the hood
Let's take this example:
SELECT user_id , SUM ( amount ) as balance from transactions where was_successful = 1 GROUP BY user_id
Enter fullscreen mode
Exit fullscreen mode
The grouping happens first , and the aggregate calculation happens second .
Here is how the SQL engine processes this conceptually step-by-step:
1. Filter rows ( FROM and WHERE )
The engine grabs the transactions table and discards any row where was_successful is not 1 (or true ). At this stage, you just have a filtered list of individual transaction rows.
2. Form the buckets ( GROUP BY )
Next, the engine organizes the remaining rows into distinct "buckets" based on the grouping key—in this case, user_id .…