Home » SQL Server MIN

SQL Server MIN

SQL Server MIN() function is an aggregate function that is used to find the smallest  or minimum value of a column.





If a table have only single value then the minimum value will be a value itself.

If all values in a table are same then the minimum value will be a value itself.

Syntax

MIN ( columnname )

Lets look at an example of MIN function in SQL Server.

Here we have a EMPLOYEE table named as EMP.

CREATE TABLE dbo.EMP ( 
EMPID INT NOT NULL, 
EMP_DEPT VARCHAR(50), 
EMP_NAME VARCHAR(50), 
EMP_SALARY NUMERIC(9,2)
)

 INSERT INTO dbo.EMP
(EMPID, EMP_DEPT, EMP_NAME, EMP_SALARY) 
VALUES
(101, 'PRODUCTION', 'RAJAT M',75000.00),
(102, 'PRODUCTION', 'MUKESH BHATIA',70000.00),
(103, 'PRODUCTION', 'MUSKAN MEHTA',75000.00),
(104, 'SALES', 'ROHAN B NARAYA',45000.00),
(105, 'SALES', 'SUSHIL DAS',40000.00),
(106, 'SALES', 'MANISH',45000.00),
(107, 'PRODUCTION', 'RAJESH SINGH',78000.00),
(108, 'HR', 'MOHIN KHAN',50000.00),
(109, 'HR', 'SUSHANT K SINGH',55000.00),
(110, 'HR', 'LAKSHYA RAWAT',55000.00),
(111, 'PRODUCTION', 'MANOJ KUMAR',75000.00),
(112, 'SALES', 'SUJOY M',40000.00),
(113, 'LOGISTIC', 'VINAY AGARWAL',35000.00),
(114, 'LOGISTIC','MUSTAKIM M',35000.00),
(115, 'LOGISTIC', 'VIJAY KUMAWAT',45000.00)

 To get the lowest salary in the entire table

SELECT MIN(EMP_SALARY) AS MIN_SAL FROM dbo.EMP

Get the Lowest salary that is given to employees in each DEPARTMENT

SELECT EMP_DEPT, MIN(EMP_SALARY) AS MIN_SAL FROM dbo.EMP
GROUP BY EMP_DEPT

 

Get the Lowest paid salary for PRODUCTION Department

SELECT MIN(EMP_SALARY) AS MIN_SAL FROM dbo.EMP
WHERE EMP_DEPT = 'PRODUCTION'

 

 

Get the Lowest paid salary in each department that should be less than 50000

SELECT EMP_DEPT, MIN(EMP_SALARY) AS MIN_SAL FROM dbo.EMP
GROUP BY EMP_DEPT
HAVING MIN(EMP_SALARY) < 50000

 




Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.