MySQL Select minimum/maximum among two (or more) given values
In MySQL, you can use the and functions to determine the minimum or maximum value among two or more given values.Example 1: Using the FunctionThe function returns the smallest value among all specified parameters. For instance, to identify the smallest value among several numbers, you can use it as follows:This SQL statement returns 7 because 7 is the smallest value among 34, 7, 23, 99, and 15.Example 2: Using the FunctionThe function, which serves as the counterpart to , returns the largest value among all specified parameters. For example, to find the largest value among several numbers, you can use it as follows:This SQL statement returns 99 because 99 is the largest value among 34, 7, 23, 99, and 15.Practical ApplicationSuppose you have an table with columns and . To calculate the actual minimum payment amount for customers, you can use the function to compare the original price and the discounted price:This query returns the actual minimum payment amount for each order by comparing the values of and .With these functions, MySQL provides a straightforward and efficient method for handling comparisons among multiple values, enabling quick results for both minimum and maximum values. This approach is highly valuable in data analysis and routine database operations.