PARTITION BY Clause

SQL · Window Functions

Theory

PARTITION BY divides the rows into groups, and the window function is calculated separately inside each group. It works like GROUP BY in deciding which rows belong together, but it does not merge them: every row stays, and each row simply sees only the rows of its own partition.

For example, MAX(salary) OVER (PARTITION BY dept) returns 75000 on every IT row, 45000 on the HR row and 52000 on every Sales row. You can then compare each person to their department, as in dept_max - salary to show how far below the top they are. You can partition by several columns, such as PARTITION BY region, year. When ORDER BY is also given, the numbering or running total restarts at the start of each partition. If you leave PARTITION BY out, all rows form one single partition.

Why we use it

Real data is grouped: departments, customers, regions, months. PARTITION BY lets you compute rankings, totals and comparisons per group in one pass while keeping the detail rows, instead of running a separate query for each group.

When to use it

Use PARTITION BY whenever the calculation should restart or be separate per group (rank within department, running total per customer). Leave it out when the whole result is one group. Use GROUP BY instead if you only need one row per group.

How it works

  1. Rows arrive after WHERE / GROUP BY
  2. Rows are split by the PARTITION BY columns
  3. Each partition is processed on its own
  4. ORDER BY inside OVER? Yes: Walk rows in order per partition. No: Row sees whole partition.
  5. Compute the function for each row in its partition
  6. All rows returned with the per-partition result

Syntax

function() OVER (PARTITION BY column)

Example

SELECT name, department, salary,
  MAX(salary) OVER (PARTITION BY department) AS dept_max
FROM employees;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic