The SQL MIN function is used to retrieve the smallest value from a column in a database table. It is a built-in function in Structured Query Language (SQL) that can be used in SELECT statements to return the minimum value of a specified column.
Syntax
The syntax of the MIN function is as follows:
SELECT MIN(column_name) FROM table_name;
In this syntax, column_name refers to the name of the column from which the minimum value needs to be retrieved, and table_name refers to the name of the table that contains the column.
Example
For example, consider a table named Training_Course with columns id, name, duration, and price.
Training_Course
ID | NAME | DURATION | PRICE |
---|---|---|---|
1 | SQL | 5 | 200 |
2 | T-SQL | 7 | 700 |
3 | MySQL | 5 | 600 |
4 | PL/SQL | 7 | 800 |
5 | PostgreSQL | 6 | 500 |
To retrieve the minimum price of all courses in the table, the following SQL query can be used:
select MIN(PRICE) from Training_Course;
Results: 200
It is also possible to use the MIN function with the GROUP BY clause to retrieve the minimum value of a column for each group of data in the table. For example, consider the following SQL query:
SELECT department, MIN(salary) FROM employees GROUP BY department;
This query will return the minimum salary value for each department in the employees table.
In summary, the SQL MIN function is a useful tool for retrieving the minimum value of a column in a database table. It can be used in conjunction with other SQL functions and clauses to perform more complex queries and retrieve specific data from a database.