<?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>LeetCode Problem Statements Be Testing My English More Than SQL</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Sun, 13 Sep 2026 10:40:19 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/leetcode-problem-statements-be-testing-my-english-more-than-sql-3f9c</link>
      <guid>https://dev.to/saamiabbaskhan/leetcode-problem-statements-be-testing-my-english-more-than-sql-3f9c</guid>
      <description>&lt;p&gt;Currently, I am solving SQL problems on LeetCode, and believe me, the wording quality of some problems, especially the premium ones, is so poor. After spending the worst 15 minutes trying to understand the question, I finally realise that the problem is saying something completely different 😭. There is not even a proper explanation of the output on the main problem page. I do not know why. Maybe too few people are paying for premium and reporting these issues, hahaha. Anyway, I just wanted to share my frustration. Now, before solving a problem, I ask AI to explain the hidden secrets of the question without giving me the solution 😂.&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>leetcode</category>
      <category>sql</category>
      <category>programming</category>
    </item>
    <item>
      <title>How an Employee Hierarchy Helped Me Understand Recursive CTEs</title>
      <dc:creator>Saami abbas Khan</dc:creator>
      <pubDate>Fri, 28 Aug 2026 16:45:24 +0000</pubDate>
      <link>https://dev.to/saamiabbaskhan/how-an-employee-hierarchy-helped-me-understand-recursive-ctes-4541</link>
      <guid>https://dev.to/saamiabbaskhan/how-an-employee-hierarchy-helped-me-understand-recursive-ctes-4541</guid>
      <description>&lt;h2&gt;
  
  
  How I Solved the Employee Hierarchy Problem (LC 3482 for our reference), With a Recursive CTE
&lt;/h2&gt;




&lt;p&gt;Hierarchical SQL problems can feel completely different from ordinary aggregation problems.&lt;/p&gt;

&lt;p&gt;When I first attempted LeetCode 3482, I tried to solve it using the SQL concepts I already knew: joins, subqueries, grouping, and window functions. However, the organization can have an unknown number of levels. An employee may manage someone who manages another employee, who may manage another employee, and so on.&lt;/p&gt;

&lt;p&gt;A fixed number of joins can only handle a fixed number of levels. What I needed was a query that could continue moving through the organization until no additional employees were found.&lt;/p&gt;

&lt;p&gt;That is exactly what a recursive Common Table Expression, or recursive CTE, is designed to do.&lt;/p&gt;

&lt;p&gt;In this article, I will explain:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How to recognize a hierarchy problem&lt;/li&gt;
&lt;li&gt;How a recursive CTE works&lt;/li&gt;
&lt;li&gt;Why I included every employee in their own hierarchy&lt;/li&gt;
&lt;li&gt;How the query calculates team size and budget&lt;/li&gt;
&lt;li&gt;Why the hierarchy level must be taken from the CEO's perspective&lt;/li&gt;
&lt;li&gt;Why a recursive &lt;code&gt;JOIN&lt;/code&gt; is more suitable than a correlated subquery here&lt;/li&gt;
&lt;li&gt;How the complete solution fits together&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;My submitted solution performed better than 99% of accepted MySQL submissions at the time of submission. Runtime percentiles can change, but more importantly, this problem helped me understand how recursive SQL works.&lt;/p&gt;

&lt;h3&gt;
  
  
  The problem in simple terms
&lt;/h3&gt;

&lt;p&gt;We are given an &lt;code&gt;Employees&lt;/code&gt; table containing each employee's manager and salary.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;+-------------+---------------+------------+--------+-------------+
| employee_id | employee_name | manager_id | salary | department  |
+-------------+---------------+------------+--------+-------------+
| 1           | Alice         | null       | 12000  | Executive   |
| 2           | Bob           | 1          | 10000  | Sales       |
| 3           | Charlie       | 1          | 10000  | Engineering |
| 4           | David         | 2          | 7500   | Sales       |
| 5           | Eva           | 2          | 7500   | Sales       |
| 6           | Frank         | 3          | 9000   | Engineering |
| 7           | Grace         | 3          | 8500   | Engineering |
| 8           | Hank          | 4          | 6000   | Sales       |
| 9           | Ivy           | 6          | 7000   | Engineering |
| 10          | Judy          | 6          | 7000   | Engineering |
+-------------+---------------+------------+--------+-------------+
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For every employee, we need to determine:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Their level in the company hierarchy&lt;/li&gt;
&lt;li&gt;The number of employees in their complete team&lt;/li&gt;
&lt;li&gt;The total budget of that team&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The complete team includes both direct and indirect reports. The budget includes the employee's own salary as well as the salaries of all direct and indirect reports.&lt;/p&gt;

