MAX()The MAX() aggregate function in MySQL returns the largest value of a given column. It's commonly used with numeric types, dates, or any comparable values. Use it alone to get a single maximum across a set, or with GROUP BY to get maxima per group.
MAX() ignores NULL values.GROUP BY to compute maxima per group.MAX() only returns the value, not the row.Basic syntax:
SELECT MAX(column_name) FROM table_name;SELECT group_col, MAX(value_col) FROM table_name GROUP BY group_col;
// Get the highest salary from the employees table
SELECT MAX(salary) AS max_salary
FROM employees;
// Get the maximum sale amount per region
SELECT region, MAX(sale_amount) AS max_sale
FROM sales
GROUP BY region;
// Return the employee(s) who have the maximum salary
SELECT e.*
FROM employees e
JOIN (
SELECT MAX(salary) AS max_salary
FROM employees
) m ON e.salary = m.max_salary;
// MAX(DISTINCT ...) is allowed but usually redundant;
example shown for completeness
SELECT MAX(DISTINCT score) AS top_unique_score
FROM quiz_results;
Query: SELECT MAX(salary) AS max_salary FROM employees;
Explanation: Returns the single highest numeric value found in the salary column. NULL values are ignored. If the table is empty, result is NULL.
Query: SELECT region, MAX(sale_amount) AS max_sale FROM sales GROUP BY region;
Explanation: For each distinct region, MySQL computes the largest sale_amount. Useful for leaderboards, summaries, and reporting.
Pattern: Use a subquery to compute the max value, then join back to the main table to retrieve full rows that match that value. This returns all rows that tie for the maximum.
MAX() ignores NULL — ensure NULLs are intended to be excluded.products(id, name, price, category) and insert sample data; write a query to find the most expensive product overall.category.scores(student_id, test_id, score) table, write a query to list students who achieved the maximum score on any test.NULL? Verify the returned results.