In MySQL, The "MIN()" and "MAX()" functions are aggregate functions used to find the minimum and maximum values, respectively, within a set of values.
These functions can be used in SELECT statements to find the minimum or maximum value of a specific column.
Min and Max Functions can be used within Subqueries to retrieve records corresponding to the minimum or maximum values.
# Syntax to Find MIN Value From Column SELECT MIN(column) AS min_column_value FROM tableName; # Syntax to Find MAX Value From Column SELECT MAX(column) AS max_column_value FROM tableName;
MIN(columnName): The Min function finds the smallest value from a specific column.
MAX(columnName): The Max function finds the largest value from a specific column.
tableName: The name of the table from which you want to retrieve data.
min_column_value, max_column_value: Aliases value of minimum and maximum value of respective columns.
The `MIN()` function returns the smallest value in a set.
SELECT MIN(price) AS min_price_product FROM products;
The above query retrieves the minimum `product_price` from the `products` table and aliases the result as `min_product_price`.
The MAX() function returns the largest value in a set.
SELECT MAX(price) AS max_price_product FROM products;
The above query retrieves the maximum `product_price` from the `products` table and aliases the result as `max_product_price`.
We can combine the "MIN()" and "MAX()" functions in a single query to find both the minimum and maximum values within a set.
SELECT MIN(price) AS min_price_product, MAX(price) AS max_price_product FROM products;
The above query retrieves the smallest and largest `product_price` from the `products` table.
SELECT product_name, product_price FROM products WHERE product_price = (SELECT MIN(product_price) FROM products) OR product_price = (SELECT MAX(product_price) FROM products);
The subqueries (SELECT MIN(product_price) FROM products) and (SELECT MAX(product_price) FROM products) find the smallest and largest prices in the `products` table, respectively.
The outer query selects the `product_name` and `product_price` where the `product_price` matches either the minimum or maximum price.