MAX() OVER

SQL · Window Functions

Theory

MAX(column) OVER (...) returns the largest value in the window on every row. With PARTITION BY dept it shows each department's top salary next to each employee; with ORDER BY it becomes a running maximum (the highest value so far, such as a record high price).

A very common pattern is finding the top earner in each group. You might try WHERE salary = MAX(salary) OVER (PARTITION BY dept), but that fails: window functions are computed after WHERE, so they are not allowed there. Instead, compute the window in a subquery or CTE and filter outside: SELECT * FROM (SELECT ..., MAX(salary) OVER (PARTITION BY dept) AS dept_max FROM employees) t WHERE salary = dept_max. Unlike ROW_NUMBER = 1, this keeps all tied top earners. Snowflake, BigQuery and a few others also support QUALIFY to filter on a window result directly.

Why we use it

Showing the group's peak next to each row answers 'how far from the top am I' and makes 'top row(s) per group' queries easy. MAX() OVER does it in one scan instead of joining to a GROUP BY subquery.

When to use it

Use MAX() OVER to compare with the group maximum, to keep all tied top rows, or for a running maximum. Use ROW_NUMBER when you need exactly one row per group even if values tie.

How it works

  1. Inner query: compute MAX(salary) OVER (PARTITION BY dept)
  2. Every row now carries its department maximum
  3. Window used in WHERE? Yes: Error: move it to a subquery. No: Continue.
  4. Outer query keeps rows WHERE salary = dept_max
  5. Top earner(s) per department, ties included

Syntax

MAX(column) OVER (PARTITION BY col)

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