PostgreSQL – MIN Function
In PostgreSQL, the MIN
function is used to find the minimum value of a given set of expressions. The syntax for the MIN
function is as shown below:
SELECT MIN(expression) FROM table_name;
where, expression
represents the column name or expression that you want to find the minimum value for, and table_name
represents the name of the table that contains the data.
For example, consider a table named products
with columns product_name
and price
. To find the minimum price of all products, we can use the MIN
function as follows:
SELECT MIN(price) FROM products;
Above query will return the minimum value of the price
column from the products
table.
The MIN
function can be used with different data types such as integer, decimal, date, and string.
The MIN
function can be used with the GROUP BY
clause to find the minimum value for each group of data. For example, if we have a table with columns product_name
, category
, and price
, and we want to find the minimum price for each category, we can use the following query:
SELECT category, MIN(price) FROM products GROUP BY category;
The MIN
function can be used with multiple expressions to find the minimum value for multiple columns. For example, consider a table with columns name
, age
, and height
. To find the minimum age and minimum height, we can use the following query:
SELECT MIN(age), MIN(height) FROM people;