Skip to main content

SQL MIN Function

SQL MIN Function

The MIN function in SQL is used to retrieve the minimum value from a set of values in a column of a table. It is an aggregate function that returns the smallest value in a dataset.


1. Overview

The MIN function allows you to find the smallest value in a specified column of a table. It is commonly used to retrieve the earliest or lowest value in a dataset.

Example:

// Example of using the MIN function
SELECT MIN(salary) AS min_salary
FROM employees;

This example retrieves the minimum salary from the employees table.


2. Syntax

The basic syntax of the MIN function is as follows:

SELECT MIN(column_name) AS min_value
FROM table_name
WHERE condition;

The MIN function operates on a specified column (column_name) and returns the smallest value as min_value. You can also include a WHERE clause to filter rows based on specific conditions.


3. Usage

To use the MIN function, specify the column containing the values you want to evaluate. The function then returns the smallest value found in that column.

Example:

// Example of using the MIN function with a WHERE clause
SELECT MIN(price) AS min_price
FROM products
WHERE category = 'Electronics';

This example retrieves the minimum price of electronics products from the products table.

The MIN function can be applied to various data types, including numeric, string, and date values.


4. Aggregate Functions

SQL provides several aggregate functions besides MIN, including MAX (maximum), SUM (total sum), AVG (average), and COUNT (row count). These functions offer powerful tools for data analysis and reporting.

Example:

// Example of using MAX function to retrieve maximum value
SELECT MAX(stock_quantity) AS max_stock
FROM inventory;

This example retrieves the maximum stock quantity from the inventory table.


5. Conclusion

The SQL MIN function is a useful tool for finding the smallest value in a dataset. Whether used alone or in combination with other aggregate functions and SQL clauses, it provides valuable insights into dataset analysis and reporting.

Comments