MIN

MIN is an SQL aggregate function that returns the smallest value in a selected column.

MIN(expression)

  • expression: Refers to the column or set of columns in the table. The `MIN()` function will return the smallest value found in this specified column or set of columns.

Example

SELECT MIN(salary) FROM employee;

Output

85000

Explanation

This query is used to find the minimum value in the ‘salary’ column of the ‘employee’ table.

MIN(expression) OVER ([PARTITION BY partition_expression] [ORDER BY sort_expression [ASC | DESC]] [ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW])

  • expression: Refers to any valid expression that represents the column from which the minimum value has to be found.
  • partition by partition_expression: This is an optional clause that separates the data into different partitions, as indicated by partition_expression. This is useful for analyzing sections of data.
  • order by sort_expression [asc | desc]: This optional clause dictates the order in which the data is sorted before the minimum value is found. The sort_expression determines the column by which the data is sorted. ASC or DESC indicates ascending or descending order respectively.
  • rows between unbounded preceding and current row: This optional clause defines a sliding window of data to operate on. “UNBOUNDED PRECEDING” indicates that the window starts at the first row of the partition, and “CURRENT ROW” means the window ends at the current row. The minimum value will then be calculated from this defined section of data.

Example

SELECT MIN(unit_price) FROM products;

Output

min
------
1.23

Explanation

In the given example, the MIN() function is used to find the lowest unit price amongst all products within the ‘products’ table. The value ‘1.23’ is the minimum unit price found.

MIN( [ ALL | DISTINCT ] expression ) OVER ( [ partition_by_clause ] order_by_clause rows_range_clause )

  • all: This keyword is used when you want to calculate the minimum value from all possible values within the specified expression.
  • distinct: The DISTINCT keyword is used when you want to calculate the minimum value from distinct values within the specified expression.
  • expression: The expression parameter is used to specify the column or set of columns upon which the MIN function would be applied.
  • over: The OVER keyword is used to signify that we are about to describe the windowing clause, included within the parentheses after the OVER keyword.
  • partition_by_clause: This clause is used to divide the query result set into partitions. The function is applied to each partition separately and computation is reset for each partition.
  • order_by_clause: The order_by_clause parameter allows you to specify the column or columns used to order the result set.
  • rows_range_clause: By using the rows_range_clause, one specifies the start and end points within the partition for the window frame where the function will calculate the result.
  • min: This SQL function returns the smallest value of the selected column.

Example

SELECT MIN(Salary) AS MinimumSalary
FROM Employees;

Output

MinimumSalary
15000

Explanation

The MIN() function returns the smallest value of the selected column, in this case, it returns the lowest salary from the Employees table.

MIN(expression)

  • expression: It refers to the column or set of values on which the MIN function is going to be computed. This can refer to a column name, the result of another function, or a mathematical formula.

Example

SELECT MIN(salary) FROM employees;

Output

| MIN(SALARY) |
|-------------|
| 2000 |

Explanation

This SQL statement returns the minimum value of the “salary” column from the “employees” table. The MIN function in SQL returns the smallest value of the selected column.

MIN(expression)

  • expression: The portion of the SQL statement that specifies the column of a table or query which the function will operate on. The MIN function takes this expression parameter, and returns the smallest value in the set of values in the specified column.

Example

SELECT MIN(salary) FROM employees;

Output

35000

Explanation

The above SQL query retrieves the minimum value from the ‘salary’ column of the ‘employees’ table. In this case, the lowest salary is 35000.

For in-depth explanations and examples SQL keywords where you write your SQL, install our extension.