&lt;h2&gt;
  
  
  Visualizing the organization
&lt;/h2&gt;

&lt;p&gt;Before writing SQL, it helps to convert the table into a tree:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alice (1)
├── Bob (2)
│   ├── David (4)
│   │   └── Hank (8)
│   └── Eva (5)
└── Charlie (3)
    ├── Frank (6)
    │   ├── Ivy (9)
    │   └── Judy (10)
    └── Grace (7)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From this tree:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Alice is at level 1.&lt;/li&gt;
&lt;li&gt;Bob and Charlie are at level 2.&lt;/li&gt;
&lt;li&gt;David, Eva, Frank, and Grace are at level 3.&lt;/li&gt;
&lt;li&gt;Hank, Ivy, and Judy are at level 4.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Alice's complete team contains every employee below her. Bob's complete team contains David, Eva, and Hank. Charlie's complete team contains Frank, Grace, Ivy, and Judy.&lt;/p&gt;

&lt;p&gt;This is important because an ordinary self-join finds only one generation at a time. For example, joining &lt;code&gt;Employees&lt;/code&gt; to itself once can find Bob's direct reports, David and Eva, but it does not automatically continue from David to Hank.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why ordinary joins are not enough
&lt;/h2&gt;

&lt;p&gt;A self-join can find direct reports:&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;manager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;report_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="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;manager&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;report&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;manager&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We could add another join to find reports two levels below:&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;JOIN&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;second_level&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;second_level&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;report&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But then we would need another join for the third level, another for the fourth level, and so on.&lt;/p&gt;

&lt;p&gt;That approach assumes we already know the maximum depth of the organization. A recursive CTE does not require us to know that depth. It repeatedly applies the same relationship until no more matching employees exist.&lt;/p&gt;

&lt;h2&gt;
  
  
  How a recursive CTE works
&lt;/h2&gt;

&lt;p&gt;A recursive CTE has two main parts:&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;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&lt;/span&gt; &lt;span class="n"&gt;cte_name&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="c1"&gt;-- Anchor query&lt;/span&gt;

    &lt;span class="k"&gt;UNION&lt;/span&gt;

    &lt;span class="c1"&gt;-- Recursive query&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  The anchor query
&lt;/h3&gt;

&lt;p&gt;The anchor query creates the initial rows. It answers the question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Where should the recursion begin?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3&gt;
  
  
  The recursive query
&lt;/h3&gt;

&lt;p&gt;The recursive query uses rows already produced by the CTE to find the next set of rows. It answers the question:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Given the employees found in the previous step, who should be visited next?&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The database keeps executing the recursive part for newly generated rows. The recursion stops naturally when an iteration produces no more rows.&lt;/p&gt;

&lt;h2&gt;
  
  
  The key idea in my solution
&lt;/h2&gt;

&lt;p&gt;Instead of creating only one hierarchy starting from the CEO, I create a separate hierarchy starting from every employee.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;One hierarchy starts from Alice.&lt;/li&gt;
&lt;li&gt;Another starts from Bob.&lt;/li&gt;
&lt;li&gt;Another starts from Charlie.&lt;/li&gt;
&lt;li&gt;The same process is repeated for every employee.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Why do this?&lt;/p&gt;

&lt;p&gt;Because the problem asks for the complete team and budget of every employee. If I built only the CEO's hierarchy, I would know where everyone is globally, but I would still need a way to identify every employee's descendants.&lt;/p&gt;

&lt;p&gt;By building a hierarchy for every employee, all descendants of a particular employee share the same starting &lt;code&gt;employee_id&lt;/code&gt;. I can then group by that starting ID to calculate the complete team size and budget.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 1: Start one hierarchy from every employee
&lt;/h3&gt;

