<?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: Saami abbas Khan</title>
    <description>The latest articles on DEV Community by Saami abbas Khan (@saamiabbaskhan).</description>
    <link>https://dev.to/saamiabbaskhan</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%2F2069606%2Fb35d1209-61fd-4120-ae9b-f4e76801e6f4.png</url>
      <title>DEV Community: Saami abbas Khan</title>
      <link>https://dev.to/saamiabbaskhan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/saamiabbaskhan"/>
    <language>en</language>
    <item>
      <title>Confused by UNBOUNDED PRECEDING and CURRENT ROW in SQL window functions? I was too. This guide breaks down exactly how window frames work and uses LeetCode 1204 to show how they create a running total step by step.</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Sun, 23 Aug 2026 12:34:06 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/confused-by-unbounded-preceding-and-current-row-in-sql-window-functions-i-was-too-this-guide-5528</link>
      <guid>https://dev.to/saamiabbaskhan/confused-by-unbounded-preceding-and-current-row-in-sql-window-functions-i-was-too-this-guide-5528</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/saamiabbaskhan/sql-window-frames-explained-how-unbounded-preceding-creates-a-running-total-m21" class="crayons-story__hidden-navigation-link"&gt;SQL Window Frames Explained: How UNBOUNDED PRECEDING Creates a Running Total&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/saamiabbaskhan" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2069606%2Fb35d1209-61fd-4120-ae9b-f4e76801e6f4.png" alt="saamiabbaskhan profile" class="crayons-avatar__image"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/saamiabbaskhan" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Saami abbas Khan
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Saami abbas Khan
                
                
              
              &lt;div id="story-author-preview-content-4467750" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/saamiabbaskhan" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2069606%2Fb35d1209-61fd-4120-ae9b-f4e76801e6f4.png" class="crayons-avatar__image" alt=""&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Saami abbas Khan&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/saamiabbaskhan/sql-window-frames-explained-how-unbounded-preceding-creates-a-running-total-m21" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Aug 23&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/saamiabbaskhan/sql-window-frames-explained-how-unbounded-preceding-creates-a-running-total-m21" id="article-link-4467750"&gt;
          SQL Window Frames Explained: How UNBOUNDED PRECEDING Creates a Running Total
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/sql"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;sql&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/database"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;database&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/beginners"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;beginners&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/mysql"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;mysql&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/saamiabbaskhan/sql-window-frames-explained-how-unbounded-preceding-creates-a-running-total-m21" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/exploding-head-daceb38d627e6ae9b730f36a1e390fca556a4289d5a41abb2c35068ad3e2c4b5.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="18" height="18"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;6&lt;span class="hidden s:inline"&gt;&amp;nbsp;reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/saamiabbaskhan/sql-window-frames-explained-how-unbounded-preceding-creates-a-running-total-m21#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              &lt;span class="hidden s:inline"&gt;Add&amp;nbsp;Comment&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            9 min read
          &lt;/small&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>beginners</category>
      <category>database</category>
      <category>sql</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>SQL Window Frames Explained: How UNBOUNDED PRECEDING Creates a Running Total</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Sun, 23 Aug 2026 12:30:12 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/sql-window-frames-explained-how-unbounded-preceding-creates-a-running-total-m21</link>
      <guid>https://dev.to/saamiabbaskhan/sql-window-frames-explained-how-unbounded-preceding-creates-a-running-total-m21</guid>
      <description>&lt;h3&gt;
  
  
  If you have started learning SQL window functions, you have probably seen something like this:
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt;
    &lt;span class="k"&gt;RANGE&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first glance, the syntax looks intimidating.&lt;/p&gt;

&lt;p&gt;What exactly is a &lt;strong&gt;frame&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;What does &lt;code&gt;UNBOUNDED PRECEDING&lt;/code&gt; mean?&lt;/p&gt;

&lt;p&gt;Why does &lt;code&gt;CURRENT ROW&lt;/code&gt; not mean that the calculation only considers the current row?&lt;/p&gt;

&lt;p&gt;And how does this produce:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10
30
60
100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;instead of just giving the total &lt;code&gt;100&lt;/code&gt; everywhere?&lt;/p&gt;

&lt;p&gt;I had the same confusion, so let's break it down from first principles and then use &lt;strong&gt;LeetCode 1204 — Last Person to Fit in the Bus&lt;/strong&gt; as a practical example.&lt;/p&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;1. First: What Is a Window Function?&lt;/li&gt;
&lt;li&gt;2. So What Is a Window?&lt;/li&gt;
&lt;li&gt;3. What Is a Window Frame?&lt;/li&gt;
&lt;li&gt;4. Understanding &lt;code&gt;UNBOUNDED PRECEDING&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;5. Understanding &lt;code&gt;CURRENT ROW&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;6. Putting It Together&lt;/li&gt;
&lt;li&gt;7. Why Doesn't &lt;code&gt;CURRENT ROW&lt;/code&gt; Limit the Scope?&lt;/li&gt;
&lt;li&gt;8. &lt;code&gt;ROWS&lt;/code&gt; vs &lt;code&gt;RANGE&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;9. What Happens If We Don't Write the Frame?&lt;/li&gt;
&lt;li&gt;10. Not Every Window Function Uses the Frame&lt;/li&gt;
&lt;li&gt;11. A Real Problem: LeetCode 1204&lt;/li&gt;
&lt;li&gt;12. Building the Running Total&lt;/li&gt;
&lt;li&gt;13. Remove Anyone Who Exceeds the Limit&lt;/li&gt;
&lt;li&gt;14. Using &lt;code&gt;FIRST_VALUE()&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;15. Complete Solution&lt;/li&gt;
&lt;li&gt;16. The Mental Model I Use for Window Frames&lt;/li&gt;
&lt;li&gt;17. Other Useful Window Frames&lt;/li&gt;
&lt;li&gt;18. Final Takeaway&lt;/li&gt;
&lt;li&gt;Quick Reference&lt;/li&gt;
&lt;li&gt;References&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  1. First: What Is a Window Function?
&lt;/h3&gt;

&lt;p&gt;A normal aggregate function such as:&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;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;combines multiple rows into a single result.&lt;/p&gt;

&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;weight
------
10
20
30
40
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A normal &lt;code&gt;SUM()&lt;/code&gt; gives:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The individual rows are no longer represented in the result of that aggregation.&lt;/p&gt;

&lt;p&gt;A &lt;strong&gt;window function&lt;/strong&gt; is different.&lt;/p&gt;

&lt;p&gt;When we write:&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;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SQL calculates a sum using a set of related rows, but &lt;strong&gt;keeps the original rows&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;So instead of:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we can get something like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10
30
60
100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is the first important idea:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A window function performs a calculation across related rows without collapsing those rows into one row.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  2. So What Is a Window?
&lt;/h2&gt;

&lt;p&gt;Consider:&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;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;There are several pieces here.&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;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This tells SQL &lt;strong&gt;what calculation&lt;/strong&gt; to perform.&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="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(...)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;turns the calculation into a window function.&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;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;tells SQL how the rows should be ordered inside that window.&lt;/p&gt;

&lt;p&gt;But there is one more concept:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Which rows should actually be included for the current row?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is where the &lt;strong&gt;window frame&lt;/strong&gt; comes in.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. What Is a Window Frame?
&lt;/h2&gt;

&lt;p&gt;A frame is a subset of the rows in the current window/partition that the function operates on for the &lt;strong&gt;current row&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Think of it as a moving boundary.&lt;/p&gt;

&lt;p&gt;For example, suppose we have:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;turn&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If we want a running total, we want the frame to behave like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;For turn 1:

[10]


For turn 2:

[10, 20]


For turn 3:

[10, 20, 30]


For turn 4:

[10, 20, 30, 40]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Therefore the results are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;10
30
60
100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frame is what lets us describe this behavior.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Understanding UNBOUNDED PRECEDING
&lt;/h2&gt;

&lt;p&gt;Now let's look at:&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="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Start at the first row of the partition.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It does not mean "some extremely large number of rows before the current row."&lt;/p&gt;

&lt;p&gt;It literally means that the frame begins at the beginning of the partition.&lt;/p&gt;

