<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ahmed Awji</title>
    <description>The latest articles on DEV Community by Ahmed Awji (@awji).</description>
    <link>https://dev.to/awji</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2079574%2F2b0957ff-1603-4698-889a-fc6a4b24e380.jpg</url>
      <title>DEV Community: Ahmed Awji</title>
      <link>https://dev.to/awji</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/awji"/>
    <language>en</language>
    <item>
      <title>SQL, GroupBy and Aggregates</title>
      <dc:creator>Ahmed Awji</dc:creator>
      <pubDate>Thu, 26 Nov 2026 13:40:04 +0000</pubDate>
      <link>https://dev.to/awji/sql-groupby-and-aggregates-3e9h</link>
      <guid>https://dev.to/awji/sql-groupby-and-aggregates-3e9h</guid>
      <description>&lt;h2&gt;
  
  
  How Grouping happens under the hood
&lt;/h2&gt;

&lt;p&gt;Let's take this example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;as&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;
&lt;span class="k"&gt;from&lt;/span&gt;
  &lt;span class="n"&gt;transactions&lt;/span&gt;
&lt;span class="k"&gt;where&lt;/span&gt;
  &lt;span class="n"&gt;was_successful&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt;
  &lt;span class="n"&gt;user_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The grouping happens &lt;strong&gt;first&lt;/strong&gt;, and the aggregate calculation happens &lt;strong&gt;second&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Here is how the SQL engine processes this conceptually step-by-step:&lt;/p&gt;

&lt;h3&gt;
  
  
  1. Filter rows (&lt;code&gt;FROM&lt;/code&gt; and &lt;code&gt;WHERE&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;The engine grabs the &lt;code&gt;transactions&lt;/code&gt; table and discards any row where &lt;code&gt;was_successful&lt;/code&gt; is not &lt;code&gt;1&lt;/code&gt; (or &lt;code&gt;true&lt;/code&gt;). At this stage, you just have a filtered list of individual transaction rows.&lt;/p&gt;

&lt;h3&gt;
  
  
  2. Form the buckets (&lt;code&gt;GROUP BY&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Next, the engine organizes the remaining rows into distinct "buckets" based on the grouping key—in this case, &lt;code&gt;user_id&lt;/code&gt;. Every row with &lt;code&gt;user_id = 1&lt;/code&gt; goes into bucket 1, rows with &lt;code&gt;user_id = 2&lt;/code&gt; go into bucket 2, and so on:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[Bucket: user_id = 1]
- Row A: amount = 10.50
- Row B: amount = -4.25

[Bucket: user_id = 2]
- Row C: amount = -9.56
- Row D: amount = -50.00
- Row E: amount = 40.00
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  3. Compute the aggregates (&lt;code&gt;SELECT&lt;/code&gt;)
&lt;/h3&gt;

&lt;p&gt;Once the buckets exist, the aggregate function (&lt;code&gt;SUM(amount)&lt;/code&gt;) runs across each bucket individually:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Bucket 1: &lt;code&gt;10.50 + (-4.25) = 6.25&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Bucket 2: &lt;code&gt;-9.56 + (-50.00) + 40.00 = -19.56&lt;/code&gt;&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  4. Produce the final output
&lt;/h3&gt;

&lt;p&gt;Each bucket is collapsed into exactly one output row containing the grouping key (&lt;code&gt;user_id&lt;/code&gt;) and the computed result (&lt;code&gt;balance&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;This order of operations is also why you cannot use an aggregate function like &lt;code&gt;SUM()&lt;/code&gt; directly in a &lt;code&gt;WHERE&lt;/code&gt; clause—the &lt;code&gt;WHERE&lt;/code&gt; clause filters rows before any groups or buckets are formed.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Order of Operations:&lt;/strong&gt;&lt;br&gt;
This is the order which SQL executes the parts of a query:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;FROM&lt;/code&gt;&lt;/strong&gt;: Pick the table.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;WHERE&lt;/code&gt;&lt;/strong&gt;: Filter &lt;strong&gt;individual rows&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;GROUP BY&lt;/code&gt;&lt;/strong&gt;: Group the remaining rows into buckets.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;HAVING&lt;/code&gt;&lt;/strong&gt;: Filter the &lt;strong&gt;grouped buckets&lt;/strong&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;SELECT&lt;/code&gt;&lt;/strong&gt;: Calculate expressions and aggregates (like &lt;code&gt;SUM()&lt;/code&gt;).&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Because &lt;code&gt;WHERE&lt;/code&gt; runs at step 2, the database is examining rows one by one. At that moment, no groups exist yet, and no sums have been calculated.&lt;/p&gt;

&lt;p&gt;If you try to write something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;transactions&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;  &lt;span class="c1"&gt;-- ERROR!&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The database will throw an error: &lt;code&gt;misuse of aggregate function SUM()&lt;/code&gt;. It cannot check whether &lt;code&gt;SUM(amount) &amp;gt; 0&lt;/code&gt; during the &lt;code&gt;WHERE&lt;/code&gt; step because it hasn't grouped the rows or summed them yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  How to filter on aggregated results
&lt;/h3&gt;

&lt;p&gt;If you want to filter based on an aggregate, SQL provides the &lt;strong&gt;&lt;code&gt;HAVING&lt;/code&gt;&lt;/strong&gt; clause, which runs &lt;em&gt;after&lt;/em&gt; grouping and aggregating (step 4):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;balance&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;transactions&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;was_successful&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;   &lt;span class="c1"&gt;-- Filters individual rows FIRST&lt;/span&gt;
&lt;span class="k"&gt;GROUP&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;          &lt;span class="c1"&gt;-- Groups rows into buckets SECOND&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;-- Filters the grouped buckets THIRD&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In short:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;WHERE&lt;/code&gt;&lt;/strong&gt; filters rows &lt;em&gt;before&lt;/em&gt; aggregation.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;&lt;code&gt;HAVING&lt;/code&gt;&lt;/strong&gt; filters groups &lt;em&gt;after&lt;/em&gt; aggregation.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>sql</category>
      <category>backend</category>
      <category>machinelearning</category>
      <category>backenddevelopment</category>
    </item>
  </channel>
</rss>