&lt;p&gt;The anchor query is:&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;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;reporter_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;level&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The full recursive CTE begins 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;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&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;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;reporter_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;level&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;UNION&lt;/span&gt;

    &lt;span class="k"&gt;SELECT&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;employee_id&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;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;level&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
    &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
        &lt;span class="k"&gt;ON&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;manager_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;reporter_id&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The column names can be understood as follows:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;employee_id&lt;/code&gt;: the employee whose complete hierarchy we are currently building&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reporter_id&lt;/code&gt;: the current person reached inside that hierarchy&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;level&lt;/code&gt;: the current person's distance from the starting employee, with the starting employee at level 1&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;salary&lt;/code&gt;: the salary of the current &lt;code&gt;reporter_id&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The distinction between &lt;code&gt;employee_id&lt;/code&gt; and &lt;code&gt;reporter_id&lt;/code&gt; is the most important part of the solution.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;employee_id&lt;/code&gt; remains fixed throughout one hierarchy. &lt;code&gt;reporter_id&lt;/code&gt; changes as the recursion moves downward.&lt;/p&gt;

&lt;p&gt;For Bob's hierarchy, the rows conceptually look 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;employee_id | reporter_id | level | salary
------------+-------------+-------+-------
2           | 2           | 1     | 10000
2           | 4           | 2     | 7500
2           | 5           | 2     | 7500
2           | 8           | 3     | 6000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The value &lt;code&gt;2&lt;/code&gt; remains fixed in &lt;code&gt;employee_id&lt;/code&gt; because all four rows belong to Bob's hierarchy. The &lt;code&gt;reporter_id&lt;/code&gt; changes as Bob, David, Eva, and Hank are visited.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 2: Understand the recursive join
&lt;/h3&gt;

&lt;p&gt;The recursive part is:&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;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&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;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;level&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
    &lt;span class="k"&gt;ON&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;manager_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;reporter_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For each current row in &lt;code&gt;t&lt;/code&gt;, the query searches &lt;code&gt;Employees&lt;/code&gt; for people whose &lt;code&gt;manager_id&lt;/code&gt; equals the current &lt;code&gt;reporter_id&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Suppose the current row in Bob's hierarchy is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;employee_id = 2
reporter_id = 2
level       = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The join checks:&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;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It finds David and Eva. The recursive query then produces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 | 4 | 2 | 7500
2 | 5 | 2 | 7500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The key detail is that recursion does not continue for only one returned row. If an iteration returns multiple rows, every newly returned row participates in the next recursive iteration.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;David is checked for reports.&lt;/li&gt;
&lt;li&gt;Eva is also checked for reports.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;David manages Hank, so David's row produces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;2 | 8 | 3 | 6000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Eva manages nobody, so her branch produces no additional row and ends. The recursion continues independently for every active branch until no branch can produce another employee.&lt;/p&gt;

&lt;p&gt;This was one of the most important things I learned from the problem: a recursive CTE expands every row returned by the previous iteration, not just one row.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 3: Why each employee includes themselves
&lt;/h3&gt;

&lt;p&gt;In the anchor query, I set both IDs to the same value:&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;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;reporter_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This includes every employee in their own hierarchy.&lt;/p&gt;

&lt;p&gt;At first, that can look unnecessary because an employee is not their own report. However, it makes the budget calculation much cleaner.&lt;/p&gt;

&lt;p&gt;For Bob, the required budget is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Bob     = 10000
David   =  7500
Eva     =  7500
Hank    =  6000
----------------
Budget  = 31000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because Bob is already included in his own hierarchy, the budget can be calculated with one 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;t1&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;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We do not need to calculate the reports' salaries separately and then add Bob's salary afterward.&lt;/p&gt;

&lt;p&gt;The team size should not include Bob himself. Since his hierarchy contains four rows but only three reports, it can be calculated 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;COUNT&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This is a useful SQL design pattern: sometimes deliberately including a row simplifies one calculation, and a small adjustment makes the other calculation correct.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 4: Aggregate each employee's hierarchy
&lt;/h3&gt;

&lt;p&gt;After the recursive CTE generates all hierarchies, the following query calculates each employee's team size and budget:&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;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&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;employee_name&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="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;team_size&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;t1&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;AS&lt;/span&gt; &lt;span class="n"&gt;budget&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;AS&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;
&lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
    &lt;span class="k"&gt;ON&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;employee_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;employee_id&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;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&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;employee_name&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For every starting &lt;code&gt;employee_id&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;COUNT(*) - 1&lt;/code&gt; counts all direct and indirect reports.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;SUM(t1.salary)&lt;/code&gt; adds the starting employee's salary and all descendant salaries.&lt;/li&gt;