&lt;p&gt;So:&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="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is essentially saying:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start here
↓
[ FIRST ROW ------------------------ CURRENT ROW ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For our example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Turn 1:
[10]

Turn 2:
[10, 20]

Turn 3:
[10, 20, 30]

Turn 4:
[10, 20, 30, 40]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  5. Understanding CURRENT ROW
&lt;/h2&gt;

&lt;p&gt;Now comes the part that initially seems confusing.&lt;/p&gt;

&lt;p&gt;We have:&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;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Does that mean the frame contains &lt;strong&gt;only the current row&lt;/strong&gt;?&lt;/p&gt;

&lt;p&gt;No.&lt;/p&gt;

&lt;p&gt;It means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The end boundary of the frame is the current row.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So when we write:&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;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Start at the beginning and stop at the current row.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Therefore:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Current row = 1

[1]


Current row = 2

[1, 2]


Current row = 3

[1, 2, 3]


Current row = 4

[1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's why it creates a running total.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. Putting It Together
&lt;/h2&gt;

&lt;p&gt;Now the entire expression:&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;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt;
    &lt;span class="k"&gt;RANGE&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can be translated into plain English as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;Order the rows by &lt;code&gt;turn&lt;/code&gt;, and for each row, sum everything from the beginning of the window up to the current row.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;For:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;turn&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;the calculation is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Turn 1:
10
= 10


Turn 2:
10 + 20
= 30


Turn 3:
10 + 20 + 30
= 60


Turn 4:
10 + 20 + 30 + 40
= 100
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Result:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;turn&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;th&gt;running_total&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;40&lt;/td&gt;
&lt;td&gt;100&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;That is a &lt;strong&gt;running total&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Why Doesn't CURRENT ROW Limit the Scope?
&lt;/h2&gt;

&lt;p&gt;This is the most common source of confusion.&lt;/p&gt;

&lt;p&gt;When you see:&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;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;don't read it as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"Use the current row."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Read it as:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;"The frame starts at the beginning and its &lt;strong&gt;ending boundary&lt;/strong&gt; is the current row."&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The frame is therefore different for every row.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Row 1:
[1]


Row 2:
[1, 2]


Row 3:
[1, 2, 3]


Row 4:
[1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;current row moves&lt;/strong&gt;, and the frame moves with it.&lt;/p&gt;

&lt;p&gt;That's the key mental model:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A window frame is evaluated relative to each current row.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  8. ROWS vs RANGE
&lt;/h2&gt;

&lt;p&gt;There are two frame units that are particularly important in MySQL:&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;ROWS&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and&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;RANGE&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;They can look similar, but they are conceptually different.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;ROWS&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;&lt;code&gt;ROWS&lt;/code&gt; works with &lt;strong&gt;physical row positions&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;For 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;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Include every physical row from the first row through the current row.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h4&gt;
  
  
  &lt;code&gt;RANGE&lt;/code&gt;
&lt;/h4&gt;

&lt;p&gt;&lt;code&gt;RANGE&lt;/code&gt; works with &lt;strong&gt;values in the window ordering&lt;/strong&gt;, so rows that are peers according to the &lt;code&gt;ORDER BY&lt;/code&gt; can belong to the same frame boundary.&lt;/p&gt;

&lt;p&gt;For example, if we have:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;turn&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;10&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;20&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;30&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;then with:&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;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt;
&lt;span class="k"&gt;RANGE&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;the two rows with &lt;code&gt;turn = 1&lt;/code&gt; are peers.&lt;/p&gt;

&lt;p&gt;So the frame for the &lt;code&gt;turn = 1&lt;/code&gt; rows includes both of them.&lt;/p&gt;

&lt;p&gt;This distinction matters whenever your &lt;code&gt;ORDER BY&lt;/code&gt; column contains duplicate values.&lt;/p&gt;

&lt;p&gt;For a simple row-by-row running total where the ordering column is unique, &lt;code&gt;ROWS&lt;/code&gt; is often the clearest way to express the intention:&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;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  9. What Happens If We Don't Write the Frame?
&lt;/h2&gt;

&lt;p&gt;This is another important point.&lt;/p&gt;

&lt;p&gt;In MySQL, when an &lt;code&gt;ORDER BY&lt;/code&gt; is present and no explicit frame is specified, the default frame is equivalent to:&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;RANGE&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So:&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;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;is effectively using that default frame.&lt;/p&gt;

&lt;p&gt;However, writing the frame explicitly can be useful when you're learning or when you want the query to communicate exactly which rows should be included.&lt;/p&gt;

&lt;p&gt;It also makes the distinction between &lt;code&gt;RANGE&lt;/code&gt; and &lt;code&gt;ROWS&lt;/code&gt; explicit.&lt;/p&gt;




&lt;h2&gt;
  
  
  10. Not Every Window Function Uses the Frame
&lt;/h2&gt;

&lt;p&gt;This is an extremely important distinction.&lt;/p&gt;

&lt;p&gt;Functions such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SUM()
AVG()
FIRST_VALUE()
LAST_VALUE()
NTH_VALUE()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;can operate on the rows in the current frame.&lt;/p&gt;

&lt;p&gt;But functions such as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ROW_NUMBER()
RANK()
DENSE_RANK()
LAG()
LEAD()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;do not use the frame in the same way.&lt;/p&gt;

&lt;p&gt;For 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="n"&gt;ROW_NUMBER&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt;
    &lt;span class="k"&gt;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;does not suddenly make &lt;code&gt;ROW_NUMBER()&lt;/code&gt; calculate a running count based on that frame.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;ROW_NUMBER()&lt;/code&gt; is concerned with the position of the current row in the ordered partition.&lt;/p&gt;

&lt;p&gt;This is why it is useful to distinguish between:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;window ordering&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;and&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;window framing&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;They are related, but they are not the same concept.&lt;/p&gt;




&lt;h2&gt;
  
  
  11. A Real Problem: LeetCode 1204
&lt;/h2&gt;

&lt;p&gt;Now let's use this idea in an actual SQL problem.&lt;/p&gt;

&lt;p&gt;The problem gives us people waiting to enter a bus.&lt;/p&gt;

&lt;p&gt;Each person has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;code&gt;person_name&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;weight&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;turn&lt;/code&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The bus can carry a maximum weight of &lt;code&gt;1000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We need to find:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;The last person who can get on the bus without making the total weight exceed &lt;code&gt;1000&lt;/code&gt;.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The queue order is determined by &lt;code&gt;turn&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;So the first thing we need is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Person 1
Person 1 + Person 2
Person 1 + Person 2 + Person 3
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In other words:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;We need a running total.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  12. Building the Running Total
&lt;/h2&gt;

&lt;p&gt;This is the key part of the solution:&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;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;
    &lt;span class="k"&gt;RANGE&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;sum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Suppose the queue is:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;person_name&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;th&gt;turn&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;250&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;300&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;400&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;David&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The window frame produces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Turn 1:
[250]
→ 250


Turn 2:
[250, 300]
→ 550


Turn 3:
[250, 300, 400]
→ 950


Turn 4:
[250, 300, 400, 200]
→ 1150
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So our derived table becomes:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;person_name&lt;/th&gt;
&lt;th&gt;weight&lt;/th&gt;
&lt;th&gt;turn&lt;/th&gt;
&lt;th&gt;sum&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;250&lt;/td&gt;
&lt;td&gt;1&lt;/td&gt;
&lt;td&gt;250&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;300&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;550&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;400&lt;/td&gt;
&lt;td&gt;3&lt;/td&gt;
&lt;td&gt;950&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;David&lt;/td&gt;
&lt;td&gt;200&lt;/td&gt;
&lt;td&gt;4&lt;/td&gt;
&lt;td&gt;1150&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Now the problem is much easier.&lt;/p&gt;




&lt;h2&gt;
  
  
  13. Remove Anyone Who Exceeds the Limit
&lt;/h2&gt;

&lt;p&gt;We can simply use:&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;WHERE&lt;/span&gt; &lt;span class="k"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This leaves:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;person_name&lt;/th&gt;
&lt;th&gt;sum&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Alice&lt;/td&gt;
&lt;td&gt;250&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Bob&lt;/td&gt;
&lt;td&gt;550&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Charlie&lt;/td&gt;
&lt;td&gt;950&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;David is excluded because:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1150 &amp;gt; 1000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now the answer must be the person with the &lt;strong&gt;largest remaining cumulative total&lt;/strong&gt;.&lt;/p&gt;




&lt;h2&gt;
  
  
  14. Using FIRST VALUE
&lt;/h2&gt;

&lt;p&gt;This is where I use:&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="n"&gt;FIRST_VALUE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;sum&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After filtering, the rows are ordered by cumulative weight:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Charlie → 950
Bob     → 550
Alice   → 250
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Therefore, the first value is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Charlie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;FIRST_VALUE()&lt;/code&gt; returns that first value from the current frame.&lt;/p&gt;

&lt;p&gt;Since the first value is the same for all the remaining rows, the intermediate result is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Charlie
Charlie
Charlie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So I use:&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;DISTINCT&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to get:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Charlie
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  15. Complete Solution
&lt;/h2&gt;

&lt;p&gt;Here is the complete query:&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="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;FIRST_VALUE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;person_name&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="k"&gt;sum&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="s1"&gt;'person_name'&lt;/span&gt;

&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;

    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; 

        &lt;span class="k"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt; 
            &lt;span class="k"&gt;RANGE&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
        &lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="s1"&gt;'sum'&lt;/span&gt;

    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Queue&lt;/span&gt;

&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt; 

&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;sum&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The solution can be viewed as four steps:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Queue
   ↓
Calculate running total
   ↓
Remove totals &amp;gt; 1000
   ↓
Find the person with the largest valid total
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  16. The Mental Model I Use for Window Frames
&lt;/h2&gt;

&lt;p&gt;When I see:&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;ROWS&lt;/span&gt;&lt;span class="o"&gt;/&lt;/span&gt;&lt;span class="k"&gt;RANGE&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="p"&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I try not to memorize the syntax.&lt;/p&gt;

&lt;p&gt;Instead, I ask two questions:&lt;/p&gt;

&lt;h3&gt;
  
  
  Where does the frame start?
&lt;/h3&gt;

&lt;p&gt;For 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="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Start from the beginning.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Where does the frame end?
&lt;/h3&gt;

&lt;p&gt;For 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;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;means:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Stop at the current row.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So:&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;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;becomes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;START
 ↓
[---------------- CURRENT ROW]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As the current row changes:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Row 1:
[1]


Row 2:
[1, 2]


Row 3:
[1, 2, 3]


Row 4:
[1, 2, 3, 4]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That's the mental model.&lt;/p&gt;

&lt;p&gt;Once you visualize the frame, the syntax becomes much less intimidating.&lt;/p&gt;




&lt;h2&gt;
  
  
  17. Other Useful Window Frames
&lt;/h2&gt;

&lt;p&gt;The same idea can be used for many other problems.&lt;/p&gt;

&lt;h3&gt;
  
  
  Running total
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;1
1 + 2
1 + 2 + 3
1 + 2 + 3 + 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Moving 3-row average
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Row 1 → [1]
Row 2 → [1, 2]
Row 3 → [1, 2, 3]
Row 4 → [2, 3, 4]
Row 5 → [3, 4, 5]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is useful for rolling/moving calculations.&lt;/p&gt;

&lt;h4&gt;
  
  
  Entire partition
&lt;/h4&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;ROWS&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;FOLLOWING&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The frame contains the entire partition for every row.&lt;/p&gt;




&lt;h2&gt;
  
  
  18. Final Takeaway
&lt;/h2&gt;

&lt;p&gt;The biggest thing I learned from window frames is that:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;&lt;code&gt;CURRENT ROW&lt;/code&gt; is a boundary, not a restriction to a single row.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;When we write:&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;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;we are saying:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Start from the beginning of the partition and extend the frame through the current row.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is why:&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;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;weight&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;turn&lt;/span&gt;
    &lt;span class="k"&gt;RANGE&lt;/span&gt; &lt;span class="k"&gt;BETWEEN&lt;/span&gt; &lt;span class="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="k"&gt;CURRENT&lt;/span&gt; &lt;span class="k"&gt;ROW&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;creates a running total.&lt;/p&gt;

&lt;p&gt;The frame changes for every current row:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;[1]
[1, 2]
[1, 2, 3]
[1, 2, 3, 4]
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once this mental model clicks, many window-function problems become much easier to reason about.&lt;/p&gt;




&lt;h3&gt;
  
  
  Quick Reference
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Syntax&lt;/th&gt;
&lt;th&gt;Meaning&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;UNBOUNDED PRECEDING&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Start at the first row&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;CURRENT ROW&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;End at the current row&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;UNBOUNDED FOLLOWING&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Extend to the last row&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ROWS&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Frame based on physical row positions&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;RANGE&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Frame based on ordering values and their peers&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;code&gt;ROWS/RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW&lt;/code&gt;&lt;/td&gt;
&lt;td&gt;Running/cumulative frame&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;If you're learning SQL window functions, don't just memorize:&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="n"&gt;UNBOUNDED&lt;/span&gt; &lt;span class="k"&gt;PRECEDING&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Visualize the frame moving through the partition.&lt;/p&gt;

&lt;p&gt;That is the part that makes the syntax finally make sense.&lt;/p&gt;




&lt;h3&gt;
  
  
  References
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;&lt;a href="https://dev.mysql.com/doc/refman/8.0/en/window-functions-frames.html" rel="noopener noreferrer"&gt;MySQL 8.0 Reference Manual — Window Function Frame Specification&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;&lt;a href="https://dev.mysql.com/doc/refman/8.0/en/window-function-descriptions.html" rel="noopener noreferrer"&gt;MySQL 8.0 Reference Manual — Window Function Descriptions&lt;/a&gt;&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>beginners</category>
      <category>mysql</category>
    </item>
    <item>
      <title>Correlated Subqueries Explained: From Confusing to Intuitive</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Sat, 01 Aug 2026 11:16:22 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/correlated-subqueries-explained-from-confusing-to-intuitive-32jn</link>
      <guid>https://dev.to/saamiabbaskhan/correlated-subqueries-explained-from-confusing-to-intuitive-32jn</guid>
      <description>&lt;p&gt;Hey everyone! 👋&lt;/p&gt;

&lt;p&gt;It's been a while since I posted here. Over the past few days, I've been revisiting SQL from the ground up—not just solving problems, but understanding &lt;strong&gt;why&lt;/strong&gt; different SQL concepts exist and &lt;strong&gt;when&lt;/strong&gt; they should be used.&lt;/p&gt;

&lt;p&gt;One topic that confused me (and many beginners) was &lt;strong&gt;correlated subqueries&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;You may have seen queries 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="p"&gt;...&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;table1&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="k"&gt;EXISTS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;table2&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;t2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;id&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;and wondered:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;How is the inner query accessing columns from the outer query?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;When does the inner query execute?&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;How is this different from a normal subquery?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In this article, we'll answer all of these questions by solving a real SQL problem from &lt;strong&gt;LeetCode&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;By the end, you'll understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;✅ What correlated subqueries are&lt;/li&gt;
&lt;li&gt;✅ How they work internally&lt;/li&gt;
&lt;li&gt;✅ SQL's logical execution order&lt;/li&gt;
&lt;li&gt;✅ When to use them&lt;/li&gt;
&lt;li&gt;✅ How they compare with window functions&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's dive in!&lt;/p&gt;




&lt;blockquote&gt;
&lt;h2&gt;
  
  
  Prerequisites
&lt;/h2&gt;

&lt;p&gt;This article assumes you know:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basic &lt;code&gt;SELECT&lt;/code&gt; statements&lt;/li&gt;
&lt;li&gt;&lt;code&gt;WHERE&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;GROUP BY&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;Aggregate functions (&lt;code&gt;MIN&lt;/code&gt;, &lt;code&gt;MAX&lt;/code&gt;, &lt;code&gt;COUNT&lt;/code&gt;, ...)&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you're comfortable with these, you're ready to learn correlated subqueries.&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  Table of Contents
&lt;/h2&gt;

&lt;ol&gt;
&lt;li&gt;Why Are Correlated Subqueries So Confusing?&lt;/li&gt;
&lt;li&gt;Correlated Subqueries from Scratch&lt;/li&gt;
&lt;li&gt;Correlated vs Non-Correlated Subqueries&lt;/li&gt;
&lt;li&gt;SQL's Logical Execution Order (and Where Correlated Subqueries Fit)&lt;/li&gt;
&lt;li&gt;Using LeetCode 3421 as Our Running Example&lt;/li&gt;
&lt;li&gt;Before We Write SQL...&lt;/li&gt;
&lt;li&gt;My Fully Annotated Solution&lt;/li&gt;
&lt;li&gt;Walking Through the Query Step by Step Using Real Data&lt;/li&gt;
&lt;li&gt;Performance Discussion: Correlated Subqueries vs Window Functions&lt;/li&gt;
&lt;li&gt;Conclusion &amp;amp; Practice Problems&lt;/li&gt;
&lt;/ol&gt;




&lt;h2 id="why-confusing"&gt;1. Why Are Correlated Subqueries So Confusing?&lt;/h2&gt;

&lt;p&gt;When I first started learning SQL, correlated subqueries felt like magic.&lt;/p&gt;

&lt;p&gt;Questions like these constantly came to mind:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How can the inner query access columns from the outer query?&lt;/li&gt;
&lt;li&gt;When does the inner query execute?&lt;/li&gt;
&lt;li&gt;Does it run once or multiple times?&lt;/li&gt;
&lt;li&gt;Why can't I execute it by itself?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you've asked yourself any of these questions, don't worry—you'll have answers to all of them by the end of this article.&lt;/p&gt;




&lt;h2 id="correlated-subqueries-from-scratch"&gt;2. Correlated Subqueries from Scratch&lt;/h2&gt;

&lt;h3&gt;
  
  
  What is a subquery?
&lt;/h3&gt;

&lt;p&gt;A subquery is just a &lt;code&gt;SELECT&lt;/code&gt; statement nested inside another SQL statement. Nothing scary yet:&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;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salary&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;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&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;Employees&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here, the inner query &lt;code&gt;(SELECT AVG(salary) FROM Employees)&lt;/code&gt; runs &lt;strong&gt;once&lt;/strong&gt;, produces a single number (say, &lt;code&gt;55000&lt;/code&gt;), and the outer query just uses that number. The inner query doesn't care what row the outer query is currently looking at. This is a &lt;strong&gt;non-correlated&lt;/strong&gt; (or "simple") subquery — it's fully independent and could be run on its own.&lt;/p&gt;

&lt;h3&gt;
  
  
  What makes a subquery "correlated"?
&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;Key Idea&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A correlated subquery depends on values from the outer query.&lt;/p&gt;

&lt;p&gt;Because of that, it cannot execute independently — it re-executes once per row of the outer query, using that row's values each time.&lt;br&gt;
&lt;/p&gt;
&lt;/blockquote&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_id&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&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;Employees&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;
    &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_id&lt;/span&gt;   &lt;span class="c1"&gt;-- 👈 references outer row `e`&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Read that inner query in isolation:&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="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&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;Employees&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;e.department_id&lt;/code&gt; doesn't exist inside this subquery's own &lt;code&gt;FROM Employees e2&lt;/code&gt; — it's borrowed from the &lt;em&gt;outer&lt;/em&gt; query. That's the defining trait. Mentally, think of it like a &lt;strong&gt;function with a parameter&lt;/strong&gt;:&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="n"&gt;find_avg_salary_for&lt;/span&gt;&lt;span class="p"&gt;(&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;return&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;department_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And SQL calls that "function" once for every single row the outer query touches, plugging in that row's &lt;code&gt;department_id&lt;/code&gt;. That's the whole concept. Everything else is just decoration on top of this idea.&lt;/p&gt;

&lt;h3&gt;
  
  
  A tiny mental model
&lt;/h3&gt;

&lt;p&gt;Think of the outer query as a &lt;code&gt;for&lt;/code&gt; loop, and the correlated subquery as code that runs &lt;em&gt;inside&lt;/em&gt; that loop, using the loop variable:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight python"&gt;&lt;code&gt;&lt;span class="c1"&gt;# This is NOT real SQL — just a mental model
&lt;/span&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt; &lt;span class="ow"&gt;in&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
    &lt;span class="n"&gt;dept_avg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nc"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="n"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;department_id&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&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;if&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;dept_avg&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;
        &lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;row&lt;/span&gt;&lt;span class="p"&gt;.&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;p&gt;This mental model is &lt;em&gt;exactly&lt;/em&gt; why correlated subqueries can get expensive — because that's genuinely close to how the database may end up executing it if it can't optimize it away (more on that in the performance section).&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;A correlated subquery executes once for every row (or group) produced by the outer query, using that row's values as its input.&lt;/p&gt;




&lt;h2 id="correlated-vs-non-correlated"&gt;3. Correlated vs Non-Correlated: Side by Side&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;Non-Correlated Subquery&lt;/th&gt;
&lt;th&gt;Correlated Subquery&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;References outer query?&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Can run standalone?&lt;/td&gt;
&lt;td&gt;✅ Yes&lt;/td&gt;
&lt;td&gt;❌ No&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Execution frequency&lt;/td&gt;
&lt;td&gt;Once, total&lt;/td&gt;
&lt;td&gt;Once &lt;strong&gt;per outer row&lt;/strong&gt; (conceptually)&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Typical use&lt;/td&gt;
&lt;td&gt;Compare against a global value (e.g. overall average)&lt;/td&gt;
&lt;td&gt;Compare against a value scoped to &lt;em&gt;that row's group&lt;/em&gt; (e.g. that department's average, that student's max date)&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Non-correlated example&lt;/strong&gt; — "Employees who earn more than the company-wide average":&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;name&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;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&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;Employees&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Correlated example&lt;/strong&gt; — "Employees who earn more than &lt;em&gt;their own department's&lt;/em&gt; average":&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;name&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;salary&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;AVG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;salary&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;Employees&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;e2&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;department_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&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;p&gt;Same shape, one crucial difference: the &lt;code&gt;WHERE e2.department_id = e.department_id&lt;/code&gt; line. That one line is the entire difference between "global" and "per-row-scoped."&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;If the subquery can run on its own and return a sensible result, it's non-correlated. If it needs a value from the outer row to even make sense, it's correlated.&lt;/p&gt;




&lt;h2 id="execution-order"&gt;4. SQL's Logical Execution Order (and Where Subqueries Fit)&lt;/h2&gt;

&lt;p&gt;This trips up a lot of people, so let's nail it down. A &lt;code&gt;SELECT&lt;/code&gt; statement is &lt;em&gt;written&lt;/em&gt; top to bottom (&lt;code&gt;SELECT → FROM → WHERE → GROUP BY → ...&lt;/code&gt;), but it does &lt;strong&gt;not execute&lt;/strong&gt; in that order. The logical order MySQL (and most SQL engines) actually evaluates is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;① FROM        (and JOINs) — build the working row set

↓

② WHERE       — filter individual rows

↓

③ GROUP BY    — bucket rows into groups

↓

④ HAVING      — filter groups

↓

⑤ SELECT      — compute the output columns
                (this is where subqueries in the SELECT list run)

↓

⑥ ORDER BY    — sort the final result

↓

⑦ LIMIT       — trim the result
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Correlated subqueries that live &lt;strong&gt;inside the &lt;code&gt;SELECT&lt;/code&gt; list&lt;/strong&gt; (like the ones in my solution below) are evaluated at &lt;strong&gt;step ⑤&lt;/strong&gt; — for every row/group that survives &lt;code&gt;FROM → WHERE → GROUP BY → HAVING&lt;/code&gt;. This is the key insight for our problem:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;Key Idea&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;By the time the correlated subqueries in &lt;code&gt;SELECT&lt;/code&gt; run, MySQL has already figured out &lt;strong&gt;which&lt;/strong&gt; &lt;code&gt;(student_id, subject)&lt;/code&gt; groups exist (thanks to &lt;code&gt;GROUP BY&lt;/code&gt;). The subqueries then just get &lt;em&gt;asked&lt;/em&gt;, once per group: "for this specific student and subject, what's the first score, and what's the latest score?"&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That's it. The whole query is really just: &lt;strong&gt;1) group by student+subject, 2) for each group, ask two side-questions.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Correlated subqueries in the &lt;code&gt;SELECT&lt;/code&gt; list run at step ⑤ — once per group that has already survived filtering and grouping.&lt;/p&gt;




&lt;h2 id="the-problem"&gt;5. LeetCode 3421 as Our Running Example: Find Students Who Improved&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Table: &lt;code&gt;Scores&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Column Name&lt;/th&gt;
&lt;th&gt;Type&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;student_id&lt;/td&gt;
&lt;td&gt;int&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;subject&lt;/td&gt;
&lt;td&gt;varchar&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;score&lt;/td&gt;
&lt;td&gt;int&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;exam_date&lt;/td&gt;
&lt;td&gt;varchar&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;code&gt;(student_id, subject, exam_date)&lt;/code&gt; is the primary key. Each row is one student's score in one subject on one exam date. &lt;code&gt;score&lt;/code&gt; is between 0 and 100.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Find students who have &lt;em&gt;improved&lt;/em&gt;. A student counts as improved if, for a given subject, both of these are true:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;They took the exam in that subject on &lt;strong&gt;at least two different dates&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Their &lt;strong&gt;latest&lt;/strong&gt; score in that subject is &lt;strong&gt;higher&lt;/strong&gt; than their &lt;strong&gt;first&lt;/strong&gt; score&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Return &lt;code&gt;student_id, subject, first_score, latest_score&lt;/code&gt;, ordered by &lt;code&gt;student_id&lt;/code&gt;, &lt;code&gt;subject&lt;/code&gt; ascending.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example Input:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student_id&lt;/th&gt;
&lt;th&gt;subject&lt;/th&gt;
&lt;th&gt;score&lt;/th&gt;
&lt;th&gt;exam_date&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;Math&lt;/td&gt;
&lt;td&gt;70&lt;/td&gt;
&lt;td&gt;2023-01-15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;Math&lt;/td&gt;
&lt;td&gt;85&lt;/td&gt;
&lt;td&gt;2023-02-15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;Physics&lt;/td&gt;
&lt;td&gt;65&lt;/td&gt;
&lt;td&gt;2023-01-15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;Physics&lt;/td&gt;
&lt;td&gt;60&lt;/td&gt;
&lt;td&gt;2023-02-15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;td&gt;Math&lt;/td&gt;
&lt;td&gt;80&lt;/td&gt;
&lt;td&gt;2023-01-15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;td&gt;Math&lt;/td&gt;
&lt;td&gt;85&lt;/td&gt;
&lt;td&gt;2023-02-15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;103&lt;/td&gt;
&lt;td&gt;Math&lt;/td&gt;
&lt;td&gt;90&lt;/td&gt;
&lt;td&gt;2023-01-15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;104&lt;/td&gt;
&lt;td&gt;Physics&lt;/td&gt;
&lt;td&gt;75&lt;/td&gt;
&lt;td&gt;2023-01-15&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;104&lt;/td&gt;
&lt;td&gt;Physics&lt;/td&gt;
&lt;td&gt;85&lt;/td&gt;
&lt;td&gt;2023-02-15&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Expected Output:&lt;/strong&gt;&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;student_id&lt;/th&gt;
&lt;th&gt;subject&lt;/th&gt;
&lt;th&gt;first_score&lt;/th&gt;
&lt;th&gt;latest_score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;101&lt;/td&gt;
&lt;td&gt;Math&lt;/td&gt;
&lt;td&gt;70&lt;/td&gt;
&lt;td&gt;85&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;102&lt;/td&gt;
&lt;td&gt;Math&lt;/td&gt;
&lt;td&gt;80&lt;/td&gt;
&lt;td&gt;85&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;104&lt;/td&gt;
&lt;td&gt;Physics&lt;/td&gt;
&lt;td&gt;75&lt;/td&gt;
&lt;td&gt;85&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Notice what got filtered out:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;101 / Physics&lt;/strong&gt; — took it twice, but score &lt;em&gt;dropped&lt;/em&gt; (65 → 60). Not an improvement.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;103 / Math&lt;/strong&gt; — only took the exam &lt;strong&gt;once&lt;/strong&gt;. Doesn't satisfy "at least two different dates."&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Simple to state, but notice the core difficulty: for every &lt;code&gt;(student_id, subject)&lt;/code&gt; pair, you need to reach &lt;em&gt;back into the same table&lt;/em&gt; to find that specific group's minimum date and maximum date, and then the scores tied to those dates. This "reach back into the same table, but scoped to the current row's group" is &lt;em&gt;exactly&lt;/em&gt; what a correlated subquery is built for.&lt;/p&gt;




&lt;h2 id="before-we-write-sql"&gt;6. Before We Write SQL...&lt;/h2&gt;

&lt;p&gt;Let's forget SQL for a minute.&lt;/p&gt;

&lt;p&gt;Imagine you're writing this in C++, Java, or Python.&lt;/p&gt;

&lt;p&gt;What would you do? Probably something like this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;for each student

    for each subject

        earliest_exam

        latest_exam

        first_score

        latest_score

        if latest_score &amp;gt; first_score

            print()
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;SQL is simply another way of expressing this algorithm. The &lt;code&gt;GROUP BY&lt;/code&gt; gives you the &lt;code&gt;for each student / for each subject&lt;/code&gt; part, and the correlated subqueries give you the &lt;code&gt;earliest_exam&lt;/code&gt;, &lt;code&gt;latest_exam&lt;/code&gt;, &lt;code&gt;first_score&lt;/code&gt;, and &lt;code&gt;latest_score&lt;/code&gt; lookups. Keep this loop in your head — it makes the query below feel a lot less abstract.&lt;/p&gt;




&lt;h2 id="annotated-solution"&gt;7. My Annotated Solution&lt;/h2&gt;

&lt;p&gt;Complete Query&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;WITH&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt;
        &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

        &lt;span class="c1"&gt;-- Correlated subquery #1: get the score tied to this group's EARLIEST exam_date&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;
         &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Scores&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
         &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;
           &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;
           &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exam_date&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="c1"&gt;-- nested correlated subquery: find the MIN(exam_date) for THIS student+subject&lt;/span&gt;
                &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MIN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exam_date&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;Scores&lt;/span&gt; &lt;span class="n"&gt;ss&lt;/span&gt;
                &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;
                  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;
           &lt;span class="p"&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;first_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;

        &lt;span class="c1"&gt;-- Correlated subquery #2: get the score tied to this group's LATEST exam_date&lt;/span&gt;
        &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;score&lt;/span&gt;
         &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Scores&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;
         &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;
           &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;
           &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;exam_date&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
                &lt;span class="c1"&gt;-- nested correlated subquery: find the MAX(exam_date) for THIS student+subject&lt;/span&gt;
                &lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;MAX&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exam_date&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;Scores&lt;/span&gt; &lt;span class="n"&gt;ss&lt;/span&gt;
                &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;
                  &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;
           &lt;span class="p"&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;latest_score&lt;/span&gt;

    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Scores&lt;/span&gt; &lt;span class="n"&gt;t&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;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;latest_score&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;gt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;first_score&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;h3&gt;
  
  
  What each piece is doing
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;FROM Scores t GROUP BY student_id, subject&lt;/code&gt;&lt;/strong&gt; — collapses the raw exam rows down to one row per &lt;code&gt;(student_id, subject)&lt;/code&gt; combo. This is our set of "candidates" to evaluate.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;first_score&lt;/code&gt;&lt;/strong&gt; — a correlated subquery &lt;em&gt;nested inside&lt;/em&gt; another correlated subquery. The inner one (&lt;code&gt;MIN(exam_date)&lt;/code&gt; scoped &lt;code&gt;WHERE ss.student_id = t.student_id AND ss.subject = t.subject&lt;/code&gt;) finds the earliest date &lt;strong&gt;scoped to the current group &lt;code&gt;t&lt;/code&gt;&lt;/strong&gt;. The outer one then fetches the &lt;code&gt;score&lt;/code&gt; on that exact date, for that exact student and subject.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;latest_score&lt;/code&gt;&lt;/strong&gt; — same idea, but with &lt;code&gt;MAX(exam_date)&lt;/code&gt; instead of &lt;code&gt;MIN(exam_date)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;The condition "at least two different dates"&lt;/strong&gt; is handled &lt;em&gt;implicitly&lt;/em&gt;: if a student/subject only has one exam date, then &lt;code&gt;first_score&lt;/code&gt; and &lt;code&gt;latest_score&lt;/code&gt; end up being the &lt;strong&gt;same value&lt;/strong&gt; (same date → same score), so &lt;code&gt;latest_score &amp;gt; first_score&lt;/code&gt; is &lt;code&gt;false&lt;/code&gt; and that row gets filtered out naturally by the outer &lt;code&gt;WHERE&lt;/code&gt;. Nice side effect of the logic — no extra &lt;code&gt;HAVING COUNT(DISTINCT exam_date) &amp;gt;= 2&lt;/code&gt; needed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Final &lt;code&gt;WHERE latest_score &amp;gt; first_score&lt;/code&gt;&lt;/strong&gt; — the actual "did they improve" check.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;ORDER BY student_id, subject&lt;/code&gt;&lt;/strong&gt; — matches the problem's required output ordering.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  The line that creates the correlation
&lt;/h3&gt;

&lt;p&gt;Everything hinges on this pair of conditions inside the nested subquery:&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;WHERE&lt;/span&gt;
    &lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;student_id&lt;/span&gt;
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;ss&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;subject&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;These two conditions are the entire magic. They're what tie the inner query's &lt;code&gt;MIN&lt;/code&gt;/&lt;code&gt;MAX&lt;/code&gt; calculation to &lt;em&gt;this specific row&lt;/em&gt; of the outer query &lt;code&gt;t&lt;/code&gt;, instead of computing a &lt;code&gt;MIN&lt;/code&gt;/&lt;code&gt;MAX&lt;/code&gt; across the whole table. Remove them, and the subquery stops being correlated — it just becomes "the earliest date across every student and subject," which isn't what we want at all.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;The nested &lt;code&gt;MIN(exam_date)&lt;/code&gt; / &lt;code&gt;MAX(exam_date)&lt;/code&gt; subqueries are correlated because they're filtered by the outer group's &lt;code&gt;student_id&lt;/code&gt; and &lt;code&gt;subject&lt;/code&gt; — that's what scopes them to "this student, this subject" instead of the whole table.&lt;/p&gt;




&lt;h2 id="walkthrough"&gt;8. Walking the Query Step-by-Step on Real Data&lt;/h2&gt;

&lt;p&gt;Let's trace it for &lt;strong&gt;student 101, subject Math&lt;/strong&gt; using the example data:&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;exam_date&lt;/th&gt;
&lt;th&gt;score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2023-01-15&lt;/td&gt;
&lt;td&gt;70&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;2023-02-15&lt;/td&gt;
&lt;td&gt;85&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;Outer Query&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;----------------------------------
student_id = 101
subject    = 'Math'
----------------------------------
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;│&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;▼&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Correlated Subquery&lt;/strong&gt;&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="c1"&gt;----------------------------------&lt;/span&gt;
&lt;span class="n"&gt;Find&lt;/span&gt; &lt;span class="k"&gt;MIN&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;exam_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt;
    &lt;span class="n"&gt;student_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;101&lt;/span&gt;
&lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt;    &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'Math'&lt;/span&gt;
&lt;span class="c1"&gt;----------------------------------&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;│&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;▼&lt;/p&gt;

&lt;p&gt;&lt;code&gt;2023-01-15&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;│&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;▼&lt;/p&gt;

&lt;p&gt;Find &lt;code&gt;score&lt;/code&gt; where &lt;code&gt;exam_date = 2023-01-15&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;│&lt;br&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;▼&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;code&gt;first_score = 70&lt;/code&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;latest_score&lt;/code&gt; branch follows the exact same path, just swapping &lt;code&gt;MIN(exam_date)&lt;/code&gt; for &lt;code&gt;MAX(exam_date)&lt;/code&gt;, which resolves to &lt;code&gt;2023-02-15&lt;/code&gt; → &lt;code&gt;score = 85&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Putting it together, step by step:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;GROUP BY&lt;/code&gt; produces the group &lt;code&gt;(101, Math)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Inner subquery for &lt;code&gt;first_score&lt;/code&gt;: &lt;code&gt;MIN(exam_date)&lt;/code&gt; for &lt;code&gt;(101, Math)&lt;/code&gt; → &lt;code&gt;2023-01-15&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Outer subquery for &lt;code&gt;first_score&lt;/code&gt;: score where &lt;code&gt;exam_date = 2023-01-15&lt;/code&gt; and student=101, subject=Math → &lt;code&gt;70&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Inner subquery for &lt;code&gt;latest_score&lt;/code&gt;: &lt;code&gt;MAX(exam_date)&lt;/code&gt; for &lt;code&gt;(101, Math)&lt;/code&gt; → &lt;code&gt;2023-02-15&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Outer subquery for &lt;code&gt;latest_score&lt;/code&gt;: score where &lt;code&gt;exam_date = 2023-02-15&lt;/code&gt; → &lt;code&gt;85&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Row so far: &lt;code&gt;(101, Math, 70, 85)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Final filter: is &lt;code&gt;85 &amp;gt; 70&lt;/code&gt;? &lt;strong&gt;Yes&lt;/strong&gt; → row survives.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Now trace &lt;strong&gt;student 103, subject Math&lt;/strong&gt; (only one exam):&lt;/p&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;exam_date&lt;/th&gt;
&lt;th&gt;score&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;2023-01-15&lt;/td&gt;
&lt;td&gt;90&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ol&gt;
&lt;li&gt;Group &lt;code&gt;(103, Math)&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MIN(exam_date)&lt;/code&gt; → &lt;code&gt;2023-01-15&lt;/code&gt; → &lt;code&gt;first_score = 90&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;MAX(exam_date)&lt;/code&gt; → &lt;code&gt;2023-01-15&lt;/code&gt; (same date, only one row) → &lt;code&gt;latest_score = 90&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Final filter: is &lt;code&gt;90 &amp;gt; 90&lt;/code&gt;? &lt;strong&gt;No&lt;/strong&gt; → row is dropped. This is exactly how the "at least two exam dates" rule gets enforced for free.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;And &lt;strong&gt;student 101, subject Physics&lt;/strong&gt; (score dropped from 65 to 60):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;code&gt;first_score = 65&lt;/code&gt;, &lt;code&gt;latest_score = 60&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Is &lt;code&gt;60 &amp;gt; 65&lt;/code&gt;? &lt;strong&gt;No&lt;/strong&gt; → dropped.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;That's the entire algorithm, traced by hand.&lt;/p&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Every group's &lt;code&gt;first_score&lt;/code&gt; and &lt;code&gt;latest_score&lt;/code&gt; are resolved independently by "asking" the same two-step question: find the boundary date, then find the score on that date.&lt;/p&gt;




&lt;h2 id="performance-discussion"&gt;9. Performance Discussion: Subqueries vs Window Functions&lt;/h2&gt;

&lt;p&gt;MySQL 8.0+ gives us window functions, which can express "first and last value per group" far more efficiently and readably:&lt;/p&gt;

&lt;p&gt;Window Function Version&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;WITH&lt;/span&gt; &lt;span class="n"&gt;ranked&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="k"&gt;SELECT&lt;/span&gt;
        &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;exam_date&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;FIRST_VALUE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;exam_date&lt;/span&gt; &lt;span class="k"&gt;ASC&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;first_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;FIRST_VALUE&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;score&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
            &lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt; &lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;exam_date&lt;/span&gt; &lt;span class="k"&gt;DESC&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;latest_score&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="n"&gt;OVER&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;PARTITION&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&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;exam_count&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;Scores&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="k"&gt;DISTINCT&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;first_score&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;latest_score&lt;/span&gt;
&lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;ranked&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;exam_count&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;gt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;latest_score&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&lt;/span&gt;&lt;span class="n"&gt;gt&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;first_score&lt;/span&gt;
&lt;span class="k"&gt;ORDER&lt;/span&gt; &lt;span class="k"&gt;BY&lt;/span&gt; &lt;span class="n"&gt;student_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;subject&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;
&lt;blockquote&gt;
&lt;p&gt;💡 &lt;strong&gt;Key Idea&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Window functions typically require the engine to sort/partition the data &lt;strong&gt;once&lt;/strong&gt;, then compute all the values for every partition in that single pass. My correlated-subquery version, by contrast, re-runs &lt;code&gt;MIN&lt;/code&gt;/&lt;code&gt;MAX&lt;/code&gt;/lookup subqueries for &lt;em&gt;every group&lt;/em&gt; — and in the worst case (or without solid indexing), those inner lookups can degrade toward re-scanning relevant rows per group rather than a single unified pass.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;&lt;strong&gt;That said — a fair note on my original solution:&lt;/strong&gt; because of the &lt;code&gt;GROUP BY&lt;/code&gt;, the correlated subqueries only run &lt;strong&gt;once per distinct &lt;code&gt;(student_id, subject)&lt;/code&gt; pair&lt;/strong&gt;, not once per raw row. With a proper composite index on &lt;code&gt;(student_id, subject, exam_date)&lt;/code&gt;, MySQL can satisfy each &lt;code&gt;MIN&lt;/code&gt;/&lt;code&gt;MAX&lt;/code&gt; lookup and the point-lookup for &lt;code&gt;score&lt;/code&gt; almost instantly, so in practice this query performs quite well on realistic dataset sizes — it isn't the worst-case &lt;code&gt;O(n²)&lt;/code&gt; scenario people sometimes assume correlated subqueries always are. The window-function version is still the more "modern SQL" way to express it, and scales more predictably, but the difference is often smaller than people expect once indexing is in place.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Rule of thumb:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Reach for &lt;strong&gt;correlated subqueries&lt;/strong&gt; when you need to express "for each row/group, ask a targeted question about a related scope" — they're extremely readable for that.&lt;/li&gt;
&lt;li&gt;Reach for &lt;strong&gt;window functions&lt;/strong&gt; when you're computing this kind of first/last/rank/running-total logic across an &lt;em&gt;entire&lt;/em&gt; partitioned dataset — they usually scale better and avoid re-execution per group.&lt;/li&gt;
&lt;li&gt;Always check &lt;code&gt;EXPLAIN&lt;/code&gt; on your actual data before assuming either is "the fast one" — data size, indexes, and MySQL version all matter more than the textbook complexity story.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Summary
&lt;/h3&gt;

&lt;p&gt;Correlated subqueries shine for readability and targeted per-group lookups; window functions shine for scale, since they avoid re-running the same calculation over and over.&lt;/p&gt;




&lt;h2 id="conclusion"&gt;10. Conclusion &amp;amp; Further Practice&lt;/h2&gt;

&lt;p&gt;The core idea to take away from this whole post is small enough to fit in one sentence:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;A correlated subquery is just a mini-query that gets handed one value from the outer row at a time and answers a question scoped to that value.&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Once that clicks, most "hard" correlated-subquery problems stop being hard — they just become "what's the mini-question I need to ask per row/group, and what value from the outer query does it need?"&lt;/p&gt;

&lt;p&gt;If you want to build this muscle further, here are a few problems that lean directly on the same skill:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Second Highest Salary&lt;/strong&gt; (classic correlated subquery warm-up)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Department Top Three Salaries&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Rank Scores&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Consecutive Numbers&lt;/strong&gt;&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Employees Earning More Than Their Managers&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Try solving each one twice, the way I did here: once with a correlated subquery (or window function), and once by hand-tracing it like a loop on paper. The loop trace is what actually builds intuition — the SQL syntax is just the encoding of that intuition afterward.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final Thoughts
&lt;/h2&gt;

&lt;p&gt;Correlated subqueries are often considered one of the trickiest SQL concepts—not because the syntax is difficult, but because it's easy to lose track of &lt;strong&gt;which query is executing and which row is being referenced&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Once you realize that the inner query is simply "borrowing" values from the current row of the outer query, the concept becomes much more intuitive.&lt;/p&gt;

&lt;p&gt;I hope this article helped make that mental model a little clearer.&lt;/p&gt;

&lt;p&gt;Happy learning! 🚀&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>mysql</category>
      <category>leetcode</category>
    </item>
    <item>
      <title>Any improvements that are needed? I’d be happy to receive suggestions. The app is posted on itch.io and, since it’s a free unsigned Windows .exe, the page has been temporarily quarantined (still downloadable). As traffic and trust increase it'll be fine.</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Wed, 07 Jan 2026 15:55:14 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/any-improvements-that-are-needed-id-be-happy-to-receive-suggestions-the-app-is-posted-on-itchio-4n3f</link>
      <guid>https://dev.to/saamiabbaskhan/any-improvements-that-are-needed-id-be-happy-to-receive-suggestions-the-app-is-posted-on-itchio-4n3f</guid>
      <description>&lt;div class="ltag__link--embedded"&gt;
  &lt;div class="crayons-story "&gt;
  &lt;a href="https://dev.to/saamiabbaskhan/i-made-a-habit-tracker-because-the-free-ones-were-not-free-58a4" class="crayons-story__hidden-navigation-link"&gt;I Made a Habit Tracker Because the Free Ones Were… Not Free 😅&lt;/a&gt;


  &lt;div class="crayons-story__body crayons-story__body-full_post"&gt;
    &lt;div class="crayons-story__top"&gt;
      &lt;div class="crayons-story__meta"&gt;
        &lt;div class="crayons-story__author-pic"&gt;

          &lt;a href="/saamiabbaskhan" class="crayons-avatar  crayons-avatar--l  "&gt;
            &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2069606%2Fb35d1209-61fd-4120-ae9b-f4e76801e6f4.png" alt="saamiabbaskhan profile" class="crayons-avatar__image" width="96" height="96"&gt;
          &lt;/a&gt;
        &lt;/div&gt;
        &lt;div&gt;
          &lt;div&gt;
            &lt;a href="/saamiabbaskhan" class="crayons-story__secondary fw-medium m:hidden"&gt;
              Saami abbas Khan
            &lt;/a&gt;
            &lt;div class="profile-preview-card relative mb-4 s:mb-0 fw-medium hidden m:inline-block"&gt;
              
                Saami abbas Khan
                
                
              
              &lt;div id="story-author-preview-content-3108272" class="profile-preview-card__content crayons-dropdown branded-7 p-4 pt-0"&gt;
                &lt;div class="gap-4 grid"&gt;
                  &lt;div class="-mt-4"&gt;
                    &lt;a href="/saamiabbaskhan" class="flex"&gt;
                      &lt;span class="crayons-avatar crayons-avatar--xl mr-2 shrink-0"&gt;
                        &lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F2069606%2Fb35d1209-61fd-4120-ae9b-f4e76801e6f4.png" class="crayons-avatar__image" alt="" width="96" height="96"&gt;
                      &lt;/span&gt;
                      &lt;span class="crayons-link crayons-subtitle-2 mt-5"&gt;Saami abbas Khan&lt;/span&gt;
                    &lt;/a&gt;
                  &lt;/div&gt;
                  &lt;div class="print-hidden"&gt;
                    
                      Follow
                    
                  &lt;/div&gt;
                  &lt;div class="author-preview-metadata-container"&gt;&lt;/div&gt;
                &lt;/div&gt;
              &lt;/div&gt;
            &lt;/div&gt;

          &lt;/div&gt;
          &lt;a href="https://dev.to/saamiabbaskhan/i-made-a-habit-tracker-because-the-free-ones-were-not-free-58a4" class="crayons-story__tertiary fs-xs"&gt;&lt;time&gt;Dec 16 '25&lt;/time&gt;&lt;span class="time-ago-indicator-initial-placeholder"&gt;&lt;/span&gt;&lt;/a&gt;
        &lt;/div&gt;
      &lt;/div&gt;

    &lt;/div&gt;

    &lt;div class="crayons-story__indention"&gt;
      &lt;h2 class="crayons-story__title crayons-story__title-full_post"&gt;
        &lt;a href="https://dev.to/saamiabbaskhan/i-made-a-habit-tracker-because-the-free-ones-were-not-free-58a4" id="article-link-3108272"&gt;
          I Made a Habit Tracker Because the Free Ones Were… Not Free 😅
        &lt;/a&gt;
      &lt;/h2&gt;
        &lt;div class="crayons-story__tags"&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/opensource"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;opensource&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/gemini"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;gemini&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/ai"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;ai&lt;/a&gt;
            &lt;a class="crayons-tag  crayons-tag--monochrome " href="/t/vscode"&gt;&lt;span class="crayons-tag__prefix"&gt;#&lt;/span&gt;vscode&lt;/a&gt;
        &lt;/div&gt;
      &lt;div class="crayons-story__bottom"&gt;
        &lt;div class="crayons-story__details"&gt;
          &lt;a href="https://dev.to/saamiabbaskhan/i-made-a-habit-tracker-because-the-free-ones-were-not-free-58a4" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left"&gt;
            &lt;div class="multiple_reactions_aggregate"&gt;
              &lt;span class="multiple_reactions_icons_container"&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/raised-hands-74b2099fd66a39f2d7eed9305ee0f4553df0eb7b4f11b01b6b1b499973048fe5.svg" width="24" height="24"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/fire-f60e7a582391810302117f987b22a8ef04a2fe0df7e3258a5f49332df1cec71e.svg" width="24" height="24"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/sparkle-heart-5f9bee3767e18deb1bb725290cb151c25234768a0e9a2bd39370c382d02920cf.svg" width="24" height="24"&gt;
                  &lt;/span&gt;
              &lt;/span&gt;
              &lt;span class="aggregate_reactions_counter"&gt;13&lt;span class="hidden s:inline"&gt;&amp;nbsp;reactions&lt;/span&gt;&lt;/span&gt;
            &lt;/div&gt;
          &lt;/a&gt;
            &lt;a href="https://dev.to/saamiabbaskhan/i-made-a-habit-tracker-because-the-free-ones-were-not-free-58a4#comments" class="crayons-btn crayons-btn--s crayons-btn--ghost crayons-btn--icon-left flex items-center"&gt;
              

              7&lt;span class="hidden s:inline"&gt;&amp;nbsp;comments&lt;/span&gt;
            &lt;/a&gt;
        &lt;/div&gt;
        &lt;div class="crayons-story__save"&gt;
          &lt;small class="crayons-story__tertiary fs-xs mr-2"&gt;
            1 min read
          &lt;/small&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  &lt;/div&gt;
&lt;/div&gt;

&lt;/div&gt;


</description>
      <category>opensource</category>
      <category>gemini</category>
      <category>ai</category>
      <category>vscode</category>
    </item>
    <item>
      <title>My Indie App Got Quarantined on itch.io — and I’m Opening It Up to the Dev Community</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Wed, 07 Jan 2026 15:41:09 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/my-indie-app-got-quarantined-on-itchio-and-im-opening-it-up-to-the-dev-community-2fkk</link>
      <guid>https://dev.to/saamiabbaskhan/my-indie-app-got-quarantined-on-itchio-and-im-opening-it-up-to-the-dev-community-2fkk</guid>
      <description>&lt;p&gt;A few days ago, something unexpected happened.&lt;/p&gt;

&lt;p&gt;I published a small indie project on &lt;a href="https://saamiabbaskhan.itch.io/focus-and-grow-habit-tracking" rel="noopener noreferrer"&gt;itch.io&lt;/a&gt; — a lightweight, offline habit-tracking app I built for people who want fewer distractions and more consistency.&lt;/p&gt;

&lt;p&gt;Within hours, the page was &lt;strong&gt;automatically placed under quarantine&lt;/strong&gt; for additional review. No takedown, No warning.&lt;br&gt;&lt;br&gt;
Just… quarantine.&lt;/p&gt;




&lt;h2&gt;
  
  
  🧠 What the App Is (and Isn’t)
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Focus &amp;amp; Grow&lt;/strong&gt; is a &lt;strong&gt;fully offline Windows habit tracker&lt;/strong&gt;.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;No accounts
&lt;/li&gt;
&lt;li&gt;No ads
&lt;/li&gt;
&lt;li&gt;No trackers
&lt;/li&gt;
&lt;li&gt;No background network activity
&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Tech stack
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;TypeScript&lt;/strong&gt; for app logic
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tauri (Rust)&lt;/strong&gt; for packaging
&lt;/li&gt;
&lt;li&gt;Distributed as a zipped &lt;strong&gt;Windows &lt;code&gt;.exe&lt;/code&gt;&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Includes a clear &lt;strong&gt;README&lt;/strong&gt; explaining usage and intent
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Nothing obfuscated. Nothing hidden.&lt;/p&gt;




&lt;h2&gt;
  
  
  🤔 Why This Probably Happened
&lt;/h2&gt;

&lt;p&gt;If you’ve shipped a Windows executable before, you know the struggle:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Unsigned &lt;code&gt;.exe&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;New project with no reputation&lt;/li&gt;
&lt;li&gt;Automated security heuristics doing their thing&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I completely understand why platforms do this — but it raises a real question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;How do indie developers establish trust when starting from zero?&lt;/strong&gt;&lt;/p&gt;
&lt;/blockquote&gt;




&lt;h2&gt;
  
  
  🧪 Why I’m Sharing This Here
&lt;/h2&gt;

&lt;p&gt;I’ve already contacted itch.io for a &lt;strong&gt;manual review&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;In the meantime, I’m doing the most transparent thing I can:&lt;/p&gt;

&lt;p&gt;Opening the project up to the dev community.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Curious? Take a look.
&lt;/li&gt;
&lt;li&gt;Cautious? Inspect it.
&lt;/li&gt;
&lt;li&gt;Been through this before? Share your experience.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Feedback, comments, or even just engagement genuinely help — both for improving the app and for signaling legitimacy.&lt;/p&gt;




&lt;h2&gt;
  
  
  🔗 The Project
&lt;/h2&gt;

&lt;p&gt;The app is called &lt;a href="https://saamiabbaskhan.itch.io/focus-and-grow-habit-tracking" rel="noopener noreferrer"&gt;Focus &amp;amp; Grow&lt;/a&gt; — a simple habit tracker designed to stay out of your way.&lt;/p&gt;

&lt;p&gt;If you check it out and have thoughts, I’d love to hear them.&lt;/p&gt;




&lt;p&gt;Building in public isn’t always comfortable — but it’s worth it.&lt;/p&gt;

&lt;p&gt;Let’s talk.&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>help</category>
      <category>coding</category>
      <category>javascript</category>
    </item>
    <item>
      <title>I Made a Habit Tracker Because the Free Ones Were… Not Free 😅</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Tue, 16 Dec 2025 08:45:36 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/i-made-a-habit-tracker-because-the-free-ones-were-not-free-58a4</link>
      <guid>https://dev.to/saamiabbaskhan/i-made-a-habit-tracker-because-the-free-ones-were-not-free-58a4</guid>
      <description>&lt;p&gt;Recently, I was scrolling through Instagram and came across this &lt;strong&gt;amazing habit-tracking template&lt;/strong&gt; on Notion. But when I tried to download it… surprise, it was &lt;strong&gt;paid&lt;/strong&gt;. Ah, the pain. 😅&lt;/p&gt;

&lt;p&gt;Instead of endlessly searching for free alternatives or settling for something that didn’t quite click, I did the &lt;strong&gt;non-obvious thing&lt;/strong&gt;: I made my own.&lt;/p&gt;

&lt;p&gt;Enter &lt;strong&gt;Focus &amp;amp; Grow&lt;/strong&gt; – a minimalistic, distraction-free monthly habit tracker that I &lt;strong&gt;vibe-coded&lt;/strong&gt; using &lt;strong&gt;TypeScript, React, and Tauri&lt;/strong&gt;. The goal? A tool that’s:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Clean &amp;amp; simple&lt;/strong&gt; – no clutter, just your habits and progress
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Privacy-first&lt;/strong&gt; – 100% local storage, no login required
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Powerful analytics&lt;/strong&gt; – streaks, trends, completion rates
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exportable&lt;/strong&gt; – share your monthly consistency chart as an image
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Desktop-ready&lt;/strong&gt; – works natively via Tauri, or in your browser
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can &lt;strong&gt;either download the ready-to-use app from &lt;a href="https://saamiabbaskhan.itch.io/focus-and-grow-habit-tracking" rel="noopener noreferrer"&gt;Itch.io&lt;/a&gt;&lt;/strong&gt; for a smooth experience, &lt;strong&gt;or check out the code on &lt;a href="https://github.com/SaamiAbbasKhan/Focus-and-Grow-" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt; if you want to tinker or contribute.  &lt;/p&gt;

&lt;p&gt;It would mean a lot if you could take a few minutes to &lt;strong&gt;test it out&lt;/strong&gt;, &lt;strong&gt;share feedback&lt;/strong&gt;, or &lt;strong&gt;suggest new features&lt;/strong&gt;. I’m especially curious about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How intuitive it feels
&lt;/li&gt;
&lt;li&gt;Any additional features you’d like to see
&lt;/li&gt;
&lt;li&gt;Suggestions for improvement
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks a ton for reading! 💛  &lt;/p&gt;

&lt;p&gt;If you try it, feel free to &lt;strong&gt;comment below&lt;/strong&gt;—I’d love to hear how you &lt;strong&gt;focus and grow&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>opensource</category>
      <category>gemini</category>
      <category>ai</category>
      <category>vscode</category>
    </item>
    <item>
      <title>I Made a Habit Tracker Because the Free Ones Were… Not Free 😅</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Mon, 15 Dec 2025 10:48:36 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/i-made-a-habit-tracker-because-the-free-ones-were-not-free-225f</link>
      <guid>https://dev.to/saamiabbaskhan/i-made-a-habit-tracker-because-the-free-ones-were-not-free-225f</guid>
      <description>&lt;p&gt;Recently, I was scrolling through Instagram and came across this &lt;strong&gt;amazing habit-tracking template&lt;/strong&gt; on Notion. But when I tried to download it… surprise, it was &lt;strong&gt;paid&lt;/strong&gt;. Ah, the pain. 😅&lt;/p&gt;

&lt;p&gt;Instead of endlessly searching for free alternatives or settling for something that didn’t quite click, I did the &lt;strong&gt;non-obvious thing&lt;/strong&gt;: I made my own.&lt;/p&gt;

&lt;p&gt;Enter &lt;strong&gt;Focus &amp;amp; Grow&lt;/strong&gt; – a minimalistic, distraction-free monthly habit tracker that I &lt;strong&gt;vibe-coded&lt;/strong&gt; using &lt;strong&gt;TypeScript, React, and Tauri&lt;/strong&gt;. The goal? A tool that’s:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Clean &amp;amp; simple&lt;/strong&gt; – no clutter, just your habits and progress
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Privacy-first&lt;/strong&gt; – 100% local storage, no login required
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Powerful analytics&lt;/strong&gt; – streaks, trends, completion rates
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Exportable&lt;/strong&gt; – share your monthly consistency chart as an image
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Desktop-ready&lt;/strong&gt; – works natively via Tauri, or in your browser
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can &lt;strong&gt;either download the ready-to-use app from &lt;a href="https://saamiabbaskhan.itch.io/focus-and-grow-habit-tracking" rel="noopener noreferrer"&gt;Itch.io&lt;/a&gt;&lt;/strong&gt; for a smooth experience, &lt;strong&gt;or check out the code on &lt;a href="https://github.com/SaamiAbbasKhan/Focus-and-Grow-" rel="noopener noreferrer"&gt;GitHub&lt;/a&gt;&lt;/strong&gt; if you want to tinker or contribute.  &lt;/p&gt;

&lt;p&gt;It would mean a lot if you could take a few minutes to &lt;strong&gt;test it out&lt;/strong&gt;, &lt;strong&gt;share feedback&lt;/strong&gt;, or &lt;strong&gt;suggest new features&lt;/strong&gt;. I’m especially curious about:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How intuitive it feels
&lt;/li&gt;
&lt;li&gt;Any additional features you’d like to see
&lt;/li&gt;
&lt;li&gt;Suggestions for improvement
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Thanks a ton for reading! 💛  &lt;/p&gt;

&lt;p&gt;If you try it, feel free to &lt;strong&gt;comment below&lt;/strong&gt;—I’d love to hear how you &lt;strong&gt;focus and grow&lt;/strong&gt;.&lt;/p&gt;

</description>
      <category>productivity</category>
      <category>tooling</category>
      <category>vibecoding</category>
      <category>opensource</category>
    </item>
    <item>
      <title>CourseTime Analyzer: Python + Selenium + GUI Project for Tracking YouTube Course Time</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Wed, 17 Sep 2025 15:47:07 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/coursetime-analyzer-python-selenium-gui-project-for-tracking-youtube-course-time-2mjp</link>
      <guid>https://dev.to/saamiabbaskhan/coursetime-analyzer-python-selenium-gui-project-for-tracking-youtube-course-time-2mjp</guid>
      <description>&lt;p&gt;Ever wondered &lt;strong&gt;how long it will take to finish that YouTube course playlist&lt;/strong&gt; you just found? Instead of manually checking each video, I built a small Python project called &lt;strong&gt;CourseTime Analyzer&lt;/strong&gt; 🚀.  &lt;/p&gt;

&lt;p&gt;This tool automatically searches YouTube for a course playlist, fetches all video durations, and calculates the &lt;strong&gt;total study time&lt;/strong&gt; — all packed in a simple &lt;strong&gt;Tkinter GUI&lt;/strong&gt;.  &lt;/p&gt;




&lt;h3&gt;
  
  
  🔥 Why I Built This
&lt;/h3&gt;

&lt;p&gt;Whenever I started a YouTube course, I always wanted to know &lt;em&gt;“How much total time will this take?”&lt;/em&gt; Sure, YouTube shows individual durations, but for playlists with 50+ videos, calculating by hand is painful.  &lt;/p&gt;

&lt;p&gt;So I automated it with &lt;strong&gt;Python + Selenium&lt;/strong&gt; and wrapped it in a clean GUI using &lt;strong&gt;Tkinter&lt;/strong&gt;.  &lt;/p&gt;

&lt;p&gt;👉 To be honest, this was a &lt;strong&gt;time-pass project&lt;/strong&gt;. I wasn’t in the mood to continue my actual work (learning more about &lt;em&gt;Softmax Regression&lt;/em&gt; 😅), so I coded this as a fun escape.  &lt;/p&gt;




&lt;h3&gt;
  
  
  ⚙️ Features
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;🔍 Search YouTube for any course playlist
&lt;/li&gt;
&lt;li&gt;📺 Fetch playlist title and creator details
&lt;/li&gt;
&lt;li&gt;⏱️ Calculate total video duration in hours
&lt;/li&gt;
&lt;li&gt;🎥 Show video count and display durations (first 15 listed, rest summarized)
&lt;/li&gt;
&lt;li&gt;🔗 Clickable playlist link directly inside GUI
&lt;/li&gt;
&lt;li&gt;🖼️ Clean interface with background image
&lt;/li&gt;
&lt;li&gt;🖥️ Supports &lt;strong&gt;GUI mode (graphics.py)&lt;/strong&gt; and &lt;strong&gt;CLI mode (main.py)&lt;/strong&gt;
&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  🧩 Modularized Approach
&lt;/h3&gt;

&lt;p&gt;The project is structured to keep things clean and reusable:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;main.py&lt;/strong&gt; → Handles the core mechanism (YouTube scraping &amp;amp; analysis via Selenium).
&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;graphics.py&lt;/strong&gt; → A wrapper around &lt;code&gt;main.py&lt;/code&gt; that provides a Tkinter-based GUI.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You can run &lt;code&gt;main.py&lt;/code&gt; independently in CLI mode — the GUI is just an additional layer.  &lt;/p&gt;




&lt;h3&gt;
  
  
  🚀 GitHub Repository
&lt;/h3&gt;

&lt;p&gt;All the code is open-source and available here:  &lt;/p&gt;

&lt;p&gt;👉 &lt;a href="https://github.com/SaamiAbbasKhan/CourseTime-Analyzer" rel="noopener noreferrer"&gt;CourseTime Analyzer on GitHub&lt;/a&gt;  &lt;/p&gt;




&lt;h3&gt;
  
  
  💬 Feedback
&lt;/h3&gt;

&lt;p&gt;Comments are open! Feel free to suggest improvements, criticize the approach, or even fork the repo and make it better. This was just a fun side project, so I’d love to see how others take it further. 🚀  &lt;/p&gt;




</description>
      <category>python</category>
      <category>selenium</category>
      <category>resources</category>
      <category>tutorial</category>
    </item>
    <item>
      <title>Yo!</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Mon, 07 Jul 2025 16:57:06 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/yo-5fbj</link>
      <guid>https://dev.to/saamiabbaskhan/yo-5fbj</guid>
      <description>&lt;p&gt;Hello! After a 10-month break 😅&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>watercooler</category>
    </item>
    <item>
      <title>Hacktoberfest Experience</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Sun, 27 Oct 2024 14:26:13 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/hacktoberfest-experience-5blm</link>
      <guid>https://dev.to/saamiabbaskhan/hacktoberfest-experience-5blm</guid>
      <description>&lt;p&gt;&lt;em&gt;This is a submission for the &lt;a href="https://dev.to/challenges/hacktoberfest"&gt;2024 Hacktoberfest Writing challenge&lt;/a&gt;: Contributor Experience&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Hacktoberfest was an amazing experience! I learned so much about open-source collaboration, from understanding new codebases to following contributor guidelines. Although some issues were challenging, each taught me valuable skills and boosted my confidence.&lt;/p&gt;

&lt;p&gt;The community’s support was also very incredible.&lt;/p&gt;

</description>
      <category>devchallenge</category>
      <category>hacktoberfest</category>
      <category>opensource</category>
    </item>
    <item>
      <title>Should You Even Put Your Simple Projects on GitHub?</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Tue, 01 Oct 2024 19:20:19 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/should-you-even-put-your-simple-projects-on-github-160m</link>
      <guid>https://dev.to/saamiabbaskhan/should-you-even-put-your-simple-projects-on-github-160m</guid>
      <description>&lt;p&gt;I was hesitant to share my latest project, a Simple CLI File Explorer, because I feared it might be too basic. But after a lot of thought, I finally hit the upload button.&lt;/p&gt;

&lt;p&gt;I'm a beginner, still learning at my own pace, even if it’s just a little bit each day. But the key is, I’m staying consistent (thank God!). This project was designed to strengthen my core concepts, and while I know it’s far from perfect, and there are definitely areas for improvement, I honestly just needed to get it out there.&lt;/p&gt;

&lt;p&gt;Please check out my &lt;a href="https://github.com/SaamiAbbasKhan/Initial-Learnings/tree/main/Simple-FileExplorer-CLI" rel="noopener noreferrer"&gt;Simple CLI File Explorer&lt;/a&gt;, and I would love your honest    feedback—be as brutal as you want! Did I make the right call posting this? Where do I stand with this project?&lt;/p&gt;

&lt;p&gt;Your Opinion Matters!&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>learning</category>
      <category>programming</category>
      <category>career</category>
    </item>
    <item>
      <title>Unlocking Functional Programming in Java: A Guide to Lambdas, Method References, and Chaining</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Fri, 27 Sep 2024 11:57:04 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/unlocking-functional-programming-in-java-a-guide-to-lambdas-method-references-and-chaining-4040</link>
      <guid>https://dev.to/saamiabbaskhan/unlocking-functional-programming-in-java-a-guide-to-lambdas-method-references-and-chaining-4040</guid>
      <description>&lt;p&gt;In this post we'll discover the power of functional programming in Java with lambdas, method references, and function chaining. Simplify your code and boost efficiency with these modern techniques!&lt;/p&gt;




&lt;h2&gt;
  
  
  &lt;em&gt;Table of Contents&lt;/em&gt;
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Introduction to Functional Programming&lt;/li&gt;
&lt;li&gt;Lambda Expressions&lt;/li&gt;
&lt;li&gt;Method References&lt;/li&gt;
&lt;li&gt;Functional Interfaces&lt;/li&gt;
&lt;li&gt;Lambda Chaining&lt;/li&gt;
&lt;li&gt;Predicate Chaining&lt;/li&gt;
&lt;li&gt;Custom vs. Default Functional Interface Chaining
&lt;/li&gt;
&lt;li&gt;Conclusion&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Introduction to Functional Programming
&lt;/h2&gt;

&lt;p&gt;Functional programming is a programming paradigm that emphasizes writing concise, efficient, and reusable code by extensively using functions, particularly lambdas. One of its key benefits is &lt;strong&gt;brevity&lt;/strong&gt;—reducing code length without sacrificing clarity or efficiency. In functional programming, functions are treated as first-class citizens, allowing for easier function chaining, leading to less verbose code.&lt;/p&gt;

&lt;p&gt;Adopting functional programming can significantly enhance productivity and maintainability, especially when working with complex data transformations or streamlining logic. However, brevity doesn’t mean sacrificing efficiency or readability. A well-written functional program should still be easy to understand, debug, and maintain.&lt;/p&gt;

&lt;p&gt;To successfully leverage functional programming, it’s essential to understand key terminologies such as functional interfaces, lambda expressions, method references, and chaining of functions.&lt;/p&gt;

&lt;p&gt;In this post, we'll explore these concepts in detail to help you harness the full power of functional programming in Java.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lambda Expressions
&lt;/h2&gt;

&lt;p&gt;Lambda expressions are simply a concise way to represent methods or functions in programming languages like Java. They are a key component of functional programming, allowing you to write cleaner, more expressive code.&lt;/p&gt;

&lt;p&gt;In Java, &lt;strong&gt;lambda expressions&lt;/strong&gt; are tightly coupled with &lt;strong&gt;functional interfaces&lt;/strong&gt;. To use lambdas effectively, it's essential to understand what a functional interface is.&lt;/p&gt;

&lt;p&gt;A functional interface in Java is an interface with only one abstract method. This method can be implemented using a lambda expression, which makes the code shorter and more readable.&lt;/p&gt;

&lt;p&gt;Here's a simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@FunctionalInterface&lt;/span&gt;
&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;countInterface&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns the count, e.g., "Saami" returns 5&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;

&lt;span class="c1"&gt;// Implementing the interface using a lambda&lt;/span&gt;
&lt;span class="n"&gt;countInterface&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;();&lt;/span&gt; &lt;span class="c1"&gt;// Lambda to return string length&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;variable&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Saami"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: 5&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example, the lambda &lt;code&gt;s -&amp;gt; s.length()&lt;/code&gt; is used to implement the &lt;code&gt;count()&lt;/code&gt; method from the &lt;code&gt;countInterface&lt;/code&gt;. It's a compact and elegant way of writing what would otherwise require a more verbose approach using anonymous classes.&lt;/p&gt;

&lt;p&gt;While you could create a method to achieve the same result, using lambdas aligns with the functional programming paradigm of brevity—writing concise and expressive code. Lambdas can also be multi-line, but the aim is to maintain simplicity and brevity whenever possible&lt;/p&gt;

&lt;h2&gt;
  
  
  Method References
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Method references&lt;/strong&gt; in Java are a shorthand way to further simplify lambda expressions. They provide a more readable and concise syntax, making your code easier to understand while maintaining functionality. Method references are particularly useful when your lambda expression simply calls a method.&lt;/p&gt;

&lt;p&gt;Let’s take a look at some examples where a lambda expression can be replaced with a method reference for improved readability:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nd"&gt;@FunctionalInterface&lt;/span&gt;
&lt;span class="kd"&gt;interface&lt;/span&gt; &lt;span class="nc"&gt;CountInterface&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;
    &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;count&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="no"&gt;T&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Returns the count, e.g., "Saami" returns 5&lt;/span&gt;
&lt;span class="o"&gt;}&lt;/span&gt;
&lt;span class="c1"&gt;// Implementing the interface using a method reference&lt;/span&gt;

&lt;span class="nc"&gt;CountInterface&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;variable&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nl"&gt;String:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;length&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt; 
&lt;span class="c1"&gt;// Using the method reference to get the length of the string&lt;/span&gt;
&lt;span class="kt"&gt;var&lt;/span&gt; &lt;span class="n"&gt;result&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;variable&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;count&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Saami"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;result&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: 5&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Functional Interfaces
&lt;/h2&gt;

&lt;p&gt;In Java, a functional interface is an interface that contains exactly one abstract method. This concept is pivotal in functional programming, as it allows the use of lambda expressions to implement the interface's functionality in a concise manner. Functional interfaces can also contain default or static methods, but they must adhere to the rule of having only one abstract method.&lt;/p&gt;

&lt;p&gt;The &lt;code&gt;@FunctionalInterface&lt;/code&gt; annotation is used to indicate that an interface is intended to be a functional interface. While this annotation is not mandatory, it provides compile-time checking to ensure that the interface remains functional. If you accidentally add more than one abstract method, the compiler will throw an error.&lt;/p&gt;

&lt;p&gt;For more details on functional interfaces, feel free to check out my dedicated post on &lt;a href="https://dev.to/saamiabbaskhan/default-functional-interfaces-in-java-3hho"&gt;functional interfaces&lt;/a&gt; where I delve deeper into their usage, examples, and best practices.&lt;/p&gt;

&lt;h2&gt;
  
  
  Lambda Chaining
&lt;/h2&gt;

&lt;p&gt;Before diving into lambda chaining, it’s important to understand the default functional interfaces provided by Java. For a detailed overview, check out my post on &lt;a href="https://dev.to/saamiabbaskhan/default-functional-interfaces-in-java-3hho"&gt;Default Functional Interfaces in Java&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In Java, you can chain lambda expressions using the &lt;code&gt;andThen()&lt;/code&gt; method, which is available in both the Function and Consumer interfaces. The main difference between the two lies in how they handle inputs and outputs:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Function Interface&lt;/strong&gt;: The Function interface is designed for transformations. It takes an input, processes it, and returns an output. When chaining functions, the output of the first lambda expression becomes the input for the second. This allows for a seamless flow of data through multiple transformations.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;uCase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nl"&gt;String:&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;toUpperCase&lt;/span&gt;&lt;span class="o"&gt;;&lt;/span&gt;

&lt;span class="nc"&gt;Function&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;[]&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;fun&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;uCase&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;andThen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;concat&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"KHAN"&lt;/span&gt;&lt;span class="o"&gt;)).&lt;/span&gt;&lt;span class="na"&gt;andThen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;split&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;""&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt;
&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Arrays&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toString&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;fun&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;apply&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Saami"&lt;/span&gt;&lt;span class="o"&gt;)));&lt;/span&gt;

