How to use SQL MIN MAX Functions in program with example
SQL SELECT MIN MAX functions
The SQL MIN MAX Functions. The SELECT MIN() function returns the minimum value for a column. The SELECT MAX() function returns the maximum value for a column.
SQL MIN MAX functions General syntax
The general MIN syntax is:
|
1 2 |
SELECT MIN(column-name) FROM table-name |
The general Max syntax is:
|
1 2 |
SELECT MAX(column-name) FROM table-name |
Question: Find the minimum product
|
1 2 |
SELECT MIN(UnitPrice) FROM Product |
| PRODUCT |
|---|
| Id |
| ProductName |
| ProductPrice |
| Package |
| SellerName |
Result:
| ProductPrice |
|---|
| 4.45 |
SQL SELECT MIN MAX functions
Question: Find the largest order placed in 2017
|
1 2 3 |
SELECT MAX(TotalAmount) FROM [Order] WHERE YEAR(OrderDate) = 2017 |
Result:
| TotalAmount |
|---|
| 56,67800 |
| Order |
|---|
| Id |
| CustomerId |
| OrderNumber |
| OrderDate |
| TotalAmount |
Problem: Find the last order date in 2017
|
1 2 3 |
SELECT MAX(OrderDate) FROM [Order] WHERE YEAR(OrderDate) = 2017 |
Its return maximum order date in the 2017 year.
Resource SQL