GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • SQL
  • Projects
  • Interview Questions
  • Quiz
  • DBMS
  • PostgreSQL
  • Django
  • ReactJS
  • Vue.js
  • AngularJS
  • API
  • REST API
  • WHERE
  • SQL3 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — SQL MAX() Function - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • SQL
  • Projects
  • Interview Questions
  • Quiz
  • DBMS
  • PostgreSQL
  • Django
  • ReactJS
  • Vue.js
  • AngularJS
  • API
  • REST API

SQL MAX() Function

Last Updated : 22 Nov, 2025

The SQL MAX() function helps quickly identify the highest value within any column, making it useful for summarizing and analyzing data trends. It is widely used in reports to find top values like peak sales, latest dates, or highest scores.

  • Returns the maximum value from the specified column.
  • Supports numeric, date, and text data types.
  • Skips NULL values during calculation.

Example: First, we create a demo SQL database and table, on which we use the MAX() functions.

Screenshot-2025-11-19-100650
SalesData Table

Query:

SELECT MAX(Price) AS HighestPrice
FROM SalesData;

Output:

Screenshot-2025-11-19-100814
  • Finds the highest price from the Price column in the SalesData table.
  • Returns a single value labeled as HighestPrice.

Syntax:

SELECT MAX(column_name)
FROM table_name;

Examples of SQL MAX() Function

Here, we demonstrate the usage of the MAX() function using a single sample table named Products. Consider this Products table for all the examples below:

Screenshot-2025-11-19-102951
Products Table

Example 1: Find the Maximum Price in a Table

In this example, we see how the MAX() function retrieves the highest total_sales value from the Products table while ignoring NULL values.

Query:

SELECT MAX(total_sales) AS [Highest Total Sales]
FROM Products;

Output:

Screenshot-2025-11-19-104012
  • Returns the largest value from the total_sales column.
  • Helps identify the highest sales amount in the entire table.

Example 2: Use MAX() with a Condition

In this example, we see how the MAX() function returns the highest price after filtering rows based on a specific condition.

Query:

SELECT MAX(price) AS [Highest Price in Electronics]
FROM Products
WHERE category = 'Electronics';

Output:

Screenshot-2025-11-19-104100
  • Applies WHERE to include only products in the Electronics category.
  • MAX() then returns the highest price within that filtered group.

Example 3: Find the Latest Sale Date

In this example, we use MAX() to get the most recent sale date from the table.

Query:

SELECT MAX(sale_date) AS [Latest Sale Date]
FROM Products;

Output:

Screenshot-2025-11-19-104253
  • MAX(sale_date) returns the latest date stored in the column.
  • Useful for identifying the most recent sale activity.

Example 4: Using MAX() with GROUP BY

In this example, we use MAX() with GROUP BY to find the highest sale amount for each product.

Query:

SELECT product_name, MAX(total_sales) AS [Top Sales Amount]
FROM Products
GROUP BY product_name;

Output:

Screenshot-2025-11-19-105011
  • Groups rows by product_name and applies MAX() to each group.
  • Returns the top sales amount for every individual product.

Example 5: Using MAX() in Subqueries

In this example, we use MAX() inside a subquery to get the product with the highest total_sales.

Query:

SELECT *
FROM Products
WHERE total_sales = (SELECT MAX(total_sales) FROM Products);

Output:

Screenshot-2025-11-19-104510
  • The subquery returns the maximum total_sales value from Products.
  • The main query fetches the full record that matches this highest value.

Example 6: Using MAX() with HAVING

In this example, we use MAX() with HAVING to filter product groups based on their highest total sales.

Query:

SELECT product_name, MAX(total_sales) AS HighestSale
FROM Products
GROUP BY product_name
HAVING MAX(total_sales) > 50000;

Output:

Screenshot-2025-11-19-105311
  • GROUP BY creates groups for each product_name and calculates their MAX(total_sales).
  • HAVING filters only those products whose highest sale exceeds 50,000.


Comment

Explore

    Basics
    • SQL3 min read
    • Data Types3 min read
    • Operators4 min read
    • Commands5 min read
    • Database Operations2 min read
    • CREATE TABLE2 min read
    Queries & Operations
    • SELECT Query2 min read
    • INSERT INTO3 min read
    • UPDATE2 min read
    • DELETE2 min read
    • WHERE2 min read
    • Aliases2 min read
    Joins & Functions
    • Joins3 min read
    • CROSS JOIN1 min read
    • Date Functions2 min read
    • String functions4 min read
    Data Constraints & Aggregate Functions
    • NOT NULL Constraint3 min read
    • PRIMARY KEY3 min read
    • Count()4 min read
    • SUM()2 min read
    • MAX()3 min read
    • AVG()2 min read
    Advanced SQL Topics
    • Subquery4 min read
    • Window Functions4 min read
    • Stored Procedures3 min read
    • Triggers4 min read
    • Performance Tuning4 min read
    • TRANSACTIONS4 min read
    Database Design & Security
    • ER Model9 min read
    • Normalization2 min read
    • Keys in Relational Model7 min read
    • SQL Injection10 min read
    • Data Encryption3 min read
    • SQL Backup4 min read
    • ORM in DBMS5 min read
    Courses
    • Placement 360 Course2 min read
    • Data Science 360 Course2 min read
    • MERN Full Stack Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.