SQL MIN Function

·

·

,

The SQL MIN function returns the smallest value within a table or group.  Throughout this section we’ll use the HumanResource.Employee table for our examples: 

Using the data above, MIN calculates the smallest SickLeaveHours amount for the entire table:

SELECT MIN(sickleavehours) MinSickLeaveHours FROM HumanResources.Employee
SELECT MIN(sickleavehours) MinSickLeaveHours 
  FROM HumanResources.Employee

When used with GROUP BY, MIN returns the largest value within a group.  Here is a similar query showing the minimum SickLeaveHours by JobTitle: 

SELECT JobTitle , MIN(sickleavehours) AS MinSickLeaveHours FROM HumanResources.Employee GROUP BY JobTitle ORDER BY JobTitle;
SELECT JobTitle 
 , MIN(sickleavehours) AS MinSickLeaveHours 
FROM HumanResources.Employee 
GROUP BY JobTitle 
ORDER BY JobTitle;

Additional SQL MIN Resources  

To learn more, check out these useful resources: 

More from the blog


MySQL PostgreSQL SQLite SqlServer