MYSQL Tutorial



MySQL MIN() & MAX()


MySQL MIN() & MAX() Functions

The MIN() and MAX() functions in MySQL let you find the smallest and largest values in a numeric column (or the earliest/latest in a date column). They’re perfect for quickly identifying extremes in your data.

Syntax

MIN(column_name)
MAX(column_name)
  

Replace column_name with your target column (numeric or date). NULLs are ignored.

Example Queries for MIN()

-- Smallest salary in the company
SELECT MIN(salary) AS lowest_salary
FROM employees;

-- Earliest hire date
SELECT MIN(hire_date) AS first_hired
FROM employees;

-- Minimum sale amount in 2024
SELECT MIN(sales_amount) AS min_sale
FROM orders
WHERE YEAR(order_date) = 2024;
  

Example Queries for MAX()

-- Highest salary in the company
SELECT MAX(salary) AS highest_salary
FROM employees;

-- Most recent hire date
SELECT MAX(hire_date) AS last_hired
FROM employees;

-- Maximum sale amount in 2024
SELECT MAX(sales_amount) AS max_sale
FROM orders
WHERE YEAR(order_date) = 2024;
  

Using with GROUP BY

-- Lowest and highest salary per department
SELECT
  department,
  MIN(salary) AS dept_min_salary,
  MAX(salary) AS dept_max_salary
FROM employees
GROUP BY department;
  

This query shows the smallest and largest salaries in each department—great for spotting pay ranges.

Key Points

  • MIN() returns the minimum (smallest) value; MAX() returns the maximum (largest).
  • NULL values are ignored in both functions.
  • Works on numeric and date/time columns (e.g., find earliest/latest dates).
  • Combine with WHERE to filter before computing extremes.
  • Use GROUP BY to get min/max per category or group.

🌟 Enjoyed Learning with Us?

Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!

Leave a Google Review