MAX Function

SQL · Aggregate Functions

Theory

MAX returns the largest value in a column across a set of rows. It works on any type that can be sorted: for numbers it is the biggest number, for dates it is the latest date, and for text it is the value that sorts last alphabetically (so MAX(name) of Asha, Ravi and Meera is Ravi). NULLs are ignored, and MAX returns NULL only when there are no non-NULL values.

With GROUP BY you get the maximum per group, for example each customer's largest order and latest order date. One common mistake: MAX gives you the value, not the row. SELECT customer_id, MAX(amount), order_id is not valid in most databases, because order_id is not tied to the max. To get the whole row behind the maximum, use a subquery, a join back, or ROW_NUMBER().

Why we use it

'What is the highest?' and 'when was the latest?' are everyday questions: top salary, biggest order, most recent login. MAX answers them in one pass without sorting the whole table yourself.

When to use it

Use MAX for the largest number or the latest date or timestamp, per group with GROUP BY. If you need the full row that holds the max (who earns it, which order), use ROW_NUMBER() or a join to the max. For text, remember it is alphabetical, not by length.

How it works

  1. Rows arrive after FROM and WHERE
  2. GROUP BY splits rows into groups
  3. Start with no current maximum
  4. Is the next value NULL? Yes: Skip it. No: Keep it if it is larger than the current maximum.
  5. Repeat for every row in the group
  6. Return the maximum (NULL if no values)

Syntax

MAX(column)

Example

SELECT MAX(salary) 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