How would you calculate period-over-period comparisons such as Month-over-Month or Year-over-Year growth in SQL, leveraging window functions?
Last updated
Aggregate to one row per period first, then fetch the prior value with LAG(total) OVER (ORDER BY month) for MoM, or
LAG(total, 12)for YoY on monthly data.Growth is
(cur - prev) / NULLIF(prev, 0)to avoid division by zero.LAGreads the previous present row, not the previous calendar period. Join to a date spine so missing months don't shift the offset.
- https://datalemur.com/blog/sql-window-functions-interview-questions
- https://builder.ai2sql.io/blog/sql-window-functions-interview
- https://www.thoughtspot.com/sql-tutorial/sql-window-functions
- https://pipeline2insights.substack.com/p/week-332-advanced-sql-concepts-for
- https://beeclassifieds.beenews.com/story/061/1TW/bE97eV/sql__advanced-interview_questions
- https://medium.com/@aggarwalakshima/interview-question-asked-by-google-and-difference-among-row-number-rank-and-dense-rank-4ca08f888486
- https://www.practicewindowfunctions.com/learn/ranking_functions
- https://www.youtube.com/watch?v=nk8MjpVevsc
- https://sql-academy.org/en/blog/row-number-rank-dense-rank
- https://www.youtube.com/watch?v=GNggtNSz6bw
- https://medium.com/@gankur277/sql-window-functions-explained-with-real-interview-questions-9ab2d44399e6
- https://interviewstack.io/machine_learning_engineer/categories/question-bank/advanced-sql-window-functions-ctes-for-complex-analysis
- https://dev.to/de_clerke/sql-interview-problems-for-data-engineers-30-patterns-that-actually-get-asked-4npk
- https://www.datacamp.com/blog/top-sql-interview-questions-and-answers-for-beginners-and-intermediate-practitioners
- https://www.testgorilla.com/blog/joins-in-sql-interview-questions/
- https://code.likeagirl.io/cte-vs-subquery-vs-recursive-cte-the-interview-question-every-data-scientist-should-know-ce6163487841
- https://www.youtube.com/watch?v=x8wFduXs2kk
- https://medium.com/towards-data-engineering/mastering-sql-recursive-ctes-key-concepts-and-interview-questions-04f69c408e81
- https://learnsql.com/blog/sql-cte-interview-questions/
- https://upesonline.ac.in/blog/advanced-sql-interview-questions
- https://blog.devgenius.io/5-advanced-sql-interview-questions-0b6bcd2f7691
- https://www.red-gate.com/simple-talk/databases/sql-server/t-sql-programming-sql-server/questions-about-cube-rollup-and-grouping-sets-that-you-were-too-shy-to-ask/
- https://towardsdatascience.com/sql-explained-grouping-sets-rollup-and-cube-a097456fb9a2/
- https://www.youtube.com/watch?v=Ccye_H34hBo&vl=en
- https://blog.dailydoseofds.com/p/grouping-sets-rollup-and-cube-in
- https://www.rittmanmead.com/blog/2008/09/cube-rollup-grouping-sets-and-grouping-ids/
- https://blog.stackademic.com/concept-of-pivoting-unpivoting-967ee12a5969
- https://medium.com/geekculture/crack-sql-interview-question-self-join-and-non-equi-join-1cb2a181104a
- https://www.youtube.com/watch?v=k-WQmJWxzTY
- https://learnsql.com/blog/sql-non-equi-joins-examples/
- https://www.datacamp.com/blog/top-sql-joins-interview-questions
- https://thedatamonk.com/advanced-sql-interview-questions/
- https://medium.com/@shankergoud6666/top-50-sql-interview-questions-2026-clear-detailed-answers-to-crack-any-job-275d5d6d3fc9
- https://www.foundit.sg/career-advice/20-common-sql-interview-questions-answers/
- https://www.java-success.com/07-50-sql-scenarios-based-interview-qas-on-pivot-vs-unpivot-case-statements-null-safe-join/
- https://www.datainterview.com/blog/top-100-sql-interview-questions
- https://medium.com/@adichaurasia2611/pivot-and-unpivot-in-sql-explained-simply-91bbf6f49514
- https://www.geeksforgeeks.org/sql/pivot-and-unpivot-in-sql/
- https://www.scaler.com/topics/pivot-in-sql/
- https://grokipedia.com/page/Advanced_SQL_Server_Interview_Questions
- https://www.youtube.com/watch?v=18_cInZr0Gc
- https://medium.com/towards-data-engineering/sql-interview-patterns-003-mastering-gap-islands-with-the-longest-activity-streak-problem-8adeaf7adfb5
- https://medium.com/@mvanshika23/sql-interview-patterns-you-must-know-part-1-n-gaps-and-islands-e9db5c97140f
- https://www.practicewindowfunctions.com/learn/gap_and_island
- https://medium.com/@krthiak/30-sql-window-functionion-questions-for-interviews-day-47-of-100-days-of-data-engineering-ai-and-638bef1af0f6
- https://www.youtube.com/watch?v=5cMLWs1UP5I
- https://www.youtube.com/watch?v=Lb3vfuew7Gk
- https://www.youtube.com/watch?v=Hv54WLTHN70
- https://cwiki.apache.org/confluence/display/Hive/Enhanced+Aggregation%2C+Cube%2C+Grouping+and+Rollup
- https://learnsql.com/blog/group-by-sql-interview-questions/
- https://www.youtube.com/watch?v=HTemHu1o4Qs
- https://www.reddit.com/r/SQL/comments/u30iqg/sql_interview_tips_creative_use_of_sql_time/
- https://medium.com/coding-odyssey/sql-joins-deep-dive-with-interview-questions-b0d64a5670a8
- https://www.crunchydata.com/developers/playground/lateral-join
- https://medium.com/@goldengrisha/a-deep-dive-into-sql-lateral-join-7b09fcb3b745
- https://www.youtube.com/watch?v=NiiN63lEzvI
- https://www.youtube.com/watch?v=EgW3fHqeBNM