The GREATEST function compares multiple expressions and returns the largest value, subject to database-specific NULL handling rules.
Comparing several values in the same row is common in business analysis, especially when identifying the highest metric.
Find the highest sales amount among three sales channels.
SELECT
CustomerID,
GREATEST(OnlineSales, StoreSales, MobileSales) AS HighestSales
FROM CustomerSales;
GREATEST can be useful for maximum-value analysis, risk metrics, pricing comparisons, and row-level calculations.
- GREATEST compares multiple expressions.
- It returns the largest value.
- It performs row-level comparison.
- NULL behavior varies by database.
- It is useful for business comparisons.
What does GREATEST return?
GREATEST returns the largest value among the supplied expressions.
Is GREATEST the same as MAX?
No. GREATEST compares expressions within a row, while MAX is normally an aggregate function used across multiple rows.