Master MySQL MAX MIN Functions for Optimized Query Results

MySQL, an open-source relational database management system, offers a variety of functions to retrieve and manipulate data. Among these, the MAX and MIN functions are particularly useful for finding the largest and smallest values in a dataset, respectively. Understanding how to effectively use these functions is crucial for any MySQL user, regardless of their level of expertise.

Group By in SQL With Example [ COUNT, AVG, MAX, MIN ,SUM ]
Group By in SQL With Example [ COUNT, AVG, MAX, MIN ,SUM ]

Whether you're a beginner eager to explore MySQL's capabilities or an experienced user looking to optimize your queries, this guide will delve into the intricacies of the MAX and MIN functions, providing clear explanations and practical examples to enhance your understanding and skills.

7 Function of MySql
7 Function of MySql

Understanding MySQL MAX and MIN Functions

The MAX and MIN functions in MySQL are deterministic functions that return the largest and smallest values, respectively, from a group of values. These functions can be used in SELECT statements,WinnerSELECT statements, HAVING clauses, and even within aggregate functions.

SQL: Group By,  Having, Min, Max Sum
SQL: Group By, Having, Min, Max Sum

MAX and MIN functions can operate on numeric data types (INT, FLOAT, DOUBLE, etc.), as well as date and time data types (DATE, DATETIME, TIMESTAMP, etc.). However, they cannot be used directly on string data types (VARCHAR, CHAR, TEXT, etc.) without the use of specific comparison operators or techniques, such as the LENGTH function.

Using MAX and MIN in SELECT Statements

MySQL Performance Tuning: Tips, Scripts and Tools
MySQL Performance Tuning: Tips, Scripts and Tools

One of the most common uses of MAX and MIN functions is in SELECT statements, where they can be employed to retrieve the maximum and minimum values from a column. Here's a simple example:

<table>
<tr>
<th>Country</th>
<th>Population</th>
</tr>
<tr>
<td>China</td>
<td>1439323776</td>
</tr>
<tr>
<td>India</td>
<td>1380004385</td>
</tr>
<tr>
<td>United States</td>
<td>331002651</td>
</tr>
</table>

Using the above table as an example, the following queries would return the maximum and minimum population values, respectively:

  1. SELECT MAX(Population) FROM Countries;
  2. SELECT MIN(Population) FROM Countries;
SQL to MongoDB: An Updated Mapping
SQL to MongoDB: An Updated Mapping

Using MAX and MIN with HAVING Clauses

MySQL allows the use of MAX and MIN functions in the HAVING clause, which filters data after the aggregate function has been applied. This is particularly useful when you want to find the maximum or minimum value among groups of data. Here's an example:

Consider a table called 'Sales' with the following data:

MySQL AVG Function with Examples - MySQL Tutorial
MySQL AVG Function with Examples - MySQL Tutorial

<table>
<tr>
<th>Product</th>
<th>Salesperson</th>
<th>Amount</th>
</tr>
<tr>
<td>ProductA</td>
<td>John</td>
<td>100</td>
</tr>
<tr>
<td>ProductB</td>
<td>Alice</td>
<td>150</td>
</tr>
<tr>
<td>ProductA</td>
<td>Bob</td>
<td>75</td>
</tr>
<tr>
<td>ProductC</td>
<td>Alice</td>
<td>120</td>
</tr>
</table>

To find the salesperson with the highest and lowest sales amount, you can use the following queries:

  1. SELECT Salesperson, MAX(Amount) FROM Sales GROUP BY Salesperson HAVING MAX(Amount) = MAX(MAX(Amount));
  2. SELECT Salesperson, MIN(Amount) FROM Sales GROUP BY Salesperson HAVING MIN(Amount) = MIN(MIN(Amount));
