LogoInterview Master

SQL Learning Hub

SQL MAX Function

Understanding the SQL MAX Function

The MAX function is a SQL aggregate function designed to return the maximum (largest) value from a set of values in a specified column. It's incredibly versatile, working not just with numbers but also with dates, times, and strings (based on sort order). Use MAX to identify peak performance, find the latest event, or determine the upper bound of your data.

Basic Syntax

MAX operates on a single column and returns a single value representing the maximum found within the selected rows.

Common SQL MAX Interview Questions

  • Explain how MAX handles NULL values.
  • How does MAX work with string/text data? What determines the "maximum"?
  • How can you retrieve the entire row (or multiple rows) that contains the maximum value, not just the value itself?
  • Describe how to find the Nth highest value (e.g., the second or third maximum).
  • When would you use MAX with GROUP BY? Provide an example.

MAX Function Basics

Finding the Maximum Numeric Value

The most straightforward use is finding the largest number in a column.

Result: A single value representing the peak view count across all videos.

Finding the Latest Date/Timestamp

MAX is very useful for finding the most recent date or time in a dataset.

Result: The latest date or timestamp value present in the respective column.

Finding the Maximum String Value

When used with text or string columns, MAX returns the value that comes last in alphabetical (or dictionary) order, according to the database's collation rules.

Result: The username like "ZebraUser" would typically be returned over "AppleUser". Case sensitivity depends on the database collation settings.

MAX and NULL Value Handling

Similar to SUM and AVG, the MAX function ignores NULL values when determining the maximum. If all values considered are NULL, MAX returns NULL.

MAX vs. MIN

MAX finds the largest value, while its counterpart, MIN, finds the smallest value. They work identically regarding data types and NULL handling but return opposite ends of the value spectrum. They are often discussed together.

Learn about MIN

Finding Rows Associated with the MAX Value

A common requirement is not just finding the maximum value itself, but retrieving the entire row(s) that contain this maximum value. MAX() alone only returns the value. Here are common patterns to get the full record:

Method 1: Using a Subquery in the WHERE Clause

This is the most common and intuitive method. First, find the maximum value using a subquery, then select rows where the column equals that maximum value.

This returns all columns for any video whose views match the overall maximum. If multiple videos tie for the highest view count, all of them will be returned.

Method 2 [Advanced]: Using Window Functions

Window functions like RANK() or DENSE_RANK() can identify the top record(s) without a separate subquery for the MAX value.

This approach can be more efficient, especially if you need the top N records, and avoids calculating MAX separately. RANK() handles ties by giving them the same rank and skipping subsequent ranks; DENSE_RANK() also gives ties the same rank but doesn't skip ranks.

Method 3: Using ORDER BY and LIMIT (Simple for Top 1)

If you only need one row containing the maximum value (and don't care about ties), sorting and limiting is the simplest way.

Note: If there's a tie for the maximum value, this method arbitrarily picks only one of the top rows. Use methods 1 or 2 if you need all tied rows.

Using MAX with GROUP BY

Combining MAX with GROUP BY allows you to find the maximum value within each distinct category or group in your data.

Finding Maximums within Categories

Determine the peak value for each group. For example, this query shows the best performance achieved by each individual user, regardless of their average performance.

Finding the Latest Event per Category

Identify the most recent occurrence within each group. This query helps identify which videos have the most recent engagement activity.

Combining MAX with Other Aggregates per Group

Get a fuller picture by calculating multiple aggregate metrics for each group. This provides a performance range (max vs. min) and central tendency (avg) for each user's video portfolio.

Filtering Groups Based on MAX (HAVING Clause)

Select only groups where the maximum value meets a certain condition.

Advanced MAX Techniques & Scenarios

Finding the Nth Maximum Value

Finding values other than the absolute maximum often involves window functions or clever subqueries.

The window function approach using DENSE_RANK() is generally more flexible for finding the Nth value and handles ties gracefully.

Finding the Row with MAX Value per Group (Greatest-N-Per-Group)

This is a common, slightly complex task: retrieve the full row corresponding to the maximum value within each group (e.g., find each user's most viewed video details). Window functions are often the most efficient solution.

ROW_NUMBER() assigns a unique number within each partition. Using PARTITION BY user_id and ORDER BY views DESC ensures rn = 1 corresponds to the highest-view video for that user. Other methods involving subqueries or lateral joins exist but can be less performant.

Summary

The MAX function is a simple yet powerful tool for identifying the largest or latest values in SQL:

  • It returns the maximum value from a specified column.
  • Works with numeric, date/time, and string data types.
  • Ignores NULL values.
  • Use with GROUP BY to find the maximum value within each category.
  • Use with HAVING to filter groups based on their maximum value.
  • Retrieving the full row containing the MAX value typically requires subqueries, window functions (like RANK or ROW_NUMBER), or ORDER BY with LIMIT 1 (if ties are unimportant).
  • Finding the Nth maximum value often involves window functions or nested subqueries.

Understanding MAX and how to use it effectively, especially for finding associated records and within groups, is essential for data analysis and a common topic in SQL interviews.

Test Your Knowledge

Take a quick quiz to reinforce what you've learned about SQL MAX Function

Ready for hands-on SQL practice?

We have 200+ questions on real companies and real products.

Find a question to practice
Dawn AI