Back to cookbooks list Articles Cookbook

How to Find the Minimum Value of a Column in SQL

  • MIN

Problem:

You’d like to find the smallest numeric value in a column.

Example:

Our database has a table named employment with data in the following columns: id, first_name, last_name, department, and salary.

idfirst_namelast_namedepartmentsalary
1EllieMartinesmarketing1200
2MartinJohnsonfinance2300
3MichaelJacobsproduction1100
4StephenKowalskimarketing4300
5StanleyMillermarketing3500
6JenyBrownfinance5000
7MargaretGreenmarketing1500
8LisaThomasproduction2800

Let’s find the lowest salary among all employees.

Solution:

SELECT MIN(salary) as min_salary
FROM employment;

Here’s the result:

min_salary
1100

Discussion:

To find the minimum value of a column, use the MIN() aggregate function; it takes as its argument the name of the column for which you want to find the minimum value. If you have not specified any other columns in the SELECT clause, the minimum will be calculated for all records in the table. In our example, the query returns the minimum salary among all employees.

Of course, since it’s an aggregate function, MIN() can also be used with groups. For example, if we’d like to find the minimum salary for each department, we can write this query:

SELECT department, MIN(salary) as min_salary
FROM employment
GROUP BY department;

This query returns the minimum salary for each department:

departmentmin_salary
marketing1200
finance2300
production1100

Recommended courses:

Recommended articles:

See also: