What is SQL MIN Function?

The SQL MIN function is a powerful aggregate function that retrieves the smallest value from a column in a table. Its syntax is simple and strong.

SELECT MIN(Column_Name) FROM Table_Name WHERE [Condition];

This syntax retrieves the minimum value and also allows filtering through the WHERE clause for a more refined outcome.

Why Should You Care?

Dealing with a dataset of cars, each with unique attributes, the MIN function efficiently identifies the car with the smallest number or the student with the lowest marks. It's all about efficiency!

.

Real World Examples

Example 1: Bikes_Details Table

Create a hypothetical scenario with a table named Car_Details. The table contains fields like Car_Number, Car_Model, Car_Name, Number_of_Cars, and Car_Price.

CREATE TABLE Car_Details  
(  
    Car_Number INT PRIMARY KEY,  
    Car_Model INT,  
    Car_Name VARCHAR (50),  
    Number_of_Cars INT NOT NULL,
    Car_Price INT NOT NULL  
);

After populating it with data, we can use the MIN function:

SELECT MIN(Car_Number) AS "Smallest Car Number" FROM Car_Details;

Example 2: College_Student_Details Table

Let's now delve into the College_Student_Details table, which includes fields such as Student_ID, Student_Name, Student_Course, and Student_Marks.

CREATE TABLE Student_Details  
(  
    Student_ID INT NOT NULL,   
    Student_Name varchar(100),  
    Student_Course varchar(50),  
    Student_Marks INT  
);

After injecting some academic flair, we can identify the minimum marks above 60:

SELECT MIN(Student_Marks) AS "Lowest Marks Above 70" FROM College_Student_Details WHERE Student_Marks > 70;

MIN Function with GROUP BY Clause

Enhance your SQL proficiency by using the GROUP BY clause with the MIN function. This powerful combination enables you to identify the smallest value within each group from a table.

SELECT Student_Course, MIN(Student_Marks) FROM Student_Details GROUP BY Student_Course;

This query displays the minimum marks for each course, offering a nuanced perspective.