SQL Aggregate Functions: MAX Function Tutorial
Quick Answer
The SQL MAX function returns the maximum value from a specified column in a table. It is commonly used with numeric, date, or string data types to find the highest value within a group or entire dataset.
Learning Objectives
- Understand the purpose and use cases of the SQL MAX function.
- Learn the syntax and how to write queries using MAX.
- Apply MAX in combination with GROUP BY to find maximum values per group.
Introduction
SQL aggregate functions help summarize data by performing calculations on multiple rows.
The MAX function specifically finds the largest value in a column, which is useful for identifying top scores, latest dates, or highest prices.
Aggregate functions like MAX help us quickly find meaningful insights from large datasets.
What is the SQL MAX Function?
The MAX function returns the maximum value from a column across all rows or within groups when used with GROUP BY.
It can be applied to numeric, date/time, and string columns to find the highest value.
- Syntax: SELECT MAX(column_name) FROM table_name;
- Returns the largest value ignoring NULLs.
- Can be combined with WHERE to filter rows before aggregation.
Using MAX with GROUP BY
When you want the maximum value per category, use MAX with GROUP BY.
This groups rows by a column and returns the maximum value for each group.
- Syntax: SELECT category_column, MAX(value_column) FROM table_name GROUP BY category_column;
- Useful for finding top sales per region, highest scores per class, etc.
Examples of SQL MAX Function
Here are practical examples demonstrating how to use MAX in queries.
Example 1: Find the Highest Salary
This query returns the highest salary from the employees table.
Example 2: Highest Salary per Department
This query finds the maximum salary for each department.
Practical Example
This query returns the single highest salary value from the employees table.
This query returns the highest salary for each department by grouping rows by department_id.
Examples
SELECT MAX(salary) AS HighestSalary FROM employees;This query returns the single highest salary value from the employees table.
SELECT department_id, MAX(salary) AS HighestSalary FROM employees GROUP BY department_id;This query returns the highest salary for each department by grouping rows by department_id.
Best Practices
- Always use meaningful aliases for aggregated columns for clarity.
- Combine MAX with WHERE to filter data before aggregation when needed.
- Use GROUP BY carefully to avoid unexpected results.
- Remember MAX ignores NULL values automatically.
Common Mistakes
- Using MAX without GROUP BY when multiple groups are intended.
- Expecting MAX to return the entire row with the maximum value (it only returns the value).
- Including non-aggregated columns in SELECT without GROUP BY, causing errors.
- Not handling NULL values when they are relevant to the dataset.
Hands-on Exercise
Find Maximum Order Amount
Write a query to find the highest order amount from the orders table.
Expected output: A single value representing the highest order amount.
Hint: Use the MAX function on the order_amount column.
Maximum Price Per Product Category
Write a query to find the maximum price for each product category.
Expected output: A list of categories with their maximum prices.
Hint: Use MAX with GROUP BY on the category column.
Interview Questions
What does the SQL MAX function do?
InterviewThe SQL MAX function returns the maximum value from a specified column across rows.
Can MAX be used with non-numeric data?
InterviewYes, MAX can be used with date and string data types to find the highest or latest value.
How do you find the maximum value per group in SQL?
InterviewUse MAX in combination with GROUP BY to find the maximum value for each group.
MCQ Quiz
1. What is the best first step when learning MAX Function?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce MAX Function?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. The SQL MAX function returns the maximum value from a specified column in a table.
B. MAX Function never needs examples
C. MAX Function is unrelated to practical work
D. MAX Function should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- MAX returns the highest value in a column for a set of rows.
- It works with numeric, date, and string data types.
- Combining MAX with GROUP BY allows finding maximum values per category.
- MAX ignores NULL values in its calculation.
- The SQL MAX function returns the maximum value from a specified column in a table.
Summary
The SQL MAX function is a powerful aggregate tool to find the highest value in a column.
It works with various data types and can be combined with GROUP BY to get maximum values per group.
Understanding how to use MAX correctly helps in data analysis and reporting tasks.
Frequently Asked Questions
What data types can MAX be used with?
MAX can be used with numeric, date/time, and string data types.
Does MAX consider NULL values?
No, MAX ignores NULL values when calculating the maximum.
Can MAX return the entire row with the maximum value?
No, MAX only returns the maximum value of a column, not the entire row.





