Free Web Development Online Tutorials, Learn to Code
SQL Min and Max methods, MS SQL Tutorial Online

Min and Max Function in SQL Server Database

SQL Min or Max in-built

Min / Max functions are in-built in SQL, very useful finding the minimum and maximum values for particular column that can help you get an insight of your data

SQL MAX method example

To get the maximum value of a integer/numeric column use the MAX() function.

SELECT MAX(numericColumnName) FROM tableName;
SELECT MAX(numericColumnName) FROM tableName GROUP BY ColumnName;

SQL MIN method example

To get the minimum value of a integer/numeric column use the MIN() function.

SELECT MIN(numericColumnName) FROM tableName;
SELECT MIN(numericColumnName) FROM tableName GROUP BY ColumnName;

Suppose we want to find the max and min PointValue from the data below

tbCustomer
--+----+----------+-----+-----------+----------+--------
| Id | Name     | Code |        City    |   PointValue   |
+----+----------+-----+-----------------+----------------
|  1 | Ajit     |  0112 | Mumbai        |  8054069773 |
|  2 | Ropesh   |  0247 | Delhi         |  7852014758 |
|  3 | Aniket   |  0142 | Kolkata       |  8852104758 |
|  4 | Brinda   |  0235 | Pune          |  9954254758 |
|  5 | Aman     |  0478 | Raipur        |  9652014758 |
|  6 | Kajal    |  0268 | Goa           |  9452605758 |
|  7 | Purnendu |  1403 | Bangalore     |  9652720758 |
+----+----------+-------+---------------+-------------+

Remember, to use the max, min function in SQL query, the column data type must be numeric / integer

select MIN(PointValue) from [dbo].[tbCustomer]
select Max(PointValue) from [dbo].[tbCustomer]
Comment
Name
Email
Website
Subscribe