&lt;span class="c1"&gt;// Output&lt;/span&gt;
&lt;span class="c1"&gt;// S A A M I K H A N &lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Consumer Interface: In contrast, the Consumer interface does not return any result. Instead, it takes an input and performs an action, typically producing side effects. When using &lt;code&gt;andThen()&lt;/code&gt; with consumers, the first consumer will execute, and then the second will follow.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Consumer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printUpperCase&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;toUpperCase&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;
&lt;span class="nc"&gt;Consumer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;printLength&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Length: "&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;length&lt;/span&gt;&lt;span class="o"&gt;());&lt;/span&gt;

&lt;span class="nc"&gt;Consumer&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;combinedConsumer&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;printUpperCase&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;andThen&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;printLength&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;combinedConsumer&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;accept&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Saami"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: "SAAMI" and "Length: 5"&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;By using &lt;code&gt;andThen()&lt;/code&gt;, you can effectively chain lambda expressions to create more complex behavior in a clean and readable manner. This chaining allows for efficient code organization and minimizes boilerplate, aligning with the principles of functional programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Predicate Chaining
&lt;/h2&gt;

&lt;p&gt;Unlike the Function or Consumer interfaces, we don’t have an &lt;code&gt;andThen()&lt;/code&gt;method for predicates. However, you can chain predicates using the &lt;code&gt;and()&lt;/code&gt;, &lt;code&gt;or()&lt;/code&gt;, and &lt;code&gt;negate()&lt;/code&gt; methods. These methods allow you to combine multiple predicates into a logical chain, facilitating complex conditional checks in a concise manner.&lt;/p&gt;

