MySQL Select minimum/maximum among two or more given values
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
In MySQL, selecting min or max among multiple values can mean two different tasks: compare expressions in one row, or aggregate values across many rows. Choosing the correct function is important for both correctness and query performance. The common tools are LEAST, GREATEST, MIN, and MAX.
Row-Level Comparison with LEAST and GREATEST
Use LEAST and GREATEST when values are in the same row, such as choosing min or max among several columns.
This runs per row. It does not scan across rows in the table for global extremes.
Be mindful of data types. MySQL may coerce values during comparison, so ensure compared expressions are type-compatible. If needed, cast explicitly to avoid lexical comparison surprises.
Table-Level Aggregation with MIN and MAX
Use MIN and MAX for aggregate queries over many rows.
This is a different problem from row-level expression comparison. It computes per-group or global extremes depending on whether you use GROUP BY.
Combining Column Comparison and Aggregation
Sometimes you need both steps: derive a per-row candidate first, then aggregate that derived value across rows.
The inner LEAST and GREATEST are row-level. The outer MIN and MAX are aggregate-level. Keeping this distinction clear reduces logic bugs.
Null Handling and Deterministic Behavior
If any argument in LEAST or GREATEST is NULL, result behavior may become NULL depending on expression composition. Use COALESCE when you need deterministic defaults.
For aggregates, MIN and MAX ignore NULL values by default, which is often desired.
Performance Considerations
Expression-heavy row-level comparisons can be CPU-bound on large scans. If queries are frequent and logic is stable, consider generated columns or materialized summary tables to reduce repeated computation.
Also verify indexes for aggregate queries grouped by key columns. Index support for GROUP BY and aggregate patterns can significantly reduce latency.
Query Design Checklist
When designing min and max logic, start by writing down whether your comparison scope is within one row or across many rows. Then decide null policy explicitly and encode it with COALESCE or filtered predicates. Next, inspect execution plans to verify expected index usage, especially for grouped aggregates on large tables. If the same expression appears in many reports, consider a generated column and index that generated result. This can reduce repeated CPU cost in high-traffic analytics workloads. Finally, document the comparison semantics in query comments so future maintainers do not replace a row-level LEAST with an aggregate MIN by mistake. These small design habits prevent silent logic drift in reporting systems.
Verification Checklist
Create fixture rows that include nulls, ties, and mixed-value patterns. Validate expected outputs in both row-level and aggregate queries. This guards against future query edits that accidentally change comparison scope or null behavior.
Common Pitfalls
- Using
MINwhen you meant row-level comparison among columns. - Forgetting null behavior in
LEASTandGREATESTexpressions. - Comparing mixed string and numeric types without explicit casts.
- Building complex expression logic without verifying execution plans.
Summary
- Use
LEASTandGREATESTfor per-row multi-value comparisons. - Use
MINandMAXfor aggregation across rows. - Combine both levels carefully when needed.
- Handle nulls explicitly with
COALESCEif required. - Validate type coercion and query plans for correctness and speed.

