🎉 75% of content is free forever — Unlock Premium from $10/mo →
CW
NEWSLIVESearch All Content
đŸ’ŧ Servicesâ„šī¸ Aboutâœ‰ī¸ ContactView Pricing Plansfrom $10

MIN and MAX

SQL AggregationMIN and MAXđŸŸĸ Free Lesson
SQL Aggregation

MIN and MAX

Find the smallest and largest values in your data.

  • MIN — the minimum value in a set
  • MAX — the maximum value in a set Every dataset has boundaries. These functions find them.

MIN Function

-- Smallest salary in the company
SELECT MIN(salary) AS lowest_salary
FROM employees;
-- Earliest hire date
SELECT MIN(hire_date) AS first_hire
FROM employees;
-- Alphabetically first name
SELECT MIN(first_name) AS first_name_alphabetically
FROM employees;

MAX Function

-- Highest salary in the company
SELECT MAX(salary) AS highest_salary
FROM employees;
-- Most recent hire date
SELECT MAX(hire_date) AS latest_hire
FROM employees;
-- Last name alphabetically
SELECT MAX(last_name) AS last_name_alphabetically
FROM employees;

MIN and MAX with GROUP BY

-- Salary range per department
SELECT department,
       MIN(salary) AS min_salary,
       MAX(salary) AS max_salary,
       MAX(salary) - MIN(salary) AS salary_range
FROM employees
GROUP BY department
ORDER BY salary_range DESC;
-- Earliest and latest hire dates per department
SELECT department,
       MIN(hire_date) AS first_hire,
       MAX(hire_date) AS last_hire
FROM employees
GROUP BY department;

Finding the Row with MAX Value

-- Get the employee with the highest salary (single row)
SELECT *
FROM employees
WHERE salary = (SELECT MAX(salary) FROM employees);
-- Get top N highest salaries
SELECT *
FROM employees
ORDER BY salary DESC
LIMIT 5;
-- Get employees with maximum salary per department
SELECT e.*
FROM employees e
WHERE salary = (
    SELECT MAX(salary)
    FROM employees
    WHERE department = e.department
);

MIN and MAX with Dates

-- Date range analysis
SELECT MIN(order_date) AS first_order,
       MAX(order_date) AS last_order,
       DATEDIFF(DAY, MIN(order_date), MAX(order_date)) AS days_span
FROM orders;
-- Orders per month with date boundaries
SELECT YEAR(order_date) AS order_year,
       MONTH(order_date) AS order_month,
       MIN(order_date) AS month_start,
       MAX(order_date) AS month_end,
       COUNT(*) AS order_count
FROM orders
GROUP BY YEAR(order_date), MONTH(order_date)
ORDER BY order_year, order_month;

MIN and MAX with Strings

-- String ordering uses lexicographic comparison
SELECT MIN(product_name) AS first_product,
       MAX(product_name) AS last_product
FROM products;
-- Find products alphabetically within each category
SELECT category,
       MIN(product_name) AS first_product,
       MAX(product_name) AS last_product
FROM products
GROUP BY category;

MIN and MAX with CASE

-- Conditional MIN/MAX
SELECT department,
       MIN(CASE WHEN gender = 'M' THEN salary END) AS min_male_salary,
       MAX(CASE WHEN gender = 'M' THEN salary END) AS max_male_salary,
       MIN(CASE WHEN gender = 'F' THEN salary END) AS min_female_salary,
       MAX(CASE WHEN gender = 'F' THEN salary END) AS max_female_salary
FROM employees
GROUP BY department;

Window Functions with MIN/MAX

-- Compare each employee's salary to department min/max
SELECT employee_name, department, salary,
       MIN(salary) OVER (PARTITION BY department) AS dept_min,
       MAX(salary) OVER (PARTITION BY department) AS dept_max,
       ROUND((salary - MIN(salary) OVER (PARTITION BY department)) * 100.0 /
             NULLIF(MAX(salary) OVER (PARTITION BY department) - MIN(salary) OVER (PARTITION BY department), 0), 1) AS pct_in_range
