Back to M0 — SQL + Python Screens

SQL Screen Drills (Timed Sets)

Outcome: Clear timed set (gaps-islands/recursive-CTE/EXPLAIN) Curated video (CodeEra): Top 5 SQL Window Function Questions for Interviews (With Solutions) — https://www.youtube.com/watch?v=LoS_U22CW_E (verified live via yt-dlp 2026-09-24). Pointer: DA-track-mapped timed sets; shell: courses/video-scripts/ds-interview-prep/01.md.

9 minutesVideo LessonPDF notes
🎯 Free Guest Mode: You are learning for free. Sign in to save your completion progress and quiz answers.

Ready to continue?

Mark this lesson as complete when you're ready to proceed.

Key moments

  1. Ranking Function Rules — The three ranking functions are defined based on uniqueness and gap behavior.
  2. Top Three Positions (RANK) — Finding top three positions requires RANK because ties are allowed and subsequent ranks must be skipped.
  3. Nth Highest Salary (DENSE_RANK) — Finding the Nth highest salary requires DENSE_RANK because rank numbers cannot be skipped.
  4. Highest Salary per Dept (PARTITION BY) — Ranking within groups is achieved using PARTITION BY, and filtering for rank 1 works with both RANK and DENSE_RANK.
  5. Latest Record per Group (ROW_NUMBER) — Finding exactly one record per group, like the latest order, is best solved using ROW_NUMBER.
  6. Top N Values (DENSE_RANK) — Finding the top N values where ties are allowed and gaps are forbidden requires DENSE_RANK.
  7. Decision Logic Summary — Always ask if duplicates are possible and if ranks should be skipped to choose the correct function.
PDF notes

Frequently asked questions

Why can't I filter the window function result directly using WHERE?

Window functions are calculated after the WHERE clause executes. You must use an outer query or CTE to filter the results based on the calculated rank.

When finding the highest salary in each department, why do RANK and DENSE_RANK give the same result?

When filtering specifically for rank 1, duplicates are handled identically. The question of skipping subsequent ranks becomes irrelevant.

What does the keyword "position" imply in an interview question?

"Position" suggests that if two people tie, the next position should be skipped. This indicates the use of the RANK function.

When should I use ROW_NUMBER?

Use ROW_NUMBER when you need exactly one unique record per group, such as finding the latest order for each customer.