<?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: Ihsan the analyst</title>
    <description>The latest articles on DEV Community by Ihsan the analyst (@ihsantheanalyst).</description>
    <link>https://dev.to/ihsantheanalyst</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%2F4262389%2Fcecd8336-3297-4fc4-b50d-c55c72c690b1.png</url>
      <title>DEV Community: Ihsan the analyst</title>
      <link>https://dev.to/ihsantheanalyst</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ihsantheanalyst"/>
    <language>en</language>
    <item>
      <title>15 SQL window function problems you can run right now (with answers)</title>
      <dc:creator>Ihsan the analyst</dc:creator>
      <pubDate>Tue, 24 Nov 2026 10:31:37 +0000</pubDate>
      <link>https://dev.to/ihsantheanalyst/15-sql-window-function-problems-you-can-run-right-now-with-answers-pf0</link>
      <guid>https://dev.to/ihsantheanalyst/15-sql-window-function-problems-you-can-run-right-now-with-answers-pf0</guid>
      <description>&lt;p&gt;Window functions are the part of SQL that separates "I can write a query" from "I actually know SQL." Most tutorials explain the syntax and stop — but the only way it clicks is to solve problems.&lt;/p&gt;

&lt;p&gt;So here are a few, built on one tiny sales table so you can run them anywhere (Postgres, MySQL 8+, SQL Server, SQLite 3.25+).&lt;/p&gt;

&lt;h2&gt;
  
  
  1. Number each sale per region, newest first
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="nb"&gt;ROW_NUMBER&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&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;region&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;sale_date&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;The key insight: &lt;code&gt;PARTITION BY&lt;/code&gt; restarts the count for each group, and &lt;code&gt;ORDER BY&lt;/code&gt; decides what "first" means.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Running total of sales over time
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="nb"&gt;SUM&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;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;sale_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;No &lt;code&gt;PARTITION BY&lt;/code&gt; = one running total for everything. Add &lt;code&gt;PARTITION BY region&lt;/code&gt; and you get a running total per region.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. Compare each sale to the rep's previous sale
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="nb"&gt;LAG&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;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;rep&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;sale_date&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The gotcha: the first row has no previous row, so it returns &lt;code&gt;NULL&lt;/code&gt;. Fix it with a default: &lt;code&gt;LAG(amount, 1, 0)&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The mistake that still catches me
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;LAST_VALUE()&lt;/code&gt; only looks as far as the current row by default, so it quietly returns the wrong answer until you set the frame to &lt;code&gt;UNBOUNDED FOLLOWING&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Want all 15?
&lt;/h2&gt;

&lt;p&gt;I put together 15 of these — ranking, running totals, LAG/LEAD, NTILE — each with a ready-to-run answer and a short explanation, built on that one simple table.&lt;/p&gt;

&lt;p&gt;Full set here: &lt;a href="https://eteasmasher.com/sql-window-functions-practice-problems/" rel="noopener noreferrer"&gt;SQL Window Functions: 15 Practice Problems (with Answers)&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;What's the window function that took you longest to really understand?&lt;/p&gt;

</description>
      <category>sql</category>
      <category>database</category>
      <category>beginners</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
