How MAX works in SQL? Best MAX examples

How MAX works in SQL? Best MAX examples

When working with databases, one of the most common requirements is to find the highest value in a column, whether it be a salary, price, or timestamp. In SQL, the MAX() function helps us achieve exactly that. It’s a powerful aggregate function that works efficiently across different types of queries. Let’s explore how MAX() works, the best ways to use it, and some practical examples.

Understanding the MAX() Function

The MAX() function in SQL returns the maximum value from a given column. It’s mostly used with numerical values, dates, and even text columns (where it finds the highest value based on alphabetical order).

The basic syntax of the function looks like this:

SELECT MAX(column_name) FROM table_name;

Let’s dive into some practical applications.

Using MAX() to Find the Highest Value

Suppose we have an “employees” table with the following data:

id name department salary
1 Alice HR 50000
2 Bob IT 70000
3 Charlie IT 60000

To find the highest salary in the company, we can use:

SELECT MAX(salary) AS highest_salary FROM employees;

The result will be:

highest_salary
70000

MAX() with GROUP BY

Often, we need to find the highest value for each category in a table. For instance, finding the highest salary per department:

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

Output:

department highest_salary
HR 50000
IT 70000

This is useful when analyzing data at a grouped level.

Finding the Row with the Maximum Value

Sometimes, just finding the maximum value isn’t enough. We may need to retrieve the entire row that corresponds to the maximum value. Here’s one way to do it:

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

Output:

id name department salary
2 Bob IT 70000

Using MAX() with Dates

The MAX() function also works with date values, making it useful for finding recent records. Suppose we have a “projects” table:

id project_name start_date
1 Project A 2023-01-15
2 Project B 2023-06-10
3 Project C 2023-03-20

To find the most recent project, use:

SELECT MAX(start_date) AS latest_project FROM projects;

The result:

latest_project
2023-06-10

Common Mistakes When Using MAX()

While MAX() is straightforward, here are a few common mistakes to watch out for:

  • Forgetting GROUP BY: If you use MAX() with other columns in a SELECT statement, you need to GROUP BY the other columns, or SQL will throw an error.
  • Using MAX() on NULL values: The MAX() function ignores NULLs, so ensure your dataset doesn’t have missing values if you expect an accurate result.
  • Expecting MAX() to return multiple rows: The function returns a single value, so if you need an entire row, consider using a subquery.

MAX() vs. ORDER BY

Sometimes, people wonder whether to use MAX() or a query with ORDER BY. Here’s a quick comparison:

  • MAX() is faster when you only need the highest value.
  • ORDER BY ... DESC LIMIT 1 is useful when retrieving full row details.

Example using ORDER BY:

SELECT * FROM employees ORDER BY salary DESC LIMIT 1;

Conclusion

The MAX() function in SQL is an essential tool for retrieving the highest values from a dataset. Whether you’re working with numbers, dates, or strings, it offers a reliable and efficient way to extract key insights. However, it’s important to use it correctly, especially in combination with GROUP BY or subqueries, to avoid common pitfalls.

The next time you need to extract maximum values from your data, you’ll know exactly how to apply MAX() efficiently.

 

Other interesting article:

How MIN works in SQL? Best MIN examples