SQL Server Mastery
Lesson 6 of 30 20% of course

Window Functions: ROW_NUMBER, RANK, and LEAD/LAG

18 · 8 min · 5/23/2026

Sign in to track progress and bookmarks.

Window Functions

Window Functions are the "Swiss Army Knife" of T-SQL. They allow you to perform calculations across a set of table rows that are related to the current row, without collapsing the rows into a single result (unlike GROUP BY).

1. ROW_NUMBER vs RANK vs DENSE_RANK

  • ROW_NUMBER(): Unique sequential number for each row. (Perfect for Pagination).
  • RANK(): Skips numbers if there is a tie. (1, 2, 2, 4).
  • DENSE_RANK(): Doesn't skip numbers if there is a tie. (1, 2, 2, 3).
SELECT Name, Salary, 
       RANK() OVER(ORDER BY Salary DESC) as Rank
FROM Employees

2. Analytic Functions (LEAD/LAG)

How do you compare 'Today's Sales' with 'Yesterday's Sales' in one row? You use LAG() to peek at the previous row. This eliminates the need for expensive self-joins.

4. Interview Mastery

Q: "What is the purpose of the 'PARTITION BY' clause in a window function?"

Architect Answer: "It is like a 'Local Group By'. Instead of ranking everyone in the company, `PARTITION BY DepartmentID` will reset the ranking for every department. Department A will have a Rank 1, and Department B will also have a Rank 1. It allows you to group logic while still seeing every individual row."

Test your knowledge

Quizzes linked to this course—pass to earn certificates.

Browse all quizzes
SQL Server Mastery

On this page

1. ROW_NUMBER vs RANK vs DENSE_RANK 2. Analytic Functions (LEAD/LAG) 4. Interview Mastery
1. SQL Server Architecture & Basics
SQL Server Internals: How the Storage Engine works Relational Database Design & Normalization (1NF to 3NF) Data Types Mastery: Choosing the right type for performance
2. Advanced T-SQL Querying
Joins Deep Dive: Inner, Outer, Cross, and Self Joins Subqueries vs CTEs: Writing readable, high-performance code Window Functions: ROW_NUMBER, RANK, and LEAD/LAG Aggregations & Grouping Sets: Building complex reports Set Operators: UNION vs UNION ALL, INTERSECT, and EXCEPT
3. Indexing & Performance Tuning
Clustered vs Non-Clustered Indexes: The physical storage reality Covering Indexes & Included Columns: Reducing I/O costs Index Fragmentation: Why it happens and how to fix it Execution Plans: Reading the Query Optimizer's mind Statistics: Why 'Out of Date' stats kill performance SARGability: Writing queries that actually use indexes
4. Database Programmability
Stored Procedures: Security, Performance, and Best Practices User Defined Functions (UDF): Scalar vs Table-Valued Triggers: Auditing changes and the dangers of hidden logic Views & Indexed Views: Abstraction with performance Error Handling: TRY/CATCH and XACT_STATE()
5. Transactions & Concurrency
Transaction Isolation Levels: Read Uncommitted to Snapshot Locking & Blocking: Analyzing Deadlocks like a Pro Optimistic vs Pessimistic Concurrency
6. Administration & Security
SQL Server Security: Logins, Users, and Roles SQL Injection Prevention: Beyond simple parameterization Backup & Recovery Models: Full vs Simple vs Bulk-Logged Automating Maintenance: SQL Agent Jobs & Rebuilding Indexes
7. Modern SQL & Cloud
SQL Server & JSON: Storing and Querying semi-structured data Temporal Tables: Keeping track of data history automatically Introduction to Azure SQL: Database as a Service (PaaS) SQL Server Developer Interview: Junior to Senior Architect Level