<?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 Khuddus  on Medium]]></title>
        <description><![CDATA[Stories by Khuddus  on Medium]]></description>
        <link>https://medium.com/@khuddus216?source=rss-d2947477caa2------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*nRhXNEMnEpR8ChYs.jpg</url>
            <title>Stories by Khuddus  on Medium</title>
            <link>https://medium.com/@khuddus216?source=rss-d2947477caa2------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 03 Dec 2026 18:03:19 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@khuddus216/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[SQL WINDOW FUNCTIONS]]></title>
            <link>https://medium.com/@khuddus216/sql-window-functions-2d7323a52f4d?source=rss-d2947477caa2------2</link>
            <guid isPermaLink="false">https://medium.com/p/2d7323a52f4d</guid>
            <category><![CDATA[lag]]></category>
            <category><![CDATA[dense-rank]]></category>
            <category><![CDATA[sql-window-functions]]></category>
            <category><![CDATA[window-functions]]></category>
            <category><![CDATA[rank]]></category>
            <dc:creator><![CDATA[Khuddus ]]></dc:creator>
            <pubDate>Fri, 13 Jun 2025 17:00:10 GMT</pubDate>
            <atom:updated>2025-06-13T17:00:10.302Z</atom:updated>
            <content:encoded><![CDATA[<p>In this article I will be showing some examples of <strong>WINDOW</strong> Functions in SQL.</p><p>These are used for ADVANCED ANALYTICAL QUERIES for getting accurate results/rank/Unique Values.</p><p>Some Window Functions are discussed here <strong>RANK(), DENSE_RANK(), ROW_NUMBER(), LEAD() and LAG().</strong></p><p>Lets Explains this in briefly: -</p><p>For Example, I have student wise marks in the table as <strong>student_marks_tb</strong></p><p>Syntax: - <strong>SELECT * from student_marks_tb;</strong></p><p>Let’s see in Figure 1:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/415/1*Casq_IbxqWFhOnm4766rLQ.png" /><figcaption><strong>Figure 1: MySQL Example student_marks_tb</strong></figcaption></figure><p>In this data, let’s assume we need to add rank to the student based on the marks scored.</p><p>— I have implemented <strong>RANK()</strong> function,</p><p>Syntax: -<strong> SELECT *, RANK() OVER (ORDER BY MARKS DESC) AS RNK FROM student_marks_tb;</strong></p><p>Let&#39;s see result in the Figure 2:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/743/1*6iThgeLEEEpH9VKqK3gm0g.png" /><figcaption><strong>Figure 2: Result Table using RANK()</strong></figcaption></figure><p>&gt;In this above fig 2, we are getting Ranks based on Marks.</p><p>&gt;Here we are getting SAME RANK for RAJEV and GOPAL with 65 marks, like 6th RANK.</p><p>&gt;And also, next rank 7 skipped and gives 8th RANK.</p><p>— For not getting Skipped/Gaps in ranks, we go for Next Window Function <strong>DENSE_RANK()</strong></p><p>Syntax: - <strong>SELECT *, DENSE_RANK() OVER(ORDER BY MARKS DESC) AS RNK FROM student_marks_tb.</strong></p><p>Let’s see in the Figure 3:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/801/1*0HG6QvPVTg-7YSnN9O7_DA.png" /><figcaption><strong>Figure 3: Result Table using DENSE_RANK()</strong></figcaption></figure><p>&gt;In this table we are getting Ranks based on Marks with no Skipping/Gaps in Ranks when there are <strong>ties.</strong></p><p>&gt;Like RANK() Function, we are not getting Skipped/Gap Rank found to next Rank. <strong>(Ranks will be Consecutive)</strong></p><p>For this one (Dense_Rank) also I’m having one Problem, it gives Duplicate Ranks, If I want to give Every Unique Person as a UNIQUE RANK, then it will not work.</p><p>— So, we are going to another Window Function is <strong>ROW_NUMBER().</strong></p><p>Syntax of ROW_NUMBER()</p><p>SELECT *, ROW_NUMBER() OVER(ORDER BY MARKS DESC) AS RNK FROM student_marks_tb;</p><p>let’s see Figure 4:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/785/1*yqNqmref5txr1jUNUBwEcQ.png" /><figcaption><strong>Figure 4: Result Table Using ROW_NUMBER()</strong></figcaption></figure><p>As per above result we got 100% we got Unique Ranks as per Marks (Incase Marks are same in that condition it checks another possibility as per Registered ID, who registered First, for them they have given first preference (Example: RAJEV and GOPAL got 6th and 7th rank respectively as per they registered).</p><p>— Another Window Function is<strong> LAG()</strong>, that allows every company check their year on year growth sales. Let&#39;s take one Example Figure 5:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/459/1*_haV9oV0w2GllHbhhZ_daA.png" /><figcaption><strong>Figure 5: Table of Year-on-Year Sales</strong></figcaption></figure><p>In this Table, we are having year and sales columns, in we can calculate the YOY sales by using LAG() function (in excel we can calculate YOY sales by using cell references as {[current year sales-previous year sales]/previous year sales }100 by this we are getting % of Growth Sales), Unfortunately in SQL, we are not having row level calculations, for this we are going add one more sales column as T_SALES.</p><p>Let&#39;s in this Figure 6,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/648/1*-xT3SnCw6oQ74Jh0y9ldHw.png" /><figcaption><strong>Figure 6: Additional Column added as T_SALES</strong></figcaption></figure><p>Now we can calculate year-on-year sales, by using LAG() window function.</p><p>Lag() means ONE STEP BELOW, with help of this function we are going to delay sales one step to newly added T_SALES column</p><p>Syntax: SELECT S_YEAR, T_SALES, LAG(T_SALES) OVER(ORDER BY S_YEAR) as PY_Sales FROM windows_function_lag_tb;</p><p>Let&#39;s see in Figure 7</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/798/1*pE6RO0OHAByoMTcK3b8weA.png" /><figcaption><strong>Figure 7: PY_Sales get by using LAG() Window function</strong></figcaption></figure><p>with help above Syntax, PY_Sales just now calculated so that we can write it as Subquery concept.</p><p>Syntax: SELECT *, ((T_SALES-PY_SALES)/PY_SALES)*100 as YOY_Growth FROM (SELECT S_YEAR, T_SALES, LAG(T_SALES) OVER(ORDER BY S_YEAR) as PY_SALES FROM windows_function_lag_tb) A;</p><p>We are getting YOY_Growth Sales result Table as below Figure 8,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/828/1*iA4IAXRqLz7OTA2uo2rW0w.png" /><figcaption><strong>Figure 8: Result Table show as YOY_Growth Sales.</strong></figcaption></figure><p>With help of this Lag() function, we can calculate YOY_Growth Sales as compared with previous year sales.</p><p>— <strong>Let&#39;s go for another Window Function LEAD(), it gives ONE STEP UP.</strong></p><p>For this Function, we take same table S_YEAR and T_SALES,</p><p>syntax: SELECT S_YEAR, T_SALES, LEAD(T_SALES) OVER(ORDER BY S_YEAR) AS Future_Sales FROM windows_function_lead_tb;</p><p>As shown in Figure 9,</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/763/1*9hCCUzSX99A0Z-ujoIbmHQ.png" /><figcaption><strong>Figure 9: Result Table of LEAD() Window Function</strong></figcaption></figure><p>This Lead Function will be used for Compares present Sales with future sales like Stocks forecasting.</p><p>As per real time scenarios LAG() Function more frequently used as compared with LEAD() Function. Why because it compares with current data with past data.</p><p>Conclusion —</p><p>A simple attempt made to Understanding about SQL Window Functions with examples.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2d7323a52f4d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>