SQL MIN Function | Return Smallest Value in Column

SQL MIN is an inbuilt function in SQL Server. It returns the smallest value of a column in a table. It finds the record which has the minimum value in a recordset. In this article, we will discuss the SQL MIN Function. Also, we will discuss an example of using it.

Syntax

SELECT MIN(column_name) FROM table_name WHERE condition;

  • column_name: The column from which to find the minimum value.
  • table_name: The selected table to find from.
  • condition: You can also optionally specify a WHERE condition to specify rows.

Demo Table

For instance, consider the demo table to refer to all the queries in this article.

mysql> SELECT * FROM employees;
+----+--------+--------+------------+-------+
| id | name   | salary | experience | team  |
+----+--------+--------+------------+-------+
|  1 | David  |  20000 |          5 | Alpha |
|  2 | Monica |  25000 |          6 | Beta  |
|  3 | John   |  15000 |          3 | Beta  |
|  4 | Mary   |  18000 |          4 | Gamma |
|  5 | Ross   |  30000 |          8 | Alpha |
+----+--------+--------+------------+-------+

Examples

Let’s consider a few examples of using the MIN Function in SQL.

Example 1: Finding the Minimum Column Value

For instance, if we need to find the minimum salary from our table, we can use the following query:

SELECT MIN(salary) FROM employees;

The above query returns the minimum salary from the table. The output of the query is:

mysql> select MIN(salary) from employees;
+-------------+
| MIN(salary) |
+-------------+
|       15000 |
+-------------+

Example 2: Specifying a Where Condition

Also, you can specify a WHERE Condition in the query. The function will return the minimum value of the selected column from the rows matching the where condition. For instance, consider the following example:

mysql> SELECT MIN(experience) FROM employees WHERE team = 'Alpha';
+-----------------+
| MIN(experience) |
+-----------------+
|               5 |
+-----------------+

Although, the minimum experience in the table is 3, but the minimum experience from the rows with team Alpha is 5.

Conclusion

In conclusion, we discussed the SQL MIN Function. You can read more about it on the Official Microsoft Documentation. Also, you can read more about SQL on Concatly.

Spread the Knowledge

Leave a Reply

Your email address will not be published. Required fields are marked *