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

PARTITION BY

Learn how to use PARTITION BY with SQL Server Window Functions to perform calculations separately for different groups.

What is PARTITION BY?

PARTITION BY is used with SQL Server Window Functions to divide the result set into separate groups.

The Window Function then performs its calculation separately within each group.

Unlike GROUP BY, PARTITION BY does not combine rows into a single row. All original rows remain in the result.

Syntax

WindowFunction()
OVER
(
    PARTITION BY column_name
    ORDER BY column_name
)

PARTITION BY defines the groups, while ORDER BY determines the order within each group.

Sample Employee Data

EmployeeId EmployeeName Department Salary
101 Amit IT 70000
102 Rahul IT 80000
103 Priya HR 60000
104 Neha HR 65000

ROW_NUMBER() with PARTITION BY

We can use PARTITION BY with ROW_NUMBER() to assign row numbers separately within each department.

SELECT
    EmployeeName,
    Department,
    Salary,

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

FROM Employees;

ROW_NUMBER() Output

EmployeeName Department Salary RowNumber
Rahul IT 80000 1
Amit IT 70000 2
Neha HR 65000 1
Priya HR 60000 2

Notice that numbering starts again from 1 for each department.

RANK() with PARTITION BY

PARTITION BY can also be used with RANK() to rank employees separately within each department.

SELECT
    EmployeeName,
    Department,
    Salary,

    RANK() OVER
    (
        PARTITION BY Department
        ORDER BY Salary DESC
    ) AS DepartmentRank

FROM Employees;

DENSE_RANK() with PARTITION BY

DENSE_RANK() can also calculate rankings separately for each group.

SELECT
    EmployeeName,
    Department,
    Salary,

    DENSE_RANK() OVER
    (
        PARTITION BY Department
        ORDER BY Salary DESC
    ) AS DepartmentRank

FROM Employees;

SUM() with PARTITION BY

PARTITION BY is not limited to ranking functions. It can also be used with aggregate functions such as SUM().

SELECT
    EmployeeName,
    Department,
    Salary,

    SUM(Salary) OVER
    (
        PARTITION BY Department
    ) AS DepartmentTotalSalary

FROM Employees;

This calculates the total salary for each department while keeping every employee row in the result.

AVG() with PARTITION BY

SELECT
    EmployeeName,
    Department,
    Salary,

    AVG(Salary) OVER
    (
        PARTITION BY Department
    ) AS DepartmentAverageSalary

FROM Employees;

This calculates the average salary separately for every department.

LEAD() and LAG() with PARTITION BY

PARTITION BY can also be combined with LEAD() and LAG() to compare rows within each group.

SELECT
    EmployeeName,
    Department,
    Salary,

    LAG(Salary) OVER
    (
        PARTITION BY Department
        ORDER BY Salary
    ) AS PreviousSalary

FROM Employees;

The previous salary is calculated separately within each department.

PARTITION BY vs GROUP BY

Feature PARTITION BY GROUP BY
Groups data Yes Yes
Original rows remain Yes No
Used with Window Functions Yes No
Returns one row per group No Usually yes

Practical Uses of PARTITION BY

  • Department-wise employee ranking.
  • Department-wise salary calculations.
  • Category-wise product ranking.
  • Customer-wise transaction analysis.
  • Group-wise running totals.
  • Comparing rows within individual groups.

Important Points

  • PARTITION BY divides the result into groups.
  • Window calculations are performed separately for each partition.
  • PARTITION BY does not remove or combine rows.
  • It can be used with ROW_NUMBER(), RANK(), DENSE_RANK(), LEAD(), LAG() and aggregate functions.
  • ORDER BY can be used inside OVER() to define the order within each partition.

Summary

PARTITION BY divides rows into logical groups and allows Window Functions to perform calculations separately for each group.

It is commonly used with ROW_NUMBER(), RANK(), DENSE_RANK(), LEAD(), LAG(), SUM(), AVG() and other Window Functions.