SQL Server MIN() Function
In SQL Server, the MIN()
function is an aggregate function used to calculate the minimum value in a specified column or expression.
Syntax
The basic syntax for the MIN()
function is as follows:
MIN(expression)
Here, expression
is the column or expression from which the minimum value is to be calculated.
Use Cases
The MIN()
function is commonly used to calculate the minimum value in a specified column. For example, it can be used to find the minimum order amount, lowest sales amount, or minimum inventory level in a table.
Examples
Here are two examples of using the MIN()
function.
Example 1
Suppose there is a sales table as follows:
order_id | customer_id | order_total |
---|---|---|
1 | 1001 | 50 |
2 | 1002 | 75 |
3 | 1001 | 100 |
4 | 1003 | 125 |
The following SQL statement can be used to calculate the minimum order amount in the orders table:
SELECT MIN(order_total) AS min_order_total
FROM orders;
The result is:
min_order_total |
---|
50 |
Example 2
Suppose there is a products table as follows:
product_id | product_name | unit_price |
---|---|---|
1 | Product A | 10.5 |
2 | Product B | 15.2 |
3 | Product C | 12.8 |
4 | Product D | 9.9 |
The following SQL statement can be used to calculate the minimum unit price in the products table:
SELECT MIN(unit_price) AS min_unit_price
FROM products;
The result is:
min_unit_price |
---|
9.9 |
Conclusion
By using the MIN()
function, we can easily find the minimum value in a specified column for data analysis and decision-making.