MySQL MIN Function: Query The Minimum Value Of A Specified Column

The MIN() function in MySQL is an aggregate function that returns the minimum value in a set of values. You can use the MIN() function to select the lowest (minimum) value in a column.

Syntax:

SELECT MIN(column_name)
FROM table_name
WHERE condition;

Example:

Consider we have a table named products, which contains the following data:

ProductIDProductNamePrice
1Apple1.2
2Banana0.8
3Orange1
4Kiwi1.5
5Mango1.3

If we want to find the product with the lowest price, we can use the MIN() function like this:

SELECT MIN(Price) AS MinPrice
FROM products;

The MinPrice after AS is an alias which will be used as the column name for the result set.

This will return:

MinPrice
0.8

Using MIN() in a column with text data:

You can use the MIN() function on a column containing text data to get the minimum value according to the column's collation.

For example, if you want to find the product that is first in alphabetical order, you could use the MIN() function like this:

SELECT MIN(ProductName) AS FirstProduct
FROM products;

This will return:

FirstProduct
Apple

Using MIN() with GROUP BY:

You can use the MIN() function in conjunction with the GROUP BY clause to get the minimum value for each group.

For example, consider we have another table orders:

OrderIDProductIDQuantity
115
2210
313
437
522

If you want to find the minimum quantity ordered of each product, you could do this:

SELECT ProductID, MIN(Quantity) AS MinQuantity
FROM orders
GROUP BY ProductID;

This will return:

ProductIDMinQuantity
13
22
37

This shows the minimum quantity ordered for each product in the orders table.

  1. MySQL MIN Function Example:

    • Description: The MIN function in MySQL is used to find the minimum value in a set of values.
    • Example Code:
      SELECT MIN(column_name) AS min_value
      FROM table_name;
      
  2. How to Use MIN Function in MySQL:

    • Description: Use the MIN function to retrieve the minimum value from a specific column in a table.
    • Example Code:
      SELECT MIN(price) AS min_price
      FROM products;
      
  3. Finding the Minimum Value with MIN in MySQL:

    • Description: MIN function is applied to find the minimum value in a column, such as finding the lowest salary in an employee table.
    • Example Code:
      SELECT MIN(salary) AS lowest_salary
      FROM employees;
      
  4. Grouping and MIN Function in MySQL:

    • Description: Use the MIN function with GROUP BY to find the minimum value for each group.
    • Example Code:
      SELECT department_id, MIN(salary) AS min_salary
      FROM employees
      GROUP BY department_id;
      
  5. MIN Function with WHERE Clause in MySQL:

    • Description: Apply the MIN function with a WHERE clause to find the minimum value based on specific conditions.
    • Example Code:
      SELECT MIN(total_amount) AS min_order_amount
      FROM orders
      WHERE status = 'Shipped';
      
  6. Using MIN Function for Date and Time Values in MySQL:

    • Description: MIN can be used for date and time columns, useful for finding the earliest or oldest date.
    • Example Code:
      SELECT MIN(order_date) AS earliest_order_date
      FROM orders;
      
  7. Handling NULL Values with MIN Function in MySQL:

    • Description: The MIN function ignores NULL values by default. If you want to consider NULL values, use the IFNULL or COALESCE function.
    • Example Code:
      SELECT MIN(IFNULL(salary, 0)) AS min_salary
      FROM employees;
      
  8. Examples of Using MIN Function in MySQL Queries:

    • Description: Various examples showcasing the application of MIN function in different scenarios.
    • Example Code:
      SELECT MIN(stock_quantity) AS min_stock
      FROM products;
      
      SELECT department_id, MIN(hire_date) AS earliest_hire_date
      FROM employees
      GROUP BY department_id;