<?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: Arpit Bangre</title>
    <description>The latest articles on DEV Community by Arpit Bangre (@arpitmbangre).</description>
    <link>https://dev.to/arpitmbangre</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%2F4098480%2Fe1af65b9-1b84-4dd0-89b0-40a4e0775032.png</url>
      <title>DEV Community: Arpit Bangre</title>
      <link>https://dev.to/arpitmbangre</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/arpitmbangre"/>
    <language>en</language>
    <item>
      <title>SQL Execution Order Internals: Why WHERE Fails on Aliases but ORDER BY Succeeds</title>
      <dc:creator>Arpit Bangre</dc:creator>
      <pubDate>Fri, 28 Aug 2026 07:08:52 +0000</pubDate>
      <link>https://dev.to/arpitmbangre/sql-execution-order-internals-why-where-fails-on-aliases-but-order-by-succeeds-1774</link>
      <guid>https://dev.to/arpitmbangre/sql-execution-order-internals-why-where-fails-on-aliases-but-order-by-succeeds-1774</guid>
      <description>&lt;p&gt;Ever wondered why this query fails in SQL?&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;department_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&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;emp_count&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;emp_count&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="c1"&gt;-- ❌ Error: Invalid column name 'emp_count'&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;department_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The 6-Stage Execution Engine:
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;FROM &amp;amp; JOIN&lt;/strong&gt; — Load source tables &amp;amp; evaluate join conditions&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;WHERE&lt;/strong&gt; — Filter raw rows before grouping&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GROUP BY&lt;/strong&gt; — Aggregate rows into buckets&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;HAVING&lt;/strong&gt; — Filter aggregated buckets&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;SELECT&lt;/strong&gt; — Compute expressions &amp;amp; column aliases&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ORDER BY&lt;/strong&gt; — Sort final output&lt;/li&gt;
&lt;/ol&gt;




&lt;h3&gt;
  
  
  Why It Fails:
&lt;/h3&gt;

&lt;p&gt;Because &lt;strong&gt;WHERE&lt;/strong&gt; executes at &lt;strong&gt;Stage 2&lt;/strong&gt;, the &lt;code&gt;emp_count&lt;/code&gt; alias created at &lt;strong&gt;Stage 5 (SELECT)&lt;/strong&gt; does not exist in memory yet!&lt;/p&gt;

&lt;p&gt;However, &lt;strong&gt;ORDER BY&lt;/strong&gt; runs at &lt;strong&gt;Stage 6 (after SELECT)&lt;/strong&gt;, which is why &lt;code&gt;ORDER BY emp_count DESC&lt;/code&gt; works seamlessly.&lt;/p&gt;




&lt;h3&gt;
  
  
  How to Fix:
&lt;/h3&gt;

&lt;p&gt;Use &lt;strong&gt;HAVING&lt;/strong&gt; for aggregated filtering:&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;department_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&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;emp_count&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;employees&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;department_id&lt;/span&gt;
&lt;span class="k"&gt;HAVING&lt;/span&gt; &lt;span class="k"&gt;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&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;5&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;p&gt;💡 &lt;em&gt;What's your favorite SQL execution order quirk? Drop your thoughts below!&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;💼 &lt;em&gt;Let's connect:&lt;/em&gt; &lt;a href="https://www.linkedin.com/in/arpitmbangre/" rel="noopener noreferrer"&gt;linkedin.com/in/arpitmbangre&lt;/a&gt;&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>dataengineering</category>
      <category>backend</category>
    </item>
  </channel>
</rss>
