Timedora
Free Online Flashcard Deck

SQL Window Functions, Cohorts, and Ranking Free Online Flashcards

Study SQL Window Functions, Cohorts, and Ranking with 12 free online flashcards. Review key terms, definitions, and concepts with this interactive flashcard deck.

12 cards
01
Front

In a window function, what do PARTITION BY and ORDER BY control?

Back

PARTITION BY starts a separate window calculation for each group; ORDER BY defines sequence or ranking within each group.

02
Front

Why may ROWS BETWEEN 6 PRECEDING AND CURRENT ROW not represent seven calendar days?

Back

The frame contains seven rows, not necessarily seven calendar days. Add a complete daily date series and fill missing values with zero when missing dates must count.

03
Front

How is a monthly user cohort defined, and what is month 0?

Back

A cohort groups users by the month of their first recorded event. Month 0 is that cohort month.

04
Front

How is a CTE introduced, and where can it be referenced?

Back

A CTE is introduced by WITH, has a name and a parenthesized query, and is available to later CTEs or the final query in that statement.

05
Front

How do you choose between the top three RANK() positions and exactly three rows?

Back

RANK() <= 3 selects the top three rank positions, so ties can return more than three rows. Use ROW_NUMBER() with a deterministic tie-breaker for exactly three rows per group.

06
Front

How does a window function differ from ordinary aggregation in its output?

Back

Unlike ordinary aggregation, a window function returns a value for each input row.

07
Front

Why include a six-day lookback before the first reported day in a seven-day rolling metric?

Back

Include six days before the first reported date so its seven-day window contains the current day and six preceding days.

08
Front

What does a query stage's grain mean, and why check it?

Back

The grain is what one row represents. Checking each stage's grain before joining or applying a window function helps prevent duplicate counting.

09
Front

How is monthly cohort retention calculated?

Back

Divide the number of cohort users with at least one event in that month by the cohort size. Deduplicating activity to one row per user and month avoids counting a user more than once.

10
Front

What should happen before ranking aggregated results within groups?

Back

Aggregate to the intended grain first, then rank within the required group, ordering by the measure. For monthly product sales, aggregate to one row per month and product before ranking.

11
Front

How can a row-based frame calculate a calendar-day rolling total when dates are missing?

Back

Create a complete daily date spine, join the daily values to it, replace missing values with zero, and then apply the row-based window frame.

12
Front

How do RANK() and DENSE_RANK() differ when values tie?

Back

RANK() assigns tied values the same rank and leaves gaps after ties. DENSE_RANK() also ties equal values but leaves no gaps.