Menu

SQL, GroupBy and Aggregates
📰

SQL, GroupBy and Aggregates

DEV Community: sql·Ahmed Awji·10 days ago
#942Igsco
#devtosql#devto#dev#user_id#amount
Long article (522 words) — no AI summary yet.
Reading 0:00
15s threshold

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 .…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.