🔝 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