<?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: Prashant Kumar Dey</title>
    <description>The latest articles on DEV Community by Prashant Kumar Dey (@unpredictableprashant).</description>
    <link>https://dev.to/unpredictableprashant</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%2F1477126%2F2d974a62-4328-40f2-855f-0de04a542a84.jpeg</url>
      <title>DEV Community: Prashant Kumar Dey</title>
      <link>https://dev.to/unpredictableprashant</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/unpredictableprashant"/>
    <language>en</language>
    <item>
      <title>5 SQL Window-Function Exercises Taken From Real Business Problems</title>
      <dc:creator>Prashant Kumar Dey</dc:creator>
      <pubDate>Thu, 22 Oct 2026 09:31:07 +0000</pubDate>
      <link>https://dev.to/unpredictableprashant/5-sql-window-function-exercises-taken-from-real-business-problems-4h47</link>
      <guid>https://dev.to/unpredictableprashant/5-sql-window-function-exercises-taken-from-real-business-problems-4h47</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;&lt;strong&gt;TL;DR:&lt;/strong&gt; SQL window functions (&lt;code&gt;OVER (PARTITION BY ... ORDER BY ...)&lt;/code&gt;) calculate across related rows without collapsing them like &lt;code&gt;GROUP BY&lt;/code&gt; does. Practise five patterns analysts use daily: running totals with &lt;code&gt;SUM() OVER&lt;/code&gt;, gaps between events with &lt;code&gt;LAG()&lt;/code&gt;, first-per-group with &lt;code&gt;ROW_NUMBER()&lt;/code&gt;, month-over-month change with &lt;code&gt;LAG()&lt;/code&gt; on an aggregate, and top-N with ties using &lt;code&gt;RANK()&lt;/code&gt;. You can run every exercise in a free browser &lt;a href="https://bartman.ai/labs/sql" rel="noopener noreferrer"&gt;SQL lab&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Window functions are the point where most SQL learners stall. The syntax isn't hard; knowing &lt;em&gt;when&lt;/em&gt; to reach for them is. These five exercises come from tasks analysts actually get. Try each one before reading the hint.&lt;/p&gt;

&lt;p&gt;Use any Postgres or MySQL 8 database, or open a free browser lab at &lt;a href="https://bartman.ai/labs/sql" rel="noopener noreferrer"&gt;bartman.ai/labs/sql&lt;/a&gt; so there's nothing to install.&lt;/p&gt;

&lt;h2&gt;
  
  
  Setup
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="k"&gt;CREATE&lt;/span&gt; &lt;span class="k"&gt;TABLE&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
  &lt;span class="n"&gt;order_id&lt;/span&gt;    &lt;span class="nb"&gt;INT&lt;/span&gt; &lt;span class="k"&gt;PRIMARY&lt;/span&gt; &lt;span class="k"&gt;KEY&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;customer_id&lt;/span&gt; &lt;span class="nb"&gt;INT&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;order_date&lt;/span&gt;  &lt;span class="nb"&gt;DATE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
  &lt;span class="n"&gt;amount&lt;/span&gt;      &lt;span class="nb"&gt;NUMERIC&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;orders&lt;/span&gt; &lt;span class="k"&gt;VALUES&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-01-04'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1200&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;102&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-01-09'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;450&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-02-02'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;800&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;103&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-02-11'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;2300&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;102&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-03-15'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;300&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;101&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-03-28'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;950&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt;
 &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;104&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-03-30'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;  &lt;span class="mi"&gt;610&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;103&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s1"&gt;'2026-04-02'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;1800&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  1. Running revenue per customer
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; For each order, show the customer's cumulative spend up to and including that order.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Hint
  &lt;br&gt;
&lt;code&gt;SUM(amount) OVER (PARTITION BY customer_id ORDER BY order_date)&lt;/code&gt;&lt;br&gt;


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

&lt;h2&gt;
  
  
  2. Days since the previous order
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; For each order, how many days since the same customer's previous order? First orders should show NULL.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Hint
  &lt;br&gt;
&lt;code&gt;order_date - LAG(order_date) OVER (PARTITION BY customer_id ORDER BY order_date)&lt;/code&gt;&lt;br&gt;


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