&lt;p&gt;Example of Predicate Chaining:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight java"&gt;&lt;code&gt;&lt;span class="nc"&gt;Predicate&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;equals&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"Saami"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;Predicate&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p2&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;startsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"S"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;
&lt;span class="nc"&gt;Predicate&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;p3&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt; &lt;span class="o"&gt;-&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;endsWith&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"b"&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// Chaining predicates using or(), negate(), and and()&lt;/span&gt;
&lt;span class="nc"&gt;Predicate&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;String&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;combined&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;p1&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;or&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p2&lt;/span&gt;&lt;span class="o"&gt;).&lt;/span&gt;&lt;span class="na"&gt;negate&lt;/span&gt;&lt;span class="o"&gt;().&lt;/span&gt;&lt;span class="na"&gt;and&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;p3&lt;/span&gt;&lt;span class="o"&gt;);&lt;/span&gt; 

&lt;span class="c1"&gt;// Here, chaining requires no `andThen()`; you can directly chain the logical convenience methods using the dot (.) operator.&lt;/span&gt;
&lt;span class="c1"&gt;// Thus making a LOGICAL CHAIN&lt;/span&gt;

&lt;span class="nc"&gt;System&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;out&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;println&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="n"&gt;combined&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="na"&gt;test&lt;/span&gt;&lt;span class="o"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"SaamI"&lt;/span&gt;&lt;span class="o"&gt;));&lt;/span&gt; &lt;span class="c1"&gt;// Outputs: false&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In this example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;p1 checks if the string equals &lt;strong&gt;"Saami"&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;p2 checks if the string starts with &lt;strong&gt;"S"&lt;/strong&gt;.&lt;/li&gt;
&lt;li&gt;p3 checks if the string ends with &lt;strong&gt;"b"&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The combined predicate first checks if either p1 or p2 is true and then negates that result. Finally, it checks if p3 is true. This allows you to build a logical chain without needing additional methods like &lt;code&gt;andThen()&lt;/code&gt;, making it straightforward and intuitive.&lt;/p&gt;

