T
Tech Town
Log InSign Up Free
← Back to Blog

June 17, 2025 · admin

MIN Function

πŸ”½ SQL MIN() Function – How to Find the Smallest Value in a Column

The SQL MIN() function is one of the most used aggregate functions in SQL. It helps you retrieve the minimum (smallest) value in a specific column, whether it’s numeric, text, or date-based.

In this tutorial, you’ll learn:

  • βœ… What the MIN() function does
  • πŸ”§ Syntax and usage
  • πŸ“Š Real-life examples
  • ⚠️ Tips to avoid common mistakes

βœ… What is SQL MIN()?

The MIN() function returns the smallest value from a group of rows in a column. It’s perfect for identifying the lowest price, earliest date, or alphabetically first text.


πŸ”§ Syntax

SELECT MIN(column_name)
FROM table_name
WHERE condition;
  • column_name: The column you want to find the minimum value in.
  • table_name: The table containing the data.
  • WHERE: Optional condition to filter records.

πŸ“Š SQL MIN() Function Examples

πŸ§ͺ Example 1: Find the Lowest Salary

SELECT MIN(salary) AS lowest_salary
FROM employees;

πŸ’° This returns the minimum salary in the employees table.


πŸ§ͺ Example 2: Get the Earliest Hire Date

SELECT MIN(hire_date) AS first_hired
FROM employees;

πŸ“… Use this to find when the first employee joined.


πŸ§ͺ Example 3: Find Minimum Score per Student

SELECT student_id, MIN(score) AS lowest_score
FROM test_results
GROUP BY student_id;

πŸ“‰ Useful in analytics to find the worst performance per student.


πŸ§ͺ Example 4: Alphabetically First Name

SELECT MIN(last_name) AS first_name
FROM customers;

πŸ”€ Returns the name that comes first alphabetically.


🧠 MIN() with GROUP BY

Use GROUP BY to apply MIN() to each group in your dataset:

SELECT department_id, MIN(salary) AS min_salary
FROM employees
GROUP BY department_id;

This returns the lowest salary in each department.


🧠 Pro Tip: Use MIN() in Subqueries

SELECT *
FROM employees
WHERE salary = (
    SELECT MIN(salary)
    FROM employees
);

βœ”οΈ This retrieves all employees earning the minimum salary.


⚠️ Common SQL MIN() Mistakes

❌ Mistakeβœ… Fix
Using MIN() with multiple columns without GROUP BYUse GROUP BY with all non-aggregated columns
Expecting multiple valuesMIN() returns a single value per group
Including NULLsMIN() automatically ignores NULLs

πŸ” SQL MIN() Function Use Cases

  • 🏒 HR: Find the lowest salary
  • πŸ›’ Retail: Get the cheapest product
  • πŸ“… Events: Identify the earliest transaction
  • πŸ“ Exams: Report lowest marks per student

🏁 Summary

FeatureDescription
FunctionMIN()
ReturnsThe smallest value
Works withNumbers, text, dates
Ignores NULLsβœ… Yes
Use with GROUP BYβœ… Yes