SQL Advanced (Analytics)
Back to WINDOW FUNCTIONS (ANALYTICS FOCUS)

Window Functions - Difference between ROW_NUMBER(), RANK(), DENSE_RANK()

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

Key moments

  1. Foundations: ORDER BY — The instructor demonstrates sorting the baby_names table by total popularity in descending order.
  2. Window Functions and OVER — The instructor introduces the OVER clause alongside ROW_NUMBER to calculate sequential ranks without row aggregation.
  3. Comparing Ranking Functions — The instructor compares ROW_NUMBER, RANK, and DENSE_RANK using a tied count example between Isabella and Olivia.
  4. Data Segmentation with PARTITION BY — The instructor demonstrates dividing the dataset into independent subgroups by partitioning calculations across gender categories.
  5. Window Functions in Subqueries — The instructor wraps window calculations inside a subquery to filter and isolate the top three names per gender.
PDF notes

Frequently asked questions

Why can't I filter a window function directly in a WHERE clause?

SQL evaluates WHERE clauses before window functions in the logical query lifecycle. To filter by calculated rank, wrap the query in a subquery and filter in the outer statement.

How does ROW_NUMBER decide which tied row gets the lower number?

Unless secondary sort columns are specified in the ORDER BY clause, ROW_NUMBER resolves ties non-deterministically based on the engine's physical row retrieval order.

Does PARTITION BY reduce or collapse rows like GROUP BY?

No. PARTITION BY defines independent computational subsets for the window calculation while preserving every original row in the output.

How was this lesson?

Your feedback helps us refine explanations and catch bugs.