SQL MIN function returns least or minimum value out of available records/values. Oracle MIN Function is opposite of the SQL MAX Function.
We Will be looking at:
SQL MIN Function Syntax
SELECT MIN(expression) FROM table_name WHERE conditions;
SQL MIN Function Examples
Let’s take an example for understanding the Oracle MIN Function
Suppose we have a table named “Employee” with the data as shown below.
Now we will see what the SQL MIN Function does when used in different scenarios
SQL MIN Function – Simple Usage
The simplest way to use SQL MIN Function is to fetch the minimum value from a column of a table
For example, with the below Oracle MIN Function query we will be able to fetch the minimum value of commission.
SELECT MIN(commission) as "MIN Commission" FROM employee;
The above MIN SQL Function query will return result as “10” as that’s the lowest commission available in the “Employee” table.
Also note that we have aliased the MIN(commission) field as “MIN Commission”, hence once the query is run “MIN Commission” will be displayed as the field name of the result from the Oracle MIN Function query.
NULL values are not considered by the MIN SQL Function.
SQL MIN Function – SQL WHERE Clause Example
SQL MIN Function can be used in SQL SELECT Statement having SQL WHERE Clause.
For example, below SQL MIN query returns minimum salary in ‘Sales’ department.
SELECT MIN(salary) as "Minimum Salary" FROM employee WHERE department = 'Sales';
The above Oracle MIN Function query returns ‘10000’ as minimum salary in ‘Sales’ department.
Also note that we have aliased the MIN(salary) field as “Minimum Salary”.
SQL MIN Function – SQL GROUP BY Clause Example
SQL MIN Function can be used in SQL SELECT Statement having SQL GROUP BY Clause.
For example, Oracle MIN Function query below, shows department name and minimum salary of each department.
SELECT department "Department Name" ,MIN(salary) "Minimum Salary" FROM employee GROUP BY department;
The data returned by above MIN SQL Function query will be:
|Departement Name||Minimum Salary|
Here notice that by using Oracle MIN Function with SQL GROUP BY Clause we have retrieved minimum salaries of every unique department along with the department name.