What is the frame clause in a window function? Explain the difference between ROWS and RANGE frames, when to use each, and the duplicate-peers trap with RANGE.

Last updated 13 July 2026

  • The frame clause defines which rows of the partition a window function operates on relative to the current row (e.g. ROWS BETWEEN 2 PRECEDING AND CURRENT ROW).

  • ROWS counts physical rows (use for moving averages). RANGE uses value ranges of the ORDER BY key, grouping tied peers (use for value offsets like 7 days).

  • Trap: with RANGE ... CURRENT ROW, all tied rows are included at once, so a running total shows the same value for every tie. Use ROWS for a strict row-by-row sum.

  • 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://medium.com/activated-thinker/top-20-sql-interview-questions-for-data-analyst-data-scientist-ml-engineers-688bd22bb139
  • https://thedataforge.medium.com/50-sql-interview-questions-every-data-engineer-must-know-advanced-edition-part-2-c6a5fe8dd18b
  • https://www.youtube.com/watch?v=Hv54WLTHN70
  • https://leetcode.com/discuss/study-guide/1601295/database-sql-primer-part-4-recursive-ctes/
  • https://www.geeksforgeeks.org/sql-server/recursive-cte-in-sql-server/
  • https://lolithasherley7.medium.com/sql-interview-questions-and-answers-0a51debd30e7
  • https://learnsql.com/blog/group-by-sql-interview-questions/
  • https://builder.ai2sql.io/blog/sql-aggregate-functions-interview
  • https://medium.com/@piyush.devspace/rollup-vs-cube-for-group-by-439dde93036e
  • https://stackoverflow.com/questions/28550679/what-is-the-difference-between-lateral-join-and-a-subquery-in-postgresql
  • https://www.baeldung.com/sql/lateral-join-vs-subquery
  • https://medium.com/@goldengrisha/a-deep-dive-into-sql-lateral-join-7b09fcb3b745
  • https://dba.stackexchange.com/questions/237219/when-to-use-lateral-joins-vs-correlated-subqueries-in-postgres
  • https://www.geeksforgeeks.org/sql/difference-between-nested-subquery-correlated-subquery-and-join-operation/
  • https://www.red-gate.com/simple-talk/databases/sql-server/t-sql-programming-sql-server/introduction-to-gaps-and-islands-analysis/
  • https://medium.com/analytics-vidhya/sql-classic-problem-identifying-gaps-and-islands-across-overlapping-date-ranges-5681b5fcdb8
  • https://www.interviewbit.com/sql-interview-questions/
  • https://github.com/Devinterview-io/sql-interview-questions
  • https://www.tredence.com/blog/30-sql-interview-questions-2026
  • https://www.youtube.com/watch?v=5cMLWs1UP5I
  • https://www.youtube.com/watch?v=Lb3vfuew7Gk
  • https://cwiki.apache.org/confluence/display/Hive/Enhanced+Aggregation%2C+Cube%2C+Grouping+and+Rollup
  • 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://www.youtube.com/watch?v=NiiN63lEzvI
  • https://www.youtube.com/watch?v=EgW3fHqeBNM
All Advanced SQL interview questions