&lt;li&gt;The join retrieves the starting employee's name.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For Bob, this produces:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;employee_id | employee_name | team_size | budget
------------+---------------+-----------+-------
2           | Bob           | 3         | 31000
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;For Charlie, the hierarchy contains Charlie, Frank, Grace, Ivy, and Judy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Charlie = 10000
Frank   =  9000
Grace   =  8500
Ivy     =  7000
Judy    =  7000
----------------
Budget  = 41500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Charlie has four reports in total, so his aggregated values are:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;employee_id | employee_name | team_size | budget
------------+---------------+-----------+-------
3           | Charlie       | 4         | 41500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Step 5: The subtle problem with &lt;code&gt;level&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The recursive CTE creates a hierarchy starting from every employee. As a result, its &lt;code&gt;level&lt;/code&gt; value is relative to the starting employee.&lt;/p&gt;

&lt;p&gt;For example, these are valid rows in different hierarchies:&lt;br&gt;
&lt;/p&gt;

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

&lt;/div&gt;



&lt;p&gt;All three rows refer to David as &lt;code&gt;reporter_id = 4&lt;/code&gt;, but they describe David from different starting points:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;David is level 3 from Alice's perspective.&lt;/li&gt;
&lt;li&gt;David is level 2 from Bob's perspective.&lt;/li&gt;
&lt;li&gt;David is level 1 from his own perspective.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The required result needs the global company level, not a relative level. Therefore, we need the hierarchy whose starting point is the CEO.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 6: Identify the CEO's hierarchy
&lt;/h3&gt;

&lt;p&gt;The CEO is the employee whose &lt;code&gt;manager_id&lt;/code&gt; is &lt;code&gt;NULL&lt;/code&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="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;employee_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="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Using the sample data, this returns Alice's ID, &lt;code&gt;1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;We then select only the recursive CTE rows whose starting &lt;code&gt;employee_id&lt;/code&gt; is the CEO:&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="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;employee_id&lt;/span&gt; &lt;span class="o"&gt;=&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;employee_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="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&lt;/span&gt;
&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This produces Alice's complete hierarchy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;employee_id | reporter_id | level
------------+-------------+------
1           | 1           | 1
1           | 2           | 2
1           | 3           | 2
1           | 4           | 3
1           | 5           | 3
1           | 6           | 3
1           | 7           | 3
1           | 8           | 4
1           | 9           | 4
1           | 10          | 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Because this hierarchy begins at the top of the organization, its levels are the global levels required by the problem.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;employee_id&lt;/code&gt; is always the CEO's ID.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;reporter_id&lt;/code&gt; identifies the employee at that level.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;That is why the final join matches the aggregated employee ID with &lt;code&gt;level_table.reporter_id&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Step 7: Combine the calculations with the global levels
&lt;/h3&gt;

&lt;p&gt;The aggregated subquery contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Employee ID&lt;/li&gt;
&lt;li&gt;Employee name&lt;/li&gt;
&lt;li&gt;Team size&lt;/li&gt;
&lt;li&gt;Budget&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The CEO-based hierarchy contains:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Each employee's global level&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The two results are joined as follows:&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;ON&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;level_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reporter_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This attaches the correct global level to every employee's aggregated result.&lt;/p&gt;

&lt;p&gt;The final ordering is:&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="k"&gt;level&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;budget&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_name&lt;/span&gt; &lt;span class="k"&gt;ASC&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



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

&lt;ol&gt;
&lt;li&gt;Employees closer to the top appear first.&lt;/li&gt;
&lt;li&gt;Employees at the same level are ordered by larger budget first.&lt;/li&gt;
&lt;li&gt;Remaining ties are resolved alphabetically by employee name.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  Complete MySQL solution
&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;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&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;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;reporter_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="k"&gt;level&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;UNION&lt;/span&gt;

    &lt;span class="k"&gt;SELECT&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;employee_id&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;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;level&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&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="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;
    &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
        &lt;span class="k"&gt;ON&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;manager_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;reporter_id&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;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;level_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="k"&gt;level&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;team_size&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;budget&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="n"&gt;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&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;employee_name&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="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;team_size&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;t1&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;AS&lt;/span&gt; &lt;span class="n"&gt;budget&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;AS&lt;/span&gt; &lt;span class="n"&gt;t1&lt;/span&gt;
    &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
        &lt;span class="k"&gt;ON&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;employee_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;employee_id&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;t1&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&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;employee_name&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;x&lt;/span&gt;
