๐Ÿ”ข SQL Aggregate Functions
Estimated reading: 3 minutes 398 views

SQL MIN and MAX โ€“ Find Minimum and Maximum Values

Introduction โ€“ What are MIN and MAX in SQL?

The MIN() and MAX() functions in SQL are used to retrieve the lowest and highest values in a column. These are powerful tools when analyzing data trends, identifying outliers, or setting boundaries for queries and reports.

In this guide, youโ€™ll learn how to:

  • Use MIN() and MAX() on numeric, date, and text columns
  • Combine with GROUP BY and HAVING
  • Handle NULL values appropriately

1. Basic Syntax for MIN and MAX

SELECT MIN(column_name), MAX(column_name)
FROM table_name;

Retrieves the smallest and largest values from the specified column.


2. Example โ€“ Salary Range

SELECT MIN(salary) AS lowest_salary,
       MAX(salary) AS highest_salary
FROM employees;

Shows the salary boundaries in the employees table.


3. MIN and MAX with Dates

SELECT MIN(hire_date) AS first_hired,
       MAX(hire_date) AS most_recent_hired
FROM employees;

Identifies earliest and latest hiring dates.


4. Use with GROUP BY

SELECT department,
       MIN(salary) AS min_salary,
       MAX(salary) AS max_salary
FROM employees
GROUP BY department;

Retrieves min and max salary per department.


5. Combine with HAVING

SELECT department, MAX(salary) AS top_salary
FROM employees
GROUP BY department
HAVING MAX(salary) > 100000;

Filters groups where top salaries exceed 100,000.


6. MIN/MAX with Text Columns

SELECT MIN(last_name), MAX(last_name)
FROM employees;

Alphabetically finds the first and last names in the dataset.


Best Practices

Do This Avoid This
Alias results for clarityLeaving raw column names
Pair with GROUP BY for segmentationUsing without context or sorting
Handle NULLs properly (they’re ignored)Assuming NULL = 0 or ”

Summary โ€“ Recap & Next Steps

The MIN() and MAX() functions are essential for identifying data boundaries, including numeric ranges, date windows, and alphabetical limits.

Key Takeaways:

  • Use MIN() to find the lowest value; MAX() for the highest
  • Works with numbers, dates, and strings
  • Combine with GROUP BY and HAVING for grouped insights
  • Ignores NULLs unless handled using COALESCE()

Real-World Relevance:
Used in pricing analytics, trend reports, HR records, audit trails, and data integrity checks.

Next: Explore COUNT() and conditional aggregation techniques.


FAQ โ€“ SQL MIN and MAX Functions

Do MIN and MAX include NULL values?

No. They ignore NULLs by default.

Can MIN and MAX be used on text columns?

Yes. They return the alphabetically first and last values.

How do I get min/max per category?

Use GROUP BY and select the column you want to group by.

Can I use them in subqueries?

Absolutely. Example:

SELECT name FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);

Are MIN and MAX efficient?

Yes, especially when used on indexed columns.


Share Now :
Share

๐Ÿ“ˆ SQL MIN and MAX

Or Copy Link

CONTENTS
Scroll to Top