T
Tech Town
Log InSign Up Free
← Back to Blog

June 17, 2025 · admin

MAX Function

πŸ” SQL MAX() Function – How to Get the Highest Value in a Column

The SQL MAX() function is a powerful aggregate function that returns the maximum (largest) value in a column. It’s commonly used in data analysis for tasks like finding the highest salary, most recent transaction, or the largest order.

This tutorial will teach you:

  • βœ… What the SQL MAX() function does
  • πŸ’‘ Syntax and behavior
  • πŸ“Š Practical SQL examples
  • ⚠️ Tips and common mistakes

βœ… What is SQL MAX()?

The MAX() function returns the highest value in a column across a set of rows. It works with numeric, date, and text-based (alphabetical) values.


πŸ”§ Syntax

SELECT MAX(column_name)
FROM table_name
WHERE condition;

You can also use MAX() in combination with:

  • GROUP BY to find the max in each category
  • JOINs to get related records
  • HAVING to filter group-level maximums

πŸ“Š SQL MAX() Function Examples

πŸ§ͺ Example 1: Find the Highest Salary

SELECT MAX(salary) AS highest_salary
FROM employees;

βœ”οΈ This returns the largest value in the salary column.


πŸ§ͺ Example 2: Find the Latest Order Date

SELECT MAX(order_date) AS latest_order
FROM orders;

πŸ“… Useful in tracking recent transactions or events.


πŸ§ͺ Example 3: Get Highest Score per Student

SELECT student_id, MAX(score) AS top_score
FROM test_results
GROUP BY student_id;

πŸŽ“ Perfect for reporting top performance by category.


πŸ§ͺ Example 4: MAX() on Text Values (Alphabetically)

SELECT MAX(last_name) AS max_name
FROM employees;

πŸ…°οΈ Returns the last name that is alphabetically last.


⚠️ Common SQL MAX() Mistakes to Avoid

MistakeSolution
Using MAX() without GROUP BY on multiple columnsUse GROUP BY with all non-aggregated columns
Expecting MAX() to return multiple rowsMAX() returns only one value per group
Ignoring NULLsMAX() automatically ignores NULLs

🎯 Real-World Use Cases for SQL MAX()

  • 🏒 HR: Get the highest salary in a department
  • πŸ›’ E-Commerce: Get the biggest order per customer
  • πŸ“… Events: Find the most recent login or activity
  • πŸ“ˆ Analytics: Get the peak performance metrics

βœ… SQL MAX() with GROUP BY

SELECT department_id, MAX(salary) AS highest_salary
FROM employees
GROUP BY department_id;

This query returns the highest salary in each department.


🧠 Pro Tip: Use MAX() with Subqueries

You can combine MAX() with a subquery to filter for rows matching the max value.

SELECT *
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);

🎯 Returns all employees with the highest salary.


🏁 Summary

FeatureDetails
FunctionMAX()
PurposeGet the highest value in a column
Ignores NULLsβœ… Yes
Can be grouped?βœ… Yes, with GROUP BY
Works withNumbers, dates, text