&lt;span class="k"&gt;JOIN&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;employee_id&lt;/span&gt; &lt;span class="o"&gt;=&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;employee_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="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;manager_id&lt;/span&gt; &lt;span class="k"&gt;IS&lt;/span&gt; &lt;span class="k"&gt;NULL&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;level_table&lt;/span&gt;
    &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;level_table&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;reporter_id&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;level&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;budget&lt;/span&gt; &lt;span class="k"&gt;DESC&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;x&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;employee_name&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;
  
  
  Walking through the result
&lt;/h3&gt;

&lt;p&gt;Using the sample hierarchy, the calculation for every employee is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Alice   -&amp;gt; reports: 9 -&amp;gt; budget: 84500 -&amp;gt; level: 1
Bob     -&amp;gt; reports: 3 -&amp;gt; budget: 31000 -&amp;gt; level: 2
Charlie -&amp;gt; reports: 4 -&amp;gt; budget: 41500 -&amp;gt; level: 2
David   -&amp;gt; reports: 1 -&amp;gt; budget: 13500 -&amp;gt; level: 3
Eva     -&amp;gt; reports: 0 -&amp;gt; budget:  7500 -&amp;gt; level: 3
Frank   -&amp;gt; reports: 2 -&amp;gt; budget: 23000 -&amp;gt; level: 3
Grace   -&amp;gt; reports: 0 -&amp;gt; budget:  8500 -&amp;gt; level: 3
Hank    -&amp;gt; reports: 0 -&amp;gt; budget:  6000 -&amp;gt; level: 4
Ivy     -&amp;gt; reports: 0 -&amp;gt; budget:  7000 -&amp;gt; level: 4
Judy    -&amp;gt; reports: 0 -&amp;gt; budget:  7000 -&amp;gt; level: 4
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Alice's budget is the sum of all ten salaries:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;12000 + 10000 + 10000 + 7500 + 7500
+ 9000 + 8500 + 6000 + 7000 + 7000
= 84500
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;After applying the requested ordering, Charlie appears before Bob at level 2 because Charlie has the larger budget. At level 3, Frank appears first, followed by David, Grace, and Eva according to descending budget.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why I used a &lt;code&gt;JOIN&lt;/code&gt; instead of a correlated subquery
&lt;/h3&gt;

&lt;p&gt;While learning recursion, I wondered whether a correlated subquery could replace the recursive join.&lt;/p&gt;

&lt;p&gt;The core operation is:&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;JOIN&lt;/span&gt; &lt;span class="n"&gt;Employees&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;e&lt;/span&gt;
    &lt;span class="k"&gt;ON&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;manager_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;reporter_id&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At every recursive step, we need to take the employees found in the previous step and produce a new set of employee rows. A join expresses exactly that relationship: match every current node with all of its children.&lt;/p&gt;

&lt;p&gt;A scalar correlated subquery is usually expected to return one value. Even a set-returning correlated subquery does not, by itself, repeatedly feed its results back into the same operation for an unknown number of hierarchy levels.&lt;/p&gt;

&lt;p&gt;The recursion provides the repetition, while the join provides the parent-to-child expansion. That is why recursive hierarchy queries commonly use a join in their recursive member.&lt;/p&gt;

&lt;h3&gt;
  
  
  &lt;code&gt;UNION&lt;/code&gt; versus &lt;code&gt;UNION ALL&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;My submitted solution uses &lt;code&gt;UNION&lt;/code&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="k"&gt;UNION&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;UNION&lt;/code&gt; removes duplicate rows, while &lt;code&gt;UNION ALL&lt;/code&gt; retains them and usually avoids the work required for duplicate elimination.&lt;/p&gt;

&lt;p&gt;In a valid employee tree where every employee has only one manager and there are no cycles, the recursive paths should not generate duplicate hierarchy rows. In that case, &lt;code&gt;UNION ALL&lt;/code&gt; can also be appropriate:&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;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;However, cycle handling deserves special attention in real-world systems. If invalid data allows employees to eventually report back to an employee already present in the same path, recursive traversal may repeat indefinitely or reach the database's recursion limit. Production queries may need explicit cycle detection, data constraints, or database-specific recursion safeguards.&lt;/p&gt;

