SQL - MIN() Function

The MIN() function is an aggregate function that is used to find the smallest value of given column or expression. It can be applied on numeric, character or date values.

Syntax:

SELECT MIN(column_name)
FROM table_name
[WHERE condition]
[GROUP BY];

For the demo purpose, we will use the following Employee and Department tables in all examples.

EmpId FirstName LastName Email Salary DeptId
1 John King '[email protected]' 24000 10
2 James Bond 4800 20
3 Neena Kochhar '[email protected]test.com' 15000 20
4 Lex De Haan '[email protected]' 9000 30
5 Amit Patel 60000 30
6 Abdul Kalam '[email protected]' 4800 40

The following selects the smallest salary from the Employee table.

SQL Script: MIN()
SELECT MIN(Salary) AS "Smallest Salary" FROM Employees;
Smallest Salary
4800

The following query gets all employees whose salary is the minimum.

SQL Script: MIN()
SELECT * FROM Employee 
WHERE Salary = (SELECT MIN(Salary) FROM Employee);
EmpId FirstName LastName Email Salary DeptId
2 James Bond 4800 20
6 Abdul Kalam '[email protected]' 4800 40

The MIN() is an aggregate function, so it can be used in Group By queries. The following query gets Smallest salary in each department.

SQL Script: MIN() with Group By
SELECT DeptId, MIN(Salary) AS "Smallest Salary" 
FROM Employee
GROUP BY DeptId;
DeptId Smallest Salary
10 24000
20 15000
30 9000
40 4800

The MIN() function can be allpied on the varchar columns. The following selects the smallest FirstName from the Employee table.

SQL Script: MIN()
SELECT MIN(FirstName) AS "Smallest FirstName" FROM Employee;
Smallest FirstName
Lex