The GREATEST function compares multiple expressions and returns the largest value according to the database system's comparison rules.
Finding the highest value across several columns is useful when comparing multiple business measurements for the same record.
Find the highest sales amount across three sales channels.
SELECT
CustomerID,
GREATEST(OnlineSales, StoreSales, PhoneSales) AS HighestChannelSales
FROM CustomerSales;
GREATEST can be used for performance comparisons, pricing analysis, risk evaluation, and multi-channel business analysis.
- GREATEST returns the largest value.
- It compares multiple expressions.
- It is useful for row-level comparisons.
- NULL behavior can vary by database.
- Database support should be checked.
What does GREATEST do?
GREATEST returns the largest value among the supplied expressions.
How is GREATEST different from MAX?
GREATEST typically compares values across columns or expressions within a row, while MAX commonly aggregates values across multiple rows.