&lt;p&gt;For the LeetCode problem, the input represents a valid hierarchy, so the recursive structure is safe under the problem's assumptions.&lt;/p&gt;

&lt;h3&gt;
  
  
  Complexity discussion
&lt;/h3&gt;

&lt;p&gt;Let &lt;code&gt;n&lt;/code&gt; be the number of employees and let &lt;code&gt;h&lt;/code&gt; be the total number of ancestor-descendant relationships generated across all employee hierarchies.&lt;/p&gt;

&lt;p&gt;The CTE does not create only &lt;code&gt;n&lt;/code&gt; rows. It creates one row for an employee and each employee in that person's subtree. Therefore, the generated row count depends on the shape of the organization.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;In a relatively balanced hierarchy, the number of generated relationships can be much smaller than the worst case.&lt;/li&gt;
&lt;li&gt;In a chain where every employee manages exactly one other employee, the CTE generates approximately &lt;code&gt;n + (n - 1) + ... + 1&lt;/code&gt; rows, which is quadratic.&lt;/li&gt;
&lt;li&gt;The aggregation then processes those generated hierarchy rows.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A useful high-level description is therefore:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Time: proportional to the hierarchy rows generated and processed, with a worst case of &lt;code&gt;O(n^2)&lt;/code&gt; for a chain-shaped organization&lt;/li&gt;
&lt;li&gt;Space: proportional to the generated recursive result, also up to &lt;code&gt;O(n^2)&lt;/code&gt; in the worst case&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The actual execution cost also depends on the database engine, indexes, recursive CTE implementation, and query plan. An index on &lt;code&gt;manager_id&lt;/code&gt; is especially useful in a real database because the recursive join repeatedly searches for employees reporting to a particular manager.&lt;/p&gt;

&lt;h3&gt;
  
  
  Common mistakes in this type of problem
&lt;/h3&gt;

&lt;h4&gt;
  
  
  1. Counting only direct reports
&lt;/h4&gt;

&lt;p&gt;Grouping employees by &lt;code&gt;manager_id&lt;/code&gt; finds only direct reports. It misses employees farther down the hierarchy.&lt;/p&gt;

&lt;h4&gt;
  
  
  2. Using a fixed number of self-joins
&lt;/h4&gt;

&lt;p&gt;This works only when the maximum hierarchy depth is known and small. It is not a general solution for an unknown-depth tree.&lt;/p&gt;

&lt;h4&gt;
  
  
  3. Losing the starting employee's identity
&lt;/h4&gt;

&lt;p&gt;If the recursive CTE stores only the current employee being visited, it becomes difficult to know which root employee the row belongs to. Keeping a fixed starting &lt;code&gt;employee_id&lt;/code&gt; solves this.&lt;/p&gt;

&lt;h4&gt;
  
  
  4. Using the relative level as the global level
&lt;/h4&gt;

&lt;p&gt;Because every employee begins at level 1 in their own hierarchy, those levels cannot directly be used as company-wide levels. The global level must come from the CEO's hierarchy.&lt;/p&gt;

&lt;h4&gt;
  
  
  5. Forgetting whether budget includes the employee
&lt;/h4&gt;

&lt;p&gt;The starting employee must be included in the salary sum. Including that employee in the anchor query makes the calculation straightforward.&lt;/p&gt;

&lt;h4&gt;
  
  
  6. Counting the employee as their own report
&lt;/h4&gt;

&lt;p&gt;Because the employee is included for budget calculation, team size must subtract one.&lt;/p&gt;

&lt;h4&gt;
  
  
  7. Assuming recursion processes only one row at a time
&lt;/h4&gt;

&lt;p&gt;If a recursive iteration finds several children, every child is used in the following iteration. Each branch continues independently until it reaches a leaf employee.&lt;/p&gt;

&lt;h3&gt;
  
  
  What this problem taught me
&lt;/h3&gt;

&lt;p&gt;This problem was difficult for me because it required a different way of thinking about SQL.&lt;/p&gt;

&lt;p&gt;Most SQL queries feel set-based and flat: filter rows, join tables, aggregate values, and return a result. A hierarchy is still processed using sets, but recursion allows the result of one iteration to become the input to the next iteration.&lt;/p&gt;

