๐Ÿ”ข SQL Aggregate Functions
Estimated reading: 3 minutes 24 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 :

Leave a Reply

Your email address will not be published. Required fields are marked *

Share

๐Ÿ“ˆ SQL MIN and MAX

Or Copy Link

CONTENTS
Scroll to Top