MYSQL

9.MySQL MIN and MAX – Finding the Extremes in Your Data

📊 MySQL MIN and MAX – Finding the Extremes in Your Data

When working with databases, sometimes you need to find the smallest or largest value in a column. MySQL provides the MIN() and MAX() functions to help you retrieve the minimum and maximum values from a specific column, respectively. These functions are essential for data analysis, report generation, and decision-making processes.

In this guide, we’ll explore how to use the MIN() and MAX() functions effectively in MySQL with examples and real-world applications.

🧠 What are the MIN() and MAX() Functions?

  • MIN(): Returns the smallest value in a given column.

  • MAX(): Returns the largest value in a given column.

Both functions can be used with numeric, date, and string values. These aggregate functions are typically used with the SELECT statement to return a single value that represents the smallest or largest data in a column.

🧾 Syntax of MIN() and MAX()

SELECT MIN(column_name) FROM table_name;
SELECT MAX(column_name) FROM table_name;
  • column_name: The name of the column in which you want to find the minimum or maximum value.

  • table_name: The name of the table from which you want to retrieve the data.

📋 Examples of Using MIN() and MAX()

Let’s walk through a few examples to understand how MIN() and MAX() work.

1️⃣ Finding the Minimum Value

If you want to find the minimum value of a particular column, you can use the MIN() function.

Example: Find the minimum salary from the employees table

SELECT MIN(salary) AS min_salary
FROM employees;

🟢 Result: This will return the lowest salary value from the salary column in the employees table.

2️⃣ Finding the Maximum Value

Similarly, to find the maximum value of a column, use the MAX() function.

Example: Find the maximum salary from the employees table

SELECT MAX(salary) AS max_salary
FROM employees;

🟢 Result: This will return the highest salary value from the salary column in the employees table.

3️⃣ Using MIN() and MAX() with Other Columns

You can also combine the MIN() and MAX() functions with other columns to find the smallest or largest values along with other related data.

Example: Find the employee with the minimum salary

SELECT first_name, last_name, salary
FROM employees
WHERE salary = (SELECT MIN(salary) FROM employees);

🟢 Result: This will return the first name, last name, and salary of the employee(s) with the minimum salary in the employees table.

Example: Find the employee with the maximum salary

SELECT first_name, last_name, salary
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);

🟢 Result: This will return the first name, last name, and salary of the employee(s) with the highest salary in the employees table.

4️⃣ Using MIN() and MAX() with GROUP BY

You can use MIN() and MAX() with the GROUP BY clause to find the minimum or maximum value for each group.

Example: Find the minimum and maximum salary by department

SELECT department_id, MIN(salary) AS min_salary, MAX(salary) AS max_salary
FROM employees
GROUP BY department_id;

🟢 Result: This will return the minimum and maximum salaries for each department.

5️⃣ Using MIN() and MAX() with WHERE Clause

You can combine MIN() and MAX() with the WHERE clause to filter data based on specific conditions before finding the extremes.

Example: Find the minimum salary in department 3

SELECT MIN(salary) AS min_salary
FROM employees
WHERE department_id = 3;

🟢 Result: This will return the lowest salary of employees working in department 3.

Example: Find the maximum salary in department 2

SELECT MAX(salary) AS max_salary
FROM employees
WHERE department_id = 2;

🟢 Result: This will return the highest salary of employees working in department 2.

6️⃣ MIN() and MAX() with Date and String Values

MIN() and MAX() can also be used with date and string columns to find the earliest/latest date or lexicographically smallest/largest string.

Example: Find the earliest hire date

SELECT MIN(hire_date) AS earliest_hire_date
FROM employees;

🟢 Result: This will return the earliest hire date from the employees table.

Example: Find the latest hire date

SELECT MAX(hire_date) AS latest_hire_date
FROM employees;

🟢 Result: This will return the most recent hire date from the employees table.

Example: Find the lexicographically smallest first name

SELECT MIN(first_name) AS smallest_first_name
FROM employees;

🟢 Result: This will return the lexicographically smallest first name from the employees table.

Example: Find the lexicographically largest last name

SELECT MAX(last_name) AS largest_last_name
FROM employees;

🟢 Result: This will return the lexicographically largest last name from the employees table.

📘 Real-World Use Cases of MIN() and MAX()

🔹 Use Case 1: Finding Extremes in Sales Data

In retail or e-commerce, you might want to find the highest and lowest sales amounts in a given period.

SELECT MIN(sale_amount) AS min_sale, MAX(sale_amount) AS max_sale
FROM sales
WHERE sale_date BETWEEN '2025-01-01' AND '2025-12-31';

🟢 Result: This will return the lowest and highest sale amounts for the year 2025.

🔹 Use Case 2: Identifying the Most and Least Expensive Products

For product-based businesses, knowing the most and least expensive products helps with pricing strategies.

SELECT MIN(price) AS cheapest_product, MAX(price) AS most_expensive_product
FROM products;

🟢 Result: This will return the price of the cheapest and most expensive product in the products table.

🧠 Summary of MIN() and MAX() Functions

  • MIN(): Returns the smallest value from a column (numeric, date, or string).

  • MAX(): Returns the largest value from a column (numeric, date, or string).

  • Can be used with GROUP BY: You can calculate the extremes for each group of data.

  • Can be used with WHERE: You can filter data before finding the minimum or maximum value.

  • Works with various data types: Supports numeric, date, and string data types.


💡 Tip from Acesoftech Academy: Always double-check that you’re applying MIN() or MAX() to the correct column and, if necessary, use WHERE or GROUP BY to refine your query results.

🏁 Final Thoughts

The MIN() and MAX() functions are powerful tools for identifying the smallest and largest values in your MySQL database. Whether you’re calculating extremes for pricing, dates, or employee salaries, these aggregate functions help you make informed decisions based on data insights.

Leave a Reply

Your email address will not be published. Required fields are marked *