a computer monitor sitting on top of a desk next to a keyboard and mouse in front of two monitors
a computer monitor sitting on top of a desk next to a keyboard and mouse in front of two monitors
Joins in SQL 🤝
Joins in SQL 🤝
an image of a diagram with the words,'the sol circle'on it
an image of a diagram with the words,'the sol circle'on it
📄 SQL Cheatsheet: Essential Commands and Examples 🛠️ | Programming Valley
📄 SQL Cheatsheet: Essential Commands and Examples 🛠️ | Programming Valley
I cut a SQL query from 70ms to 1ms. No code changes. No caching. Just one… | Milan Jovanović
I cut a SQL query from 70ms to 1ms. No code changes. No caching. Just one… | Milan Jovanović
Megalextoria
Megalextoria
Getting the Most Out of Aggregate Functions in MySQL: Simple Tools for Powerful Data Insights
Getting the Most Out of Aggregate Functions in MySQL: Simple Tools for Powerful Data Insights
an image of a computer screen with text
an image of a computer screen with text
an info poster showing the different types of software
an info poster showing the different types of software
SQL Cheat Sheet | Beginner to Intermediate Quick Reference | Printable PDF | Joins CTEs Window Functions
SQL Cheat Sheet | Beginner to Intermediate Quick Reference | Printable PDF | Joins CTEs Window Functions
Coding Plan от платформы MiniMax :
Coding Plan от платформы MiniMax :
an old computer screen showing the time and date for each item in the program, as well as numbers
an old computer screen showing the time and date for each item in the program, as well as numbers
an image of a computer screen with the words nmap commands
an image of a computer screen with the words nmap commands
In 2025, Learn SQL the Smart Way - Not the hard Way | Mahesh Paruchuri
In 2025, Learn SQL the Smart Way - Not the hard Way | Mahesh Paruchuri
by
by
💻🔐
💻🔐
mIRC
mIRC
a computer screen with the words commix on it
a computer screen with the words commix on it
Nes Shooter Game, Mission Impossible Ii, Impossible Mission 2 Nes, Mission Impossible Title Screen, Top Down Shooter Games Nes, Mission Impossible Fallout 4k Steelbook, Impossible Missions Force Title Card, Mission Impossible Games, Vintage Action Game
Nes Shooter Game, Mission Impossible Ii, Impossible Mission 2 Nes, Mission Impossible Title Screen, Top Down Shooter Games Nes, Mission Impossible Fallout 4k Steelbook, Impossible Missions Force Title Card, Mission Impossible Games, Vintage Action Game
an old computer screen with the words starting ms - dos
an old computer screen with the words starting ms - dos

Advanced Use Cases: Combining MAX and MIN with Other Functions

MAX and MIN functions can be combined with other MySQL functions and operators to create more complex queries. Here are two advanced use cases to illustrate this:

Finding the Largest and Smallest Positive Numbers

Consider a table called 'Numbers' with the following data:

<table>
<tr>
<th>Number</th>
</tr>
<tr>
<td>10</td>
</tr>
<tr>
<td>-5</td>
</tr>
<tr>
<td>7</td>
</tr>
<tr>
<td>0</td>
</tr>
<tr>
<td>15</td>
</tr>
</table>

To find the largest and smallest positive numbers, you can use the following queries with the ABS function:

  1. SELECT MAX(ABS(Number)) FROM Numbers;
  2. SELECT MIN(ABS(Number)) FROM Numbers;

Finding the Age Range in a User Table

Given a 'Users' table with a 'birthdate' column, you can find the age range using the MAX and MIN functions along with the TIMESTAMPDIFF function:

<table>
<tr>
<th>Name</th>
<th>Birthdate</th>
</tr>
<tr>
<td>John</td>
<td>1990-01-15</td>
</tr>
<tr>
<td>Alice</td>
<td>1975-06-22</td>
</tr>
<tr>
<td>Bob</td>
<td>2005-10-05</td>
</tr>
</table>
Query: SELECT TIMESTAMPDIFF(YEAR, MIN(birthdate), CURDATE()) AS 'MinAge', TIMESTAMPDIFF(YEAR, MAX(birthdate), CURDATE()) AS 'MaxAge' FROM Users;

This query returns the minimum and maximum ages among the users in the 'Users' table.

In essence, the MAX and MIN functions in MySQL provide powerful tools for data manipulation and retrieval. By understanding and mastering their usage, you'll be able to unlock the full potential of MySQL and enhance your skills as a database professional. Keep practicing and exploring new use cases to further solidify your understanding of these essential functions.

Related Articles

Ms Sql For Loop Example K3 Database W3 Foreign Key Sql How To Use With Clause In Sql W3 Query Params Youtube Sql Tutorial For Beginners Youtube Sql Query Year Sql Syntax Using Wildcards In Sql W3schools Sql User