MIN(): Find the Smallest Value
The MIN() function returns the smallest value in a column.
It works with numbers, dates and text (text is compared in alphabetical order).
Syntax
SELECT MIN(column) FROM table_name WHERE condition;
Example 1: Minimum Age of All Employees
SELECT MIN(age) FROM employees;
Output:
min
-----
24
(1 row)
This returns the youngest age of any employee.
Example 2: Minimum Age per Department
SELECT department, MIN(age) AS youngest
FROM employees
GROUP BY department
ORDER BY department;
Output:
department | youngest
------------+----------
Finance | 27
HR | 24
IT | 24
Marketing | 29
Sales | 25
(5 rows)
This returns the youngest age in each department.
Example 3: First Department Alphabetically
SELECT MIN(department) FROM employees;
Output:
min
---------
Finance
(1 row)
For text, the smallest value is the one that comes first in alphabetical order.
Tip
MIN()ignoresNULLvalues.- It works with numbers, text and dates.
In the exercise, find the youngest age in one department.