<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Eng. M. Rizwan on Medium]]></title>
        <description><![CDATA[Stories by Eng. M. Rizwan on Medium]]></description>
        <link>https://medium.com/@ec.mrizwan?source=rss-adacb38ab02b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*r1P_oAe6s6rrPmX8</url>
            <title>Stories by Eng. M. Rizwan on Medium</title>
            <link>https://medium.com/@ec.mrizwan?source=rss-adacb38ab02b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 05:44:06 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ec.mrizwan/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[4 Advanced SQL ROWS Window Function Interview Questions with Practical Examples and Step-by-Step…]]></title>
            <link>https://medium.com/@ec.mrizwan/4-advanced-sql-rows-window-function-interview-questions-with-practical-examples-and-step-by-step-76ca33f59e7c?source=rss-adacb38ab02b------2</link>
            <guid isPermaLink="false">https://medium.com/p/76ca33f59e7c</guid>
            <category><![CDATA[advanced-sql]]></category>
            <category><![CDATA[sql-for-beginners]]></category>
            <category><![CDATA[sql-window-functions]]></category>
            <category><![CDATA[sql-interview-questions]]></category>
            <category><![CDATA[sql]]></category>
            <dc:creator><![CDATA[Eng. M. Rizwan]]></dc:creator>
            <pubDate>Sat, 11 Jul 2026 15:35:19 GMT</pubDate>
            <atom:updated>2026-07-11T15:35:19.131Z</atom:updated>
            <content:encoded><![CDATA[<h3>4 Advanced SQL ROWS Window Function Interview Questions with Practical Examples and Step-by-Step Solutions</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0GJxKIRAfyPj4Jq9IH1eQw.png" /></figure><p>Learn how to use the<a href="https://medium.com/@ec.mrizwan/sql-rows-window-function-complete-guide-with-examples-2026-042fa9022d37"> advanced SQL ROWS window function</a> with practical examples.</p><h3>Example 1: Running Total</h3><p>Calculates a cumulative (running) total of employee salaries.</p><pre>SELECT<br>    EmployeeID,<br>    Salary,<br>    SUM(Salary) OVER (<br>        ORDER BY EmployeeID<br>        ROWS BETWEEN UNBOUNDED PRECEDING<br>        AND CURRENT ROW<br>    ) AS TotalSalary<br>FROM Employees;</pre><p><strong>Output</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*9bW6P4PfpkgknIGmTQ2cGQ.png" /><figcaption><strong>Output</strong></figcaption></figure><h3>Explanation:</h3><h4>Iteration 1:</h4><p><strong>Current row: EmployeeID = 1</strong></p><p>· SQL starts from the first row because of UNBOUNDED PRECEDING.</p><p>· There are no previous rows.</p><p>· SQL includes only the current row.</p><p>Calculation:</p><pre>1000 = 1000</pre><p>Result:</p><pre>TotalSalary = 1000</pre><h4>Iteration 2:</h4><p><strong>Current row: EmployeeID = 2</strong></p><p>· SQL starts from the first employee.</p><p>· It includes the previous row (EmployeeID = 1) and the current row (EmployeeID = 2).</p><p>Calculation:</p><pre>1000 + 2000 = 3000</pre><p>Result:</p><pre>TotalSalary = 3000</pre><h4>Iteration 3:</h4><p><strong>Current row: EmployeeID = 3</strong></p><p>· SQL starts from the first employee.</p><p>· It includes EmployeeID 1, 2, and the current row (EmployeeID = 3).</p><p>Calculation:</p><pre>1000 + 2000 + 3000 = 6000</pre><p>Result:</p><pre>TotalSalary = 6000</pre><h4>Iteration 4:</h4><p><strong>Current row: EmployeeID = 4</strong></p><p>· SQL starts from the first employee.</p><p>· It includes EmployeeID 1, 2, 3, and the current row (EmployeeID = 4).</p><p>Calculation:</p><pre>1000 + 2000 + 3000 + 4000 = 10000</pre><p>Result:</p><pre>TotalSalary = 10000</pre><h4>Iteration 5:</h4><p>· SQL checks for the next current row.</p><p>· No more rows are available.</p><p>· The calculation stops.</p><h4>Final Understanding</h4><p>At every step, the <strong>current row moves forward</strong>, and the window frame expands because UNBOUNDED PRECEDING always starts from the first row.</p><p>That is why the total keeps increasing:</p><pre>1000 → 3000 → 6000 → 10000</pre><h3>Example 2: 7-Day Moving Average</h3><p>Calculates the average sales for the current day and the previous 6 days (7 days total).</p><pre>SELECT SalesDate,<br>    SalesAmount,<br>    AVG(SalesAmount) OVER (<br>        ORDER BY SalesDate<br>        ROWS BETWEEN 6 PRECEDING<br>        AND CURRENT ROW<br>    ) AS SevenDayAverage<br>FROM DailySales;</pre><p><strong>Output</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/813/1*UlYaubueDrrHHVoohh_6jg.png" /></figure><h3>Understanding the Window Frame</h3><p>ROWS BETWEEN 6 PRECEDING AND CURRENT ROW means:</p><ul><li>Start from <strong>6 rows before the current row</strong></li><li>Include the <strong>current row</strong></li><li>Calculate the average using those rows</li></ul><p>Maximum rows included in the window frame:</p><p><strong>6 previous rows + current row = 7 rows</strong></p><h4>Iteration 1:</h4><h4>Current Row: 2026–01–01</h4><ul><li>SQL looks for 6 previous rows.</li><li>No previous rows exist.</li><li>SQL includes only the current row.</li></ul><p>Calculation:</p><pre>AVG(100) = 100</pre><p>Result:</p><pre>SevenDayAverage = 100</pre><h4>Iteration 2:</h4><h4>Current Row: 2026–01–02</h4><p>SQL includes:</p><ul><li>2026–01–01 (previous row)</li><li>2026–01–02 (current row)</li></ul><p>Calculation:</p><pre>(100 + 120) / 2 = 110</pre><p>Result:</p><pre>SevenDayAverage = 110</pre><h4>Iteration 3:</h4><h4>Current Row: 2026–01–03</h4><p>SQL includes:</p><ul><li>2026–01–01</li><li>2026–01–02</li><li>2026–01–03 (current row)</li></ul><p>Calculation:</p><pre>(100 + 120 + 150) / 3 = 123.33</pre><p>Result:</p><pre>SevenDayAverage = 123.33</pre><h4>Iteration 4:</h4><h4>Current Row: 2026–01–04</h4><p>SQL includes:</p><ul><li>2026–01–01</li><li>2026–01–02</li><li>2026–01–03</li><li>2026–01–04 (current row)</li></ul><p>Calculation:</p><pre>(100 + 120 + 150 + 180) / 4 = 137.50</pre><p>Result:</p><pre>SevenDayAverage = 137.50</pre><h4>Iteration 5:</h4><h4>Current Row: 2026–01–05</h4><p>SQL includes:</p><ul><li>2026–01–01</li><li>2026–01–02</li><li>2026–01–03</li><li>2026–01–04</li><li>2026–01–05 (current row)</li></ul><p>Calculation:</p><pre>(100 + 120 + 150 + 180 + 170) / 5 = 144</pre><p>Result:</p><pre>SevenDayAverage = 144</pre><h4>Iteration 6:</h4><h4>Current Row: 2026–01–06</h4><p>SQL includes:</p><ul><li>2026–01–01</li><li>2026–01–02</li><li>2026–01–03</li><li>2026–01–04</li><li>2026–01–05</li><li>2026–01–06 (current row)</li></ul><p>Calculation:</p><pre>(100 + 120 + 150 + 180 + 170 + 200) / 6<br>= 153.33</pre><p>Result:</p><pre>SevenDayAverage = 153.33</pre><h4>Iteration 7:</h4><h4>Current Row: 2026–01–07</h4><p>SQL now has 6 previous rows available.</p><p>Window frame includes:</p><ul><li>2026–01–01</li><li>2026–01–02</li><li>2026–01–03</li><li>2026–01–04</li><li>2026–01–05</li><li>2026–01–06</li><li>2026–01–07 (current row)</li></ul><p>Calculation:</p><pre>(100 + 120 + 150 + 180 + 170 + 200 + 220) / 7<br>= 162.86</pre><p>Result:</p><pre>SevenDayAverage = 162.86</pre><h4>Iteration 8:</h4><h4>Current Row: 2026–01–08</h4><p>Now the window frame can contain only 7 rows.</p><p>SQL removes the oldest row (2026–01–01) and moves forward.</p><p>Window frame:</p><ul><li>2026–01–02</li><li>2026–01–03</li><li>2026–01–04</li><li>2026–01–05</li><li>2026–01–06</li><li>2026–01–07</li><li>2026–01–08 (current row)</li></ul><p>Calculation:</p><pre>(120 + 150 + 180 + 170 + 200 + 220 + 210) / 7<br>= 178.57</pre><p>Result:</p><pre>SevenDayAverage = 178.57</pre><h4>Final Output</h4><p>In the first 6 rows, SQL uses all available previous rows. After the 7th row, the window frame becomes a fixed size of 7 rows and starts sliding forward. This is why it is called a <strong>7-day moving average</strong>.</p><h3>Example 3: Rolling Sum</h3><p>Calculates the sum of revenue for the current month and the previous two months (3-month rolling total).</p><pre>SELECT Month,<br>    Revenue,<br>    SUM(Revenue) OVER (<br>        ORDER BY Month<br>        ROWS BETWEEN 2 PRECEDING<br>        AND CURRENT ROW<br>    ) AS RollingRevenue<br>FROM MonthlyRevenue;</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/857/1*4MWD9Ja9QFbXrGQ1R3ydPw.png" /></figure><p>Let’s explain this query step-by-step with <strong>5 iterations</strong>.</p><p><strong>Understanding the Window Frame</strong></p><pre>ROWS BETWEEN 2 PRECEDING AND CURRENT ROW</pre><p>means:</p><ul><li>Include <strong>2 rows before the current row</strong></li><li>Include the <strong>current row</strong></li><li>Total window frame size = <strong>3 rows</strong> (2 previous rows + current row)</li></ul><h4>Iteration 1:</h4><h4>Current Row: Jan</h4><ul><li>SQL looks for 2 previous rows.</li><li>No previous rows are available.</li><li>SQL includes only the current row.</li></ul><p>Month Revenue: Jan = 10000 (Current Row)</p><p>Calculation:</p><pre>10000 = 10000</pre><p>Result:</p><pre>RollingRevenue = 10000</pre><h4>Iteration 2:</h4><h4>Current Row: Feb</h4><ul><li>SQL includes the previous row and the current row. As per frame required 2 previous row + current row but at this stage found only 1 pervious row Jan.</li></ul><p>Month Revenue : Jan = 10000 , Feb = 15000 (Current Row)</p><p>Calculation:</p><pre>10000 + 15000 = 25000</pre><p>Result:</p><pre>RollingRevenue = 25000</pre><h4>Iteration 3:</h4><h4>Current Row: Mar</h4><ul><li>SQL includes 2 previous rows and the current row.</li></ul><p>Month Revenue : Jan = 10000, Feb = 15000, Mar = 12000 (Current Row)</p><p>Calculation:</p><pre>10000 + 15000 + 12000 = 37000</pre><p>Result:</p><pre>RollingRevenue = 37000</pre><h4>Iteration 4:</h4><h4>Current Row: Apr</h4><ul><li>SQL moves to the next row.</li><li>It includes the previous 2 months and the current month.</li></ul><p>Window Frame:</p><p>Month Revenue: Feb = 15000, Mar = 12000, Apr = 18000 (Current Row)</p><p>Calculation:</p><pre>Sum(15000 + 12000 + 18000 = 45000</pre><p>Result:</p><pre>RollingRevenue = 45000</pre><p>Notice that <strong>Jan is removed</strong> because the window frame only keeps <strong>2 previous rows + current row</strong>.</p><h4>Iteration 5:</h4><h4>Current Row: May</h4><p>Window Frame:</p><p>Month Revenue: Mar = 12000, Apr = 18000, May = 20000 (Current Row)</p><p>Calculation:</p><pre>12000 + 18000 + 20000 = 50000</pre><p>Result:</p><pre>RollingRevenue = 50000</pre><h4>Key Understanding</h4><p>At the beginning, SQL uses all available rows because there are not enough previous rows. Once SQL reaches the 3rd row, the window frame reaches its maximum size:</p><p><strong>2 Previous Rows + Current Row = 3 Rows</strong></p><p>After that, the window frame starts <strong>sliding forward</strong>:</p><ul><li>The oldest row leaves the frame.</li><li>The new current row enters the frame.</li></ul><p>This is why it is called a <strong>3-month rolling sum</strong>.</p><h3>Example 4: Sum of the Previous Two Rows</h3><p>Calculates the total price of only the previous two products. The current product is not included.</p><pre>SELECT ProductID,<br>    Price,<br>    SUM(Price) OVER (<br>        ORDER BY ProductID<br>        ROWS BETWEEN 2 PRECEDING<br>        AND 1 PRECEDING<br>    ) AS PreviousTwoTotal<br>FROM Products;</pre><p>In this example, the current row is <strong>not included</strong> in the calculation. SQL sums only the <strong>two rows immediately before the current row</strong>.</p><p>Output</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/646/1*8xIlpZ9mW7wBXb7tOx-vWw.png" /></figure><p>Let’s explain this query step-by-step with <strong>5 iterations</strong>.</p><p><strong>Understanding the Window Frame</strong></p><pre>ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING</pre><p>means:</p><ul><li>Start from <strong>2 rows before the current row</strong></li><li>End at <strong>1 row before the current row</strong></li><li><strong>Do not include the current row</strong></li></ul><p>So SQL only calculates the sum of the <strong>previous two rows</strong>.</p><h4>Iteration 1:</h4><h4>Current Row: ProductID = 1</h4><ul><li>SQL looks for the previous 2 rows.</li><li>No previous rows exist.</li><li>The window frame is empty.</li></ul><p>Calculation:</p><pre>No rows available</pre><p>Result:</p><pre>PreviousTwoTotal = NULL</pre><h4>Iteration 2:</h4><h4>Current Row: ProductID = 2</h4><ul><li>SQL looks at rows before the current row.</li><li>Only ProductID = 1 is available.</li></ul><p>Window Frame:</p><p>ProductID 1 = Price1500</p><p>Calculation:</p><pre>500 = 500</pre><p>Result:</p><pre>PreviousTwoTotal = 500</pre><h4>Iteration 3:</h4><h4>Current Row: ProductID = 3</h4><ul><li>SQL looks at the previous 2 rows.</li><li>It includes ProductID = 1 and ProductID = 2.</li><li>Current row (ProductID = 3) is excluded.</li></ul><p>Window Frame:</p><p>ProductID 1 Price = 500, ProductID 2 Price = 2700</p><p>Calculation:</p><pre>500 + 700 = 1200</pre><p>Result:</p><pre>PreviousTwoTotal = 1200</pre><h4>Iteration 4:</h4><h4>Current Row: ProductID = 4</h4><ul><li>SQL moves forward.</li><li>It includes the two rows before ProductID = 4.</li></ul><p>Window Frame:</p><p>ProductID 2 Price = 700, ProductID 3 Price = 600</p><p>Calculation:</p><pre>700 + 600 = 1300</pre><p>Result:</p><pre>PreviousTwoTotal = 1300</pre><p>Notice that ProductID = 1 is removed because SQL only keeps the <strong>previous two rows</strong>.</p><h4>Iteration 5:</h4><h4>Current Row: ProductID = 5</h4><ul><li>SQL looks at the previous 2 rows.</li><li>It includes ProductID = 3 and ProductID = 4.</li><li>Current row (ProductID = 5) is excluded.</li></ul><p><strong>Window Frame:</strong></p><p>ProductID 3 Price = 600, ProductID 4 Price = 4900</p><p>Calculation:</p><pre>600 + 900 = 1500</pre><p>Result:</p><pre>PreviousTwoTotal = 1500</pre><h3>Key Understanding</h3><p>Unlike:</p><pre>ROWS BETWEEN 2 PRECEDING AND CURRENT ROW</pre><p>which includes the current row:</p><pre>Previous rows + Current row</pre><p>this frame:</p><pre>ROWS BETWEEN 2 PRECEDING AND 1 PRECEDING</pre><p>only looks backward:</p><pre>Previous 2 rows only (Current row excluded)</pre><p>This is useful when you need to compare the current row with previous values, such as calculating previous period totals, changes, or trends.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=76ca33f59e7c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[SQL ROWS Window Function: Complete Guide with Examples (2026)]]></title>
            <link>https://medium.com/@ec.mrizwan/sql-rows-window-function-complete-guide-with-examples-2026-042fa9022d37?source=rss-adacb38ab02b------2</link>
            <guid isPermaLink="false">https://medium.com/p/042fa9022d37</guid>
            <category><![CDATA[advanced-sql]]></category>
            <category><![CDATA[sql-window-functions]]></category>
            <dc:creator><![CDATA[Eng. M. Rizwan]]></dc:creator>
            <pubDate>Sat, 11 Jul 2026 14:20:05 GMT</pubDate>
            <atom:updated>2026-07-11T15:37:23.624Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*0GJxKIRAfyPj4Jq9IH1eQw.png" /><figcaption>Advanced SQL Window</figcaption></figure><p>The ROWS clause is part of <strong>Advanced SQL</strong> and enables complex calculations that are difficult or sometimes impossible — to achieve using GROUP BY alone. It is widely used in business intelligence, financial reporting, sales analytics, and data warehousing.</p><p>If you’re preparing for <strong>advanced SQL interviews</strong> or working with analytical queries, having a solid understanding of the <strong>ROWS</strong> clause is essential. It is a common topic in mid-level and senior SQL interviews, where employers expect candidates to write efficient window function queries. However, if you’re preparing for <strong>junior-level SQL roles</strong>, your primary focus should be on mastering core SQL concepts such as SELECT, WHERE, JOIN, GROUP BY, HAVING, aggregate functions, and subqueries before moving on to advanced window functions like <strong>ROWS</strong>.</p><h3>What is the ROWS Window Function?</h3><p>The <strong>ROWS</strong> clause is a part of SQL window functions that defines the <strong>physical number of rows</strong> included in the <strong>window frame</strong> for each row during a calculation.</p><p>It works together with the OVER() clause and determines which rows participate in calculations such as:</p><ul><li>Running totals</li><li>Moving averages</li><li>Rolling sums</li><li>Ranking analysis</li><li>Financial reporting</li><li>Time-series analysis</li></ul><p>Before we learn how the <strong>ROWS</strong> clause works, let’s understand two important terms used in the above definition:</p><ul><li><strong>Physical rows</strong></li><li><strong>Window frame</strong></li></ul><h3>What are Physical Rows?</h3><p><strong>Physical rows</strong> means SQL counts the <strong>actual row positions</strong> in the ordered result set, <strong>not the values</strong> in a column.</p><p>The <strong>current row</strong> acts as the <strong>reference point</strong> (or center) for the calculation. From this current row, SQL can move:</p><ul><li><strong>Backward</strong> using PRECEDING</li><li><strong>Forward</strong> using FOLLOWING</li></ul><p>by counting rows one by one.</p><p>For example, if the current row is the <strong>5th row</strong> and you specify:</p><pre>ROWS BETWEEN 2 PRECEDING (Backward) AND CURRENT ROW</pre><p>SQL counts <strong>two physical rows before the current row</strong> (3rd and 4th rows) and includes the current row (5th row) in the calculation.</p><h3>What is a Window Frame?</h3><p>A <strong>window frame</strong> is the <strong>group of rows</strong> that a window function uses to perform its calculation for the current row.</p><h3>What is a Sliding Window?</h3><p>The window frame does not remain fixed. As SQL processes each row in the result set, the window frame moves along with the current row. This moving behavior is called a <strong>sliding window</strong>.</p><h3>Simple Analogy</h3><p>Imagine <strong>10 people</strong> standing in a line. You are currently the <strong>5th person</strong>.</p><p>You tell SQL:</p><blockquote><strong><em>“Include me and the two people standing before me.”</em></strong></blockquote><p>SQL selects these three people:</p><ul><li>3rd person</li><li>4th person</li><li>5th person (<strong>current row</strong>)</li></ul><p>These three people form the <strong>window frame</strong>.</p><p>Now SQL performs the calculation (such as SUM(), AVG(), or COUNT()) using only these selected rows.</p><p>Next, SQL moves to the <strong>6th person</strong>. Since the current row has changed, the window frame also moves and now includes:</p><ul><li>4th person</li><li>5th person</li><li>6th person (<strong>current row</strong>)</li></ul><p>Notice that the group of selected rows moves forward as SQL processes each new row. This moving group is called a <strong>sliding window</strong>, while the group of rows used for each calculation is called the <strong>window frame</strong>.</p><h3>SQL ROWS Syntax</h3><pre>SELECT<br>column_name,<br>window_function() OVER (<br>PARTITION BY partition_column<br>ORDER BY order_column<br>ROWS window_frame<br>)<br>FROM table_name;</pre><h3>Components inside OVER() Clause</h3><ul><li><strong>PARTITION BY</strong> — Divides data into groups.</li><li><strong>ORDER BY</strong> — Defines row order.</li><li><strong>ROWS</strong> — Specifies the window frame.</li><li><strong>Window Function</strong> — SUM(), AVG(), COUNT(), MIN(), MAX(), RANK(), LEAD(), LAG() etc.</li></ul><h3>ROWS Window Frame Options</h3><p>The <strong>ROWS</strong> clause lets you define exactly which rows should be included in the window frame for each calculation. SQL provides several options to specify the start and end of the window frame.</p><h3>1. UNBOUNDED PRECEDING</h3><p>UNBOUNDED PRECEDING means <strong>there is no limit</strong> on how many rows to include before the current row. SQL starts from the <strong>first row of the partition</strong> and includes every row up to the current row.</p><ul><li><strong>UNBOUNDED</strong> = No limit or boundary.</li><li><strong>PRECEDING</strong> = Before the current row.</li></ul><h3>Syntax</h3><pre>ROWS BETWEEN UNBOUNDED PRECEDING<br>AND CURRENT ROW</pre><h3>Example</h3><p><strong>Calculate the running total of sales ordered by </strong><strong>OrderID.</strong></p><pre>SELECT<br>    OrderID,<br>    Sales,<br>    SUM(Sales) OVER (<br>        ORDER BY OrderID<br>        ROWS BETWEEN UNBOUNDED PRECEDING<br>        AND CURRENT ROW<br>    ) AS RunningTotal<br>FROM Orders;</pre><p><strong>Explanation:</strong></p><ul><li>For <strong>OrderID = 1</strong>, SQL adds only the first row → <strong>100</strong></li><li>For <strong>OrderID = 2</strong>, SQL adds rows 1 and 2 → <strong>100 + 200 = 300</strong></li><li>For <strong>OrderID = 3</strong>, SQL adds rows 1, 2, and 3 → <strong>450</strong></li><li>For <strong>OrderID = 4</strong>, SQL adds all rows from the beginning through row 4 → <strong>700</strong></li></ul><p>This is known as a <strong>running total</strong> or <strong>cumulative sum</strong>.</p><h3>2. CURRENT ROW</h3><p>CURRENT ROW means SQL includes <strong>only the current row</strong> in the window frame.</p><h3>Syntax</h3><pre>ROWS BETWEEN CURRENT ROW<br>AND CURRENT ROW</pre><p>Since the window frame contains only one row, the calculation is performed only on the current row.</p><p>This option is useful when you want to explicitly define a <strong>single-row window frame</strong>.</p><h3>3. N PRECEDING</h3><p>Unlike UNBOUNDED PRECEDING, which includes all previous rows, N PRECEDING lets you specify <strong>exactly how many previous rows</strong> should be included in the window frame.</p><p>The window frame consists of:</p><ul><li><strong>N previous rows</strong></li><li><strong>The current row</strong></li></ul><h3>Syntax</h3><pre>ROWS BETWEEN 2 PRECEDING<br>AND CURRENT ROW</pre><p>Here, SQL includes:</p><ul><li>2 previous rows</li><li>Current row</li></ul><p>Total rows in the window frame = <strong>3</strong></p><h3>Example</h3><pre>SELECT<br>    Sales,<br>    AVG(Sales) OVER (<br>        ORDER BY OrderID<br>        ROWS BETWEEN 2 PRECEDING<br>        AND CURRENT ROW<br>    ) AS MovingAverage<br>FROM Orders;</pre><p>This calculates a <strong>3-row moving average</strong>.</p><h3>4. N FOLLOWING</h3><p>N FOLLOWING includes the current row and a specified number of rows <strong>after</strong> the current row.</p><ul><li><strong>FOLLOWING</strong> = After the current row.</li></ul><h3>Syntax</h3><pre>ROWS BETWEEN CURRENT ROW<br>AND 2 FOLLOWING</pre><p>Here, SQL includes:</p><ul><li>Current row</li><li>Next 2 rows</li></ul><p>This option is commonly used for <strong>forecasting</strong>, <strong>future trend analysis</strong>, and <strong>forward-looking calculations</strong>.</p><h3>5. UNBOUNDED FOLLOWING</h3><p>UNBOUNDED FOLLOWING means SQL includes <strong>all rows from the current row to the end of the partition</strong>.</p><ul><li><strong>UNBOUNDED</strong> = No limit or boundary.</li><li><strong>FOLLOWING</strong> = After the current row.</li></ul><h3>Syntax</h3><pre>ROWS BETWEEN CURRENT ROW<br>AND UNBOUNDED FOLLOWING</pre><p>This is useful when you need calculations that consider all remaining rows.</p><p>Prepare for SQL interviews with <a href="https://medium.com/@ec.mrizwan/4-advanced-sql-rows-window-function-interview-questions-with-practical-examples-and-step-by-step-76ca33f59e7c">4 practical examples of advanced SQL ROWS window functions</a>. This guide covers real-world interview scenarios including running totals, moving averages, row-to-row comparisons, and cumulative calculations. Each example is explained step by step with SQL queries and iterations to help beginners and experienced developers understand how window frames work behind the database engine.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=042fa9022d37" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>