MAX and MIN Functions

tags: #sql/aggregate_functions

In SQL, the MAX and MIN functions are used to find the maximum and minimum values in a specific column of a table (or after a grouping).

// Maximum
SELECT MAX(column1) AS max_of_column1
FROM table_name;

// Minimum
SELECT MIN(column1) AS min_of_column1
FROM table_name;

This will return a single row with the highest value found in the given column, with the column alias specified by the user.

We can also use the MAX and MIN functions in combination with other clauses, such as GROUP BY, to find the maximum and minimum values for specific groups, example:

SELECT product_name, MAX(sale_amount) AS max_sale_amount
FROM sales
GROUP BY product_name;

This will return a table with two columns: "product_name" and "max_sale_amount", with each row representing the maximum sale amount for a specific product name.

Powered by Forestry.md