Master SQL Server From Scratch

Clear, interactive, and structured SQL Server lessons designed for beginners and developers.

COURSE SYLLABUS
1. Introduction to SQL Server
Introduction to SQL Server SQL Server Installation SQL Server Fundamentals
2. Data Types
Data Types Numeric Data Types Character & String Data Types Date & Time Data Types Other SQL Server Data Types
3. Database & Tables
Database & Tables Introduction Creating a Database Creating Tables Table Structure Altering Tables Dropping Tables
4. Constraints
Constraints Introduction PRIMARY KEY FOREIGN KEY UNIQUE Constraint NOT NULL & DEFAULT CHECK Constraint
5. SQL Basics
SQL Basics Introduction SQL Commands SQL Command Categories SELECT Statement INSERT, UPDATE & DELETE
6. CRUD Operations
CRUD Operations Introduction INSERT Operation SELECT Operation UPDATE Operation DELETE Operation
7. SQL Queries
Queries Introduction WHERE Clause ORDER BY Clause DISTINCT LIKE Operator IN, BETWEEN & IS NULL
8. Joins
Joins Introduction INNER JOIN LEFT JOIN RIGHT JOIN FULL OUTER JOIN CROSS JOIN
9. SQL Functions
SQL Functions Introduction String Functions Numeric Functions Date & Time Functions Conversion Functions NULL Functions
10. Grouping & Aggregate Functions
GROUP BY Aggregate Functions COUNT() SUM() AVG() MIN() & MAX()
11. Subqueries
Subqueries Introduction Single-Row Subquery Multiple-Row Subquery Correlated Subquery Subquery with DML
12. Views
SQL Views Introduction Creating a View Updating a View Altering a View Dropping a View
13. Stored Procedures
Stored Procedures Introduction Creating a Stored Procedure Input Parameters Output Parameters Executing & Modifying Stored Procedures
14. Triggers
Triggers Introduction AFTER INSERT Trigger AFTER UPDATE Trigger AFTER DELETE Trigger INSTEAD OF Trigger
15. Transactions
Transactions Introduction COMMIT ROLLBACK SAVEPOINT Transaction with TRY...CATCH
16. Indexes
Indexes Introduction Clustered Index Non-Clustered Index Creating & Dropping Indexes Index Performance
17. Common Table Expressions
CTE Introduction Simple CTE CTE with JOIN CTE with Aggregate Functions Recursive CTE
18. Window Functions
Window Functions Introduction ROW_NUMBER() RANK() and DENSE_RANK() LEAD() and LAG() PARTITION BY
19. Advanced SQL
Advanced SQL Introduction Temporary Tables Table Variables Dynamic SQL Error Handling Performance Optimization

Window Functions Introduction

Learn how SQL Server Window Functions work and how they are used to perform calculations across related rows without grouping the result into a single row.

What are Window Functions?

Window Functions in SQL Server perform calculations across a set of rows that are related to the current row.

Unlike aggregate functions such as SUM(), AVG() and COUNT(), Window Functions do not combine multiple rows into a single result row.

Instead, they return a calculated value for each row while still keeping the individual rows in the result.

Why Use Window Functions?

  • To calculate rankings for records.
  • To assign row numbers to records.
  • To compare the current row with previous or next rows.
  • To calculate running totals and averages.
  • To divide data into groups or partitions.
  • To perform analytical calculations without losing individual rows.

Common Window Functions

Function Purpose
ROW_NUMBER() Assigns a unique sequential number to each row.
RANK() Assigns a rank to rows and leaves gaps when duplicate values occur.
DENSE_RANK() Assigns a rank without leaving gaps between duplicate ranking values.
LEAD() Accesses a value from a following row.
LAG() Accesses a value from a previous row.

Basic Window Function Syntax

SELECT
    column_name,
    WindowFunction() OVER
    (
        PARTITION BY column_name
        ORDER BY column_name
    ) AS Result
FROM TableName;

Example: ROW_NUMBER()

Suppose we have an Employees table. We can use ROW_NUMBER() to assign a sequential number to each employee.

SELECT
    EmployeeId,
    EmployeeName,
    Salary,

    ROW_NUMBER() OVER
    (
        ORDER BY Salary DESC
    ) AS RowNumber

FROM Employees;

The result contains every employee along with a sequential row number based on salary in descending order.

Important Points

  • Window Functions use the OVER() clause.
  • ORDER BY inside OVER() determines the order used for the calculation.
  • PARTITION BY divides rows into separate groups for the calculation.
  • Window Functions normally keep individual rows in the result.

Summary

Window Functions are powerful SQL Server features used for analytical calculations across related rows while preserving individual records in the result.

Common Window Functions include ROW_NUMBER(), RANK(), DENSE_RANK(), LEAD() and LAG(). These functions are commonly combined with OVER(), ORDER BY and PARTITION BY.