&lt;h2&gt;
  
  
  3. Each customer's first order only
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Return one row per customer: their first order. No &lt;code&gt;GROUP BY&lt;/code&gt; joins back to the table.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Hint
  &lt;br&gt;
&lt;code&gt;ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY order_date)&lt;/code&gt; in a CTE, then filter &lt;code&gt;= 1&lt;/code&gt;.&lt;br&gt;


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

&lt;h2&gt;
  
  
  4. Month-over-month revenue change
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; Total revenue per month, plus the percentage change from the previous month.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Hint
  &lt;br&gt;
Aggregate by &lt;code&gt;DATE_TRUNC('month', order_date)&lt;/code&gt; in a CTE, then &lt;code&gt;LAG(total) OVER (ORDER BY month)&lt;/code&gt;.&lt;br&gt;


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

&lt;h2&gt;
  
  
  5. Top customer each month, ties included
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;Task:&lt;/strong&gt; For each month, the customer(s) with the highest spend. If two tie, show both.&lt;/p&gt;

&lt;p&gt;&lt;/p&gt;
  Hint
  &lt;br&gt;
&lt;code&gt;RANK()&lt;/code&gt;, not &lt;code&gt;ROW_NUMBER()&lt;/code&gt;. That one word is the whole exercise.&lt;br&gt;


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

&lt;h2&gt;
  
  
  Key takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;A window function keeps every row; &lt;code&gt;GROUP BY&lt;/code&gt; collapses rows.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;PARTITION BY&lt;/code&gt; sets the group, &lt;code&gt;ORDER BY&lt;/code&gt; sets the sequence inside it.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;LAG()&lt;/code&gt; and &lt;code&gt;LEAD()&lt;/code&gt; compare a row with its neighbours.&lt;/li&gt;
&lt;li&gt;Use &lt;code&gt;RANK()&lt;/code&gt; when ties should share a position, &lt;code&gt;ROW_NUMBER()&lt;/code&gt; when you need exactly one row.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  FAQ
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;What is a window function in SQL?&lt;/strong&gt;&lt;br&gt;
A function that calculates a value across a set of rows related to the current row, defined by an &lt;code&gt;OVER()&lt;/code&gt; clause, while still returning every individual row.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;What is the difference between RANK, DENSE_RANK and ROW_NUMBER?&lt;/strong&gt;&lt;br&gt;
&lt;code&gt;ROW_NUMBER()&lt;/code&gt; gives unique numbers even for ties. &lt;code&gt;RANK()&lt;/code&gt; gives ties the same number and skips the next ones (1, 1, 3). &lt;code&gt;DENSE_RANK()&lt;/code&gt; gives ties the same number without gaps (1, 1, 2).&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Do MySQL and PostgreSQL both support window functions?&lt;/strong&gt;&lt;br&gt;
Yes. PostgreSQL has supported them since 8.4 and MySQL since 8.0. SQLite supports them from 3.25.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Where can I practise SQL window functions online for free?&lt;/strong&gt;&lt;br&gt;
In any browser-based SQL sandbox. &lt;a href="https://bartman.ai/labs/sql" rel="noopener noreferrer"&gt;Bartman's SQL lab&lt;/a&gt; needs no install, and its projects check your query against the expected result.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where to go next
&lt;/h2&gt;

&lt;p&gt;Once these feel easy, the real test is applying them to a messy business question: churn by signup cohort, multi-touch attribution, or receivables ageing. Those are full projects rather than snippets. I put a few together on Bartman, for example &lt;a href="https://bartman.ai/project/sql-subscription-churn-ctes-window-functions" rel="noopener noreferrer"&gt;subscription churn with CTEs and window functions&lt;/a&gt; and &lt;a href="https://bartman.ai/project/sql-ecommerce-funnel-cohort-retention" rel="noopener noreferrer"&gt;e-commerce funnel and cohort retention&lt;/a&gt;, where your queries are run and checked against the expected result.&lt;/p&gt;

&lt;p&gt;Post your solution to #5 in the comments. I'm curious how many people reach for &lt;code&gt;ROW_NUMBER()&lt;/code&gt; first.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Written with help from AI tools.&lt;/em&gt;&lt;/p&gt;

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