đ The MAX() function is an aggregate function in SQL that returns the largest value from a specified column. It works with numeric values, dates, and text data. The MAX()function is commonly used to find the highest salary, maximum score, latest date, greatest value, and other maximum values in a dataset.
đ What is MAX()?
The MAX() function scans all selected values in a column and returns the largest one. It automatically ignores NULL values during the calculation.
Information
đ Basic Syntax
MAX() Syntax
SELECT MAX(column_name)
FROM table_name
WHERE condition;đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Marks | AdmissionDate |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 92 | 2024-01-15 |
| 102 | Bob | Mathematics | 85 | 2024-02-10 |
| 103 | Charlie | Physics | 78 | 2024-03-20 |
| 104 | David | Computer Science | 95 | 2024-04-05 |
| 105 | Eva | Mathematics | 88 | 2024-05-12 |
1ī¸âŖ Finding the Maximum Numeric Value
Retrieve the highest marks obtained by any student.
Maximum Marks
SELECT MAX(Marks) AS HighestMarks
FROM Students;Result:
| HighestMarks |
|---|
| 95 |
2ī¸âŖ MAX() with WHERE
Use the WHERE clause to find the maximum value from filtered records.
Highest Marks in Computer Science
SELECT MAX(Marks) AS HighestCSMarks
FROM Students
WHERE Department = 'Computer Science';3ī¸âŖ MAX() with Dates
The MAX() function can return the latest date in a column.
Latest Admission Date
SELECT MAX(AdmissionDate) AS LatestAdmission
FROM Students;4ī¸âŖ MAX() with Text
The MAX() function also works with character data. The largest value is determined according to the database's collation and sorting rules.
Alphabetically Last Name
SELECT MAX(Name) AS LastName
FROM Students;Important
đ MAX() with GROUP BY
The GROUP BY clause groups rows, and MAX() returns the largest value for each group.
Highest Marks by Department
SELECT Department,
MAX(Marks) AS HighestMarks
FROM Students
GROUP BY Department;Sample Result:
| Department | HighestMarks |
|---|---|
| Computer Science | 95 |
| Mathematics | 88 |
| Physics | 78 |
đ MAX() with HAVING
The HAVING clause filters grouped results after the maximum values have been calculated.
Departments with Maximum Marks Above 90
SELECT Department,
MAX(Marks) AS HighestMarks
FROM Students
GROUP BY Department
HAVING MAX(Marks) > 90;đ MAX() with JOIN
The MAX() function is frequently used with joins to determine the highest value across related tables.
Highest Course Score per Student
SELECT s.Name,
MAX(c.Score) AS HighestScore
FROM Students s
JOIN Courses c
ON s.StudentID = c.StudentID
GROUP BY s.Name;â ī¸ MAX() and NULL Values
The MAX() function ignores NULL values. Only valid values are considered when determining the maximum.
| Column Values | MAX() Result |
|---|---|
| 10, 20, NULL, 30 | 30 |
Important
đ Common Uses of MAX()
| Purpose | Example |
|---|---|
| Highest Marks | MAX(Marks) |
| Highest Salary | MAX(Salary) |
| Highest Price | MAX(Price) |
| Latest Date | MAX(OrderDate) |
| Department-wise Maximum | MAX(...) GROUP BY Department |
âī¸ MAX() vs MIN()
| Function | Purpose |
|---|---|
| MAX() | Returns the largest value. |
| MIN() | Returns the smallest value. |
đŧ Real-World Example
A university administrator wants to identify the highest marks achieved in each department.
Department-wise Maximum Marks
SELECT Department,
MAX(Marks) AS HighestMarks
FROM Students
GROUP BY Department
ORDER BY HighestMarks DESC;This query generates a report showing the highest marks for every department, sorted from the highest value to the lowest.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ MAX() returns the largest value in a column.
- đ It works with numeric, date, and text data types.
- đ It ignores NULL values.
- đ It is commonly used with WHERE, GROUP BY, and HAVING.
- đ It can be combined with joins for advanced queries.
- đ Text results depend on the database's collation and sorting rules.