MySQL MIN() and MAX() Functions

MySQL Tutorial

MySQL MIN() and MAX() Functions

In MySQL, the MIN() and MAX() functions are aggregate functions used to find the smallest and largest values in a column.

  •  These functions are very common in reports, analytics, exams, and interviews.

 What are MIN() and MAX()?

  • MIN() → returns the lowest value

  • MAX() → returns the highest value

  • Work on numeric, date, and string columns

  • Return one value per group

 They ignore NULL values.

 Basic Syntax

 Simple Examples

Find Minimum Marks

Find Maximum Salary

 MIN() & MAX() with GROUP BY

Department-wise Min & Max Salary

  • One row per department

 With WHERE

  •  Filters rows before aggregation.

 With Dates

  •  Useful for first/last activity

 MIN() & MAX() with Strings

  •  Based on alphabetical order.

 MIN() / MAX() with HAVING

  •  Departments with high-paid employees

 Aggregate Functions & NULL Values

FunctionNULL Handling
MIN()Ignores NULL
MAX()Ignores NULL

 Common Mistakes

  •  Expecting MIN/MAX to include NULL
  •  Using aggregate in WHERE
  •  Forgetting GROUP BY
  •  Confusing numeric vs string comparison

Interview Questions & MCQs (Very Important)

Q1. What does MIN() return?

A) Maximum value
B) Average value
C) Minimum value
D) Total value

Answer: C

Q2. What does MAX() return?

A) Minimum value
B) Maximum value
C) Count
D) Sum

Answer: B

Q3. Do MIN() and MAX() include NULL values?

A) Yes
B) No

Answer: B

Q4. Can MIN() and MAX() work with dates?

A) Yes
B) No

Answer: A

Q5. Which clause filters groups after aggregation?

A) WHERE
B) GROUP BY
C) HAVING
D) ORDER BY

Answer: C

Q6. Which is correct?

A) Correct
B) Incorrect

Answer: A

Real-Life Use Cases

  •  Highest & lowest salary
  •  First & last login date
  •  Top & bottom scores
  •  Sales range analysis
  •  Performance reports

 Summary

  • MIN() finds smallest value

  • MAX() finds largest value

  • Work with numbers, dates, strings

  • Ignore NULL values

  • Used with GROUP BY & HAVING

  • Important for SQL exams & interviews

You may also like...