beginner

MIN & MAX Functions

4 min readLast updated: 2026-07-23

Overview

The MIN and MAX functions identify the smallest and largest values in a column or partition.

Learning Objectives

  • Find numerical limits using MIN() and MAX().
  • Compare dates and strings alphabetically.
  • Group min/max limits by category.

Detailed Concept Explanation

  • MIN(col): Returns the minimum value, ignoring NULLs.
  • MAX(col): Returns the maximum value, ignoring NULLs.

Non-Numeric Values

  • Dates: MIN returns the earliest date, while MAX returns the latest date.
  • Strings: MIN and MAX return the first and last alphabetical values based on collation rules.

Code Examples

SQL

sql
-- Find price extremes and date limits in a single scan
SELECT 
  MIN(price) AS cheapest_item,
  MAX(price) AS most_expensive_item,
  MIN(created_at) AS first_log_date,
  MAX(created_at) AS latest_log_date
FROM inventory;

Best Practices

  • Use Indexing: Ensure columns queried with MIN and MAX are indexed. Indexes store values in order, allowing the database to instantly retrieve boundary values without scanning the entire table.

Interview Perspective

Note

Interview Question: Can MIN and MAX be used on string columns?

Yes. When applied to strings, MIN returns the alphabetically first value (e.g. 'Apple') and MAX returns the alphabetically last value (e.g. 'Zebra') based on the database's collation settings.


Interactive Challenges

Challenge 1: Find Salary Range limits

Select the minimum salary (alias 'min_sal') and maximum salary (alias 'max_sal') from the 'employees' table.


Summary

MIN and MAX find boundary limits for numbers, dates, and strings. Index these columns to keep searches fast.