MySQL Aggregate Functions: MIN Function Tutorial
Quick Answer
The MySQL MIN function returns the smallest value from a set of values in a column. It is commonly used in aggregate queries to find minimum values in numeric, date, or string columns.
Learning Objectives
- Understand the purpose and syntax of the MySQL MIN function.
- Learn how to use MIN in SQL queries to find minimum values.
- Identify practical use cases for the MIN function in database queries.
Introduction to the MySQL MIN Function
The MIN function in MySQL is an aggregate function used to find the smallest value in a column or expression.
It is useful when you want to identify minimum values in datasets, such as the earliest date or lowest price.
Finding the minimum value is often the first step in data analysis.
Understanding the MIN Function Syntax
The basic syntax of the MIN function is straightforward and easy to use.
It takes a single argument, which is the column or expression to evaluate.
- MIN(column_name)
- MIN(expression)
Using MIN with Numeric Data
When applied to numeric columns, MIN returns the smallest numeric value.
This is useful for finding the lowest price, minimum age, or smallest quantity.
Example: Finding the Lowest Price
Suppose you have a products table with a price column. To find the lowest price, you can use:
Using MIN with Dates and Strings
MIN can also be used with date columns to find the earliest date.
When used with strings, MIN returns the lexicographically smallest value.
Example: Earliest Order Date
To find the earliest order date from an orders table, use MIN on the order_date column.
Using MIN with GROUP BY Clause
MIN is often combined with GROUP BY to find the minimum value per group.
This is useful for grouped data analysis, such as finding the lowest salary per department.
Example: Minimum Salary per Department
You can group employees by department and find the minimum salary in each group.
Practical Example
This query returns the smallest price value from the products table.
This query returns the earliest date from the order_date column.
This query finds the lowest salary in each department.
Examples
SELECT MIN(price) AS LowestPrice FROM products;This query returns the smallest price value from the products table.
SELECT MIN(order_date) AS EarliestOrder FROM orders;This query returns the earliest date from the order_date column.
SELECT department_id, MIN(salary) AS MinSalary FROM employees GROUP BY department_id;This query finds the lowest salary in each department.
Best Practices
- Use MIN only on columns where finding the smallest value makes sense.
- Combine MIN with GROUP BY to analyze grouped data effectively.
- Be aware that MIN ignores NULL values in the column.
- Use meaningful aliases for the result column to improve readability.
Common Mistakes
- Using MIN on columns with all NULL values returns NULL, which may be unexpected.
- Forgetting to use GROUP BY when trying to find minimum values per group.
- Assuming MIN works on multiple columns at once; it only accepts one argument.
- Not handling NULL values properly in queries involving MIN.
Hands-on Exercise
Find the Minimum Age
Write a query to find the minimum age from a users table.
Expected output: A single value representing the smallest age.
Hint: Use the MIN function on the age column.
Minimum Date per Category
Write a query to find the earliest date for each category in a sales table.
Expected output: A list of categories with their earliest dates.
Hint: Use MIN with GROUP BY category.
Interview Questions
What does the MySQL MIN function do?
InterviewThe MIN function returns the smallest value from a set of values in a specified column or expression.
Can the MIN function be used with non-numeric data?
InterviewYes, MIN works with numeric, date, and string data types, returning the smallest or earliest value accordingly.
How does MIN handle NULL values?
InterviewMIN ignores NULL values and only considers non-NULL values when determining the minimum.
MCQ Quiz
1. What is the best first step when learning MIN 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 MIN 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 MySQL MIN function returns the smallest value from a set of values in a column.
B. MIN Function never needs examples
C. MIN Function is unrelated to practical work
D. MIN Function should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- MIN returns the smallest value in a column or expression.
- It works with numeric, date, and string data types.
- MIN is often used with GROUP BY to find minimum per group.
- NULL values are ignored by the MIN function.
- The MySQL MIN function returns the smallest value from a set of values in a column.
Summary
The MySQL MIN function is a powerful aggregate tool to find the smallest value in a column or expression.
It works with various data types and is commonly used with GROUP BY for grouped minimum values.
Understanding how MIN handles NULLs and data types helps write accurate and efficient queries.
Frequently Asked Questions
Can MIN be used on multiple columns at once?
No, MIN accepts only one column or expression at a time.
What happens if all values are NULL in the MIN function?
MIN returns NULL if all values in the column are NULL.
Is MIN case-sensitive when used with strings?
MIN compares strings based on the collation of the column, which may be case-insensitive depending on settings.





