We find the minimum value in a column in a dataset, with the MIN Function and we find the maximum value in a column in a dataset, with the with the MAX Function.
Let’s make examples for a better understanding of MIN and MAX Functions.
Example:
First, we create a table with the help of the following script and add a few records into this table.
1 2 3 4 5 6 |
CREATE TABLE [dbo].[MyTable] ([ID] INT IDENTITY, [Name] VARCHAR(20), [Age] INT) INSERT [dbo].[MyTable] VALUES ('Hakan GURBASLAR',36),('Nurullah CAKIR',34),('Faruk ERDEM',27) |
MIN Function Usage:
We find the minimum age value in the table with the following script.
1 |
SELECT MIN(Age) From [dbo].[MyTable] |
MAX Function Usage:
We find the maximum age value in the table with the following script.
1 |
SELECT MAX(Age) From [dbo].[MyTable] |
We usually use the MIN and MAX functions with GROUP BY Clause. You can find detailed information in the following article.
“Group By Clause, SUM, MAX, MIN, AVG, COUNT and COUNT_BIG Functions in SQL Server”