FROM employees;
-- Running min and max
SELECT order_date, amount,
       MIN(amount) OVER (ORDER BY order_date) AS running_min,
       MAX(amount) OVER (ORDER BY order_date) AS running_max
FROM orders;

Common MIN/MAX Patterns

-- Find duplicate values (min = max in a group)
SELECT department, job_title, COUNT(*) AS cnt
FROM employees
GROUP BY department, job_title
HAVING MIN(salary) = MAX(salary);
-- Detect outliers (values outside 2x the average)
SELECT *
FROM employees
WHERE salary > 2 * (SELECT AVG(salary) FROM employees);
-- Find the most recent record per group
SELECT e.*
FROM employees e
WHERE hire_date = (
    SELECT MAX(hire_date)
    FROM employees
    WHERE department = e.department
);

NULL Handling

-- MIN and MAX ignore NULLs
SELECT MIN(salary) AS min_salary,  -- Ignores NULLs
       MAX(salary) AS max_salary   -- Ignores NULLs
FROM employees;

-- If all values are NULL, result is NULL
SELECT MIN(NULL) AS result;  -- Returns NULL

Performance Tips

OperationMethod
Global min/maxSELECT MIN/MAX directly
Per-group min/maxUse GROUP BY
Row with max valueSubquery or ORDER BY LIMIT
Range calculationMAX - MIN
Percentile positionWindow functions
Outlier detectionCompare to MIN/MAX or AVG

Extended Learning: Beyond the Basics

Key Concepts Summary

This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.

Prerequisite Knowledge

Before studying this topic in depth, ensure you understand:

  • Basic definitions and terminology
  • Core formulas and their derivations
  • Common applications in real-world scenarios
  • Typical pitfalls and misconceptions

Step-by-Step Walkthrough

Let us work through a complete example to solidify understanding:

Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.

Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.

Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.

Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.

Common Mistakes to Avoid

  1. Misidentifying the problem type — Always start by categorizing the problem
  2. Incorrect formula application — Verify the formula applies to your specific case
  3. Unit errors — Always check units are consistent throughout
  4. Sign errors — Pay careful attention to positive and negative values
  5. Rounding too early — Keep full precision until the final answer
  6. Forgetting constraints — Check that solutions satisfy all given conditions

Real-World Applications

Understanding this concept is essential for:

  • Advanced problem solving in examinations
  • Building intuition for more complex topics
  • Developing analytical thinking skills
  • Applying mathematical reasoning to practical situations

Practice Exercises

  1. Basic: Solve 5 problems of increasing difficulty using the core concept.
  2. Intermediate: Apply the concept to word problems with multiple steps.
  3. Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
  4. Challenge: Create your own problem that requires this concept plus additional knowledge.

Interview and Exam Tips

  • Common question patterns: Identify which exam or interview formats test this topic
  • Time management: Practice completing problems within time constraints
  • Common traps: Know the typical mistakes examiners set as distractors
  • Verification strategy: Develop a quick method to check your answer

Related Topics

Quick Reference Card

ConceptFormula/RuleKey InsightCommon Use
Core DefinitionSee main textUnderstand the intuitionFoundation for all applications
Key FormulaSee main textMemorize the derivationDirect problem solving
Special CaseSee main textRecognize when it appliesSimplifies complex problems
Edge CaseSee main textKnow the limitationsAvoids errors

Further Reading

For students who want to go deeper:

  • Standard textbooks on the subject
  • Research papers applying this concept
  • Online courses with video explanations
  • Practice problem collections with detailed solutions
CW

ChatWhole Learn Editorial Team

Written by experienced data scientists, software engineers, and educators. All content is reviewed by industry professionals to ensure accuracy and relevance.

Last reviewed: 2026-11-14

—
☆☆☆☆☆
0 ratings

Rate & Feedback

Need Expert SQL Help?

Get personalized tutoring, project support, or professional consulting.

Advertisement