đ The MIN() function is an aggregate function in SQL that returns the smallest value from a specified column. It can be used with numeric values, dates, and text. The MIN() function is widely used to find the lowest price, earliest date, minimum score, shortest value according to sorting rules, and other minimum values in a dataset.
đ What is MIN()?
The MIN() function scans all selected values in a column and returns the smallest one. It ignores NULL values during the calculation.
Information
đ Basic Syntax
MIN() Syntax
SELECT MIN(column_name)
FROM table_name
WHERE condition;đ Sample Table
Consider the following Students table:
| StudentID | Name | Department | Marks | AdmissionDate |
|---|---|---|---|---|
| 101 | Alice | Computer Science | 92 | 2024-01-15 |
| 102 | Bob | Mathematics | 85 | 2024-02-10 |
| 103 | Charlie | Physics | 78 | 2024-03-20 |
| 104 | David | Computer Science | 95 | 2024-04-05 |
| 105 | Eva | Mathematics | 88 | 2024-05-12 |
1ī¸âŖ Finding the Minimum Numeric Value
Retrieve the lowest marks obtained by any student.
Minimum Marks
SELECT MIN(Marks) AS LowestMarks
FROM Students;Result:
| LowestMarks |
|---|
| 78 |
2ī¸âŖ MIN() with WHERE
Use the WHERE clause to find the minimum value within filtered records.
Lowest Marks in Computer Science
SELECT MIN(Marks) AS LowestCSMarks
FROM Students
WHERE Department = 'Computer Science';3ī¸âŖ MIN() with Dates
The MIN() function can return the earliest date in a column.
Earliest Admission Date
SELECT MIN(AdmissionDate) AS FirstAdmission
FROM Students;4ī¸âŖ MIN() with Text
The MIN() function can also be used with character data. The smallest value is determined according to the database's collation and sorting rules.
Alphabetically First Name
SELECT MIN(Name) AS FirstName
FROM Students;Important
đ MIN() with GROUP BY
The GROUP BY clause groups records, and MIN() returns the smallest value for each group.
Lowest Marks by Department
SELECT Department,
MIN(Marks) AS LowestMarks
FROM Students
GROUP BY Department;Sample Result:
| Department | LowestMarks |
|---|---|
| Computer Science | 92 |
| Mathematics | 85 |
| Physics | 78 |
đ MIN() with HAVING
The HAVING clause filters grouped results after the minimum values have been calculated.
Departments with Minimum Marks Above 80
SELECT Department,
MIN(Marks) AS LowestMarks
FROM Students
GROUP BY Department
HAVING MIN(Marks) > 80;đ MIN() with JOIN
The MIN() function is commonly used with joins to find the minimum value across related tables.
Lowest Course Score per Student
SELECT s.Name,
MIN(c.Score) AS LowestScore
FROM Students s
JOIN Courses c
ON s.StudentID = c.StudentID
GROUP BY s.Name;â ī¸ MIN() and NULL Values
The MIN() function ignores NULL values. Only valid values are considered when determining the minimum.
| Column Values | MIN() Result |
|---|---|
| 10, 20, NULL, 5 | 5 |
Important
đ Common Uses of MIN()
| Purpose | Example |
|---|---|
| Lowest Marks | MIN(Marks) |
| Lowest Salary | MIN(Salary) |
| Lowest Price | MIN(Price) |
| Earliest Date | MIN(OrderDate) |
| Department-wise Minimum | MIN(...) GROUP BY Department |
âī¸ MIN() vs MAX()
| Function | Purpose |
|---|---|
| MIN() | Returns the smallest value. |
| MAX() | Returns the largest value. |
đŧ Real-World Example
A university administrator wants to identify the lowest marks scored in each department.
Department-wise Minimum Marks
SELECT Department,
MIN(Marks) AS LowestMarks
FROM Students
GROUP BY Department
ORDER BY LowestMarks ASC;This query generates a report showing the minimum marks for every department, sorted from the lowest value to the highest.
â ī¸ Best Practices
Best Practice
đ Key Points to Remember
- đ MIN() returns the smallest value in a column.
- đ It works with numeric, date, and text data types.
- đ It ignores NULL values.
- đ It is commonly used with WHERE, GROUP BY, and HAVING.
- đ It can be combined with joins for advanced queries.
- đ Text results depend on the database's collation and sorting rules.