&lt;p&gt;The most useful lessons I took from this problem were:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Recognize unknown-depth relationships as a sign that recursion may be required.&lt;/li&gt;
&lt;li&gt;Separate the fixed root of a traversal from the current node being visited.&lt;/li&gt;
&lt;li&gt;Remember that every row produced by one recursive iteration can expand in the next.&lt;/li&gt;
&lt;li&gt;Design the CTE output around the final aggregation you need.&lt;/li&gt;
&lt;li&gt;Distinguish between a level relative to a selected employee and a level relative to the global root.&lt;/li&gt;
&lt;li&gt;Use joins in the recursive member to expand parent rows into child rows.&lt;/li&gt;
&lt;li&gt;Draw the hierarchy before writing the query.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  A reusable recursive hierarchy pattern
&lt;/h2&gt;

&lt;p&gt;The central idea can be generalized beyond this problem:&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;WITH&lt;/span&gt; &lt;span class="k"&gt;RECURSIVE&lt;/span&gt; &lt;span class="k"&gt;hierarchy&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;root_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;current_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;depth&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;starting_rows&lt;/span&gt;

    &lt;span class="k"&gt;UNION&lt;/span&gt; &lt;span class="k"&gt;ALL&lt;/span&gt;

    &lt;span class="k"&gt;SELECT&lt;/span&gt;
        &lt;span class="k"&gt;hierarchy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;root_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;child&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;span class="k"&gt;hierarchy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;depth&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="k"&gt;hierarchy&lt;/span&gt;
    &lt;span class="k"&gt;JOIN&lt;/span&gt; &lt;span class="n"&gt;source_table&lt;/span&gt; &lt;span class="k"&gt;AS&lt;/span&gt; &lt;span class="n"&gt;child&lt;/span&gt;
        &lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;child&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;parent_id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;hierarchy&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;current_id&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="k"&gt;hierarchy&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This pattern appears in many real-world scenarios:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Employee and manager structures&lt;/li&gt;
&lt;li&gt;Product categories and subcategories&lt;/li&gt;
&lt;li&gt;Folder and directory trees&lt;/li&gt;
&lt;li&gt;Bill-of-materials relationships&lt;/li&gt;
&lt;li&gt;Comment and reply threads&lt;/li&gt;
&lt;li&gt;Geographic region hierarchies&lt;/li&gt;
&lt;li&gt;Referral networks&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The names of the tables and columns change, but the thought process remains similar:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Choose the starting row or rows.&lt;/li&gt;
&lt;li&gt;Preserve the root identity.&lt;/li&gt;
&lt;li&gt;Track the current node.&lt;/li&gt;
&lt;li&gt;Join the current node to its children.&lt;/li&gt;
&lt;li&gt;Increase the depth.&lt;/li&gt;
&lt;li&gt;Stop when no children remain.&lt;/li&gt;
&lt;/ol&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;Recursive CTEs looked intimidating when I first encountered them. The syntax was not the hardest part. The real challenge was understanding what each row represented and how the result of one iteration was used to generate the next.&lt;/p&gt;

&lt;p&gt;For this solution, the breakthrough was to think in terms of two identities:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The employee whose entire team I want to calculate&lt;/li&gt;
&lt;li&gt;The current employee being visited inside that team&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Once those roles were separated into &lt;code&gt;employee_id&lt;/code&gt; and &lt;code&gt;reporter_id&lt;/code&gt;, the remaining steps became much clearer:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Recursively build every employee's subtree.&lt;/li&gt;
&lt;li&gt;Include the employee themselves to simplify budget calculation.&lt;/li&gt;
&lt;li&gt;Group each subtree to calculate team size and budget.&lt;/li&gt;
&lt;li&gt;Use the CEO's subtree to obtain global hierarchy levels.&lt;/li&gt;
&lt;li&gt;Join the results and apply the required ordering.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This was not just another SQL problem for me. It introduced me to a way of solving hierarchies that ordinary joins and aggregations cannot handle cleanly on their own.&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>algorithms</category>
      <category>programming</category>
    </item>
    <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" 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-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="" 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/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="24" height="24"&gt;
                  &lt;/span&gt;
                  &lt;span class="crayons_icon_container"&gt;
                    &lt;img src="https://assets.dev.to/assets/multi-unicorn-b44d6f8c23cdd00964192bedc38af3e82463978aa611b4365bd33a0f1f4f3e97.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;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>
  </channel>
</rss>