&lt;p&gt;By utilizing these chaining methods, you can create complex conditional logic while keeping your code clean and readable, which aligns perfectly with the goals of functional programming.&lt;/p&gt;

&lt;h2&gt;
  
  
  Custom Functional Interface Chaining vs. Default Functional Interfaces
&lt;/h2&gt;

&lt;p&gt;While creating custom functional interfaces allows for flexibility in defining specific behaviors, chaining these custom interfaces can become quite complex. Here’s why using default functional interfaces is often the better choice:&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity of Custom Functional Interface Chaining:
&lt;/h3&gt;

&lt;p&gt;When you decide to chain custom functional interfaces, you must carefully consider how parameters are passed between lambdas. This involves:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Parameter Matching: Ensuring that the parameters of one lambda match the expected input type of the next. This can add overhead to your design.&lt;/li&gt;
&lt;li&gt;Edge Case Handling: You need to think through various edge cases and potential input scenarios to maintain consistent and correct behavior across chains.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This added complexity can lead to more cumbersome and error-prone code.&lt;/p&gt;

&lt;p&gt;Default Functional Interfaces Are Optimized for such purposes, Java's built-in functional interfaces, such as Function, Predicate, and Consumer, are designed for common use cases and come with several advantages:&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In summary, functional programming in Java offers powerful tools for writing clean, efficient, and maintainable code. By leveraging lambda expressions, method references, and functional interfaces, developers can express complex operations concisely. Chaining functions, whether through the &lt;code&gt;andThen()&lt;/code&gt; method for functional transformations or through logical methods for predicates, enhances code readability and organization.&lt;/p&gt;

&lt;p&gt;While custom functional interfaces provide flexibility, they often introduce complexity that can be avoided by utilizing Java’s built-in default functional interfaces. This approach not only streamlines the development process but also aligns with the principles of functional programming.&lt;/p&gt;

&lt;p&gt;By understanding and applying these concepts, you can unlock the full potential of functional programming in Java, making your code more expressive and easier to maintain.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;All information in this post reflects my personal learnings as I document my journey in programming. I casually create posts to share insights with others.&lt;br&gt;
I would love to hear any additional tips or insights from fellow developers! Feel free to share your thoughts in the comments below.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>java</category>
      <category>programming</category>
      <category>career</category>
    </item>
  </channel>
</rss>
