TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2583 2.11K
29. Find Monthly Revenue Growth

WITH monthly_sales AS (

    SELECT

        DATE_TRUNC('month', o.order_date) AS month,

        SUM(oi.quantity * oi.unit_price) AS revenue

    FROM orders o

    JOIN order_items oi ON o.order_id = oi.order_id

    GROUP BY DATE_TRUNC('month', o.order_date)

)

SELECT

    month,

    revenue,

    LAG(revenue) OVER (ORDER BY month) AS previous_month_revenue,

    ROUND(100.0 * (revenue - LAG(revenue) OVER (ORDER BY month)) / LAG(revenue) OVER (ORDER BY month), 2) AS growth_percentage

FROM monthly_sales;

30. Find the Highest Value Order for Each Customer

WITH order_values AS (

    SELECT

        o.customer_id,

        o.order_id,

        SUM(oi.quantity * oi.unit_price) AS order_value

    FROM orders o

    JOIN order_items oi ON o.order_id = oi.order_id

    GROUP BY o.customer_id, o.order_id

)

SELECT *

FROM (

    SELECT *,

           ROW_NUMBER() OVER (

               PARTITION BY customer_id ORDER BY order_value DESC

           ) AS rn

    FROM order_values

) t

WHERE rn = 1;

Window Functions Covered 

• Ranking: ROW_NUMBER(), RANK(), DENSE_RANK() 

• Navigation: LAG(), LEAD() 

• Aggregates: SUM() OVER() 

• Analytics: Running Totals, Revenue Contribution, Month-over-Month Growth 

💡 Double Tap ❤️ For More
  • ❤ 4
More from @sqlanalyst
  1. Oct 9, 2026SQL Interview Series — Part 5 📌 Question 5: Find Employees Who Earn More Than Their Manag…
  2. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
  3. Oct 8, 2026🎓 𝗠𝗶𝗰𝗿𝗼𝘀𝗼𝗳𝘁 𝗙𝗥𝗘𝗘 𝗖𝗼𝘂𝗿𝘀𝗲𝘀 𝘄𝗶𝘁𝗵 𝗖𝗲𝗿𝘁𝗶𝗳𝗶𝗰𝗮𝘁𝗲𝘀! 🚀🔥 Upgr…
  4. Oct 7, 2026🚀𝗣𝗮𝘆 𝗔𝗳𝘁𝗲𝗿 𝗣𝗹𝗮𝗰𝗲𝗺𝗲𝗻𝘁 𝗧𝗿𝗮𝗶𝗻𝗶𝗻𝗴 | 𝗕𝗲𝗰𝗼𝗺𝗲 𝗮 𝗙𝘂𝗹𝗹𝘀𝘁𝗮𝗰…
  5. Oct 7, 2026SQL Interview Series — Part 4 📌 Question 4: Find the Highest Salary in Each Department Su…
  6. Oct 7, 2026𝗠𝗮𝘀𝘁𝗲𝗿 𝗣𝗼𝘄𝗲𝗿 𝗕𝗜 𝗳𝗼𝗿 𝗙𝗥𝗘𝗘! 🔥 Learn Power BI through these FREE learnin…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →