SQL Aggregate Functions: MIN Function Explained
Quick Answer
The SQL MIN function returns the smallest value from a specified column in a dataset. It is an aggregate function commonly used to find minimum values in numeric, date, or string columns within a table or query result.
Learning Objectives
- Understand the purpose and use of the SQL MIN function.
- Write SQL queries using MIN to find the smallest value in a column.
- Apply MIN function with GROUP BY to find minimum values per group.
Introduction
SQL aggregate functions help summarize data by performing calculations on multiple rows.
The MIN function is one such aggregate function that finds the smallest value in a column.
Finding the minimum value is essential for data analysis and reporting.
What is the MIN Function?
The MIN function returns the smallest value from a specified column in a table or query result.
It can be used with numeric, date, and string data types to find the minimum value.
- Syntax: SELECT MIN(column_name) FROM table_name;
- Returns a single value representing the smallest entry in the column.
- Ignores NULL values in the column.
Using MIN with GROUP BY
MIN can be combined with GROUP BY to find the smallest value within each group of rows.
This is useful for grouped data analysis, such as finding the earliest date per category.
- Syntax: SELECT group_column, MIN(column_name) FROM table_name GROUP BY group_column;
- Returns one minimum value per group defined by GROUP BY.
Examples of MIN Function
Here are practical examples demonstrating how to use the MIN function.
Example 1: Find Minimum Salary
This query finds the lowest salary in the employees table.
Example 2: Minimum Order Date per Customer
This query finds the earliest order date for each customer.
Practical Example
This query returns the smallest salary value from the employees table.
This query returns the earliest order date for each customer by grouping orders by customer_id.
Examples
SELECT MIN(salary) AS MinSalary FROM employees;This query returns the smallest salary value from the employees table.
SELECT customer_id, MIN(order_date) AS EarliestOrder FROM orders GROUP BY customer_id;This query returns the earliest order date for each customer by grouping orders by customer_id.
Best Practices
- Always exclude NULL values as MIN ignores them by default.
- Use meaningful aliases for the MIN result column for clarity.
- Combine MIN with GROUP BY to analyze grouped data effectively.
Common Mistakes
- Using MIN without GROUP BY when grouping is needed, leading to incorrect results.
- Expecting MIN to consider NULL values, which it does not.
- Applying MIN on non-comparable data types without proper casting.
Hands-on Exercise
Find Minimum Prices
Write a query to find the minimum product price in the products table.
Expected output: A single value representing the lowest price.
Hint: Use the MIN function on the price column.
Minimum Date per Category
Write a query to find the earliest release date for each product category.
Expected output: A list of categories with their earliest release dates.
Hint: Use MIN with GROUP BY on the category column.
Interview Questions
What does the SQL MIN function do?
InterviewThe MIN function returns the smallest value from a specified column in a dataset, ignoring NULLs.
Can the MIN function be used with GROUP BY?
InterviewYes, MIN is often used with GROUP BY to find the minimum value within each group.
Does the MIN function consider NULL values?
InterviewNo, the MIN function ignores NULL values when calculating 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 SQL MIN function returns the smallest value from a specified column in a dataset.
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 group.
- It works with numeric, date, and string data types.
- MIN is often combined with GROUP BY to aggregate data by categories.
- The SQL MIN function returns the smallest value from a specified column in a dataset.
- It is an aggregate function commonly used to find minimum values in numeric, date, or string columns within a table or query result.
Summary
The SQL MIN function is a fundamental aggregate function used to find the smallest value in a column.
It works with various data types and is commonly combined with GROUP BY for grouped data analysis.
Understanding how to use MIN effectively helps in writing efficient SQL queries for data summarization.
Frequently Asked Questions
What data types can MIN be used with?
MIN can be used with numeric, date, and string data types.
Does MIN include NULL values in its calculation?
No, MIN ignores NULL values and only considers non-NULL entries.
How do I find the minimum value per group in SQL?
Use the MIN function combined with a GROUP BY clause to find the minimum value per group.





