• Home
  • SQL MIN() and MAX() Functions

    The SQL MIN() and MAX() functions are aggregate functions that return the minimum and maximum values in a set of values, respectively. They are very popular and can be used in a variety of ways.

    The syntax for the MIN() and MAX() functions is as follows:

    SQL
    SELECT MIN(column_name) FROM table_name;
    SELECT MAX(column_name) FROM table_name;

    For example, the following query would return the minimum and maximum values in the Price column of the Products table:

    SQL
    SELECT MIN(Price), MAX(Price) FROM Products;

    The MIN() and MAX() functions can also be used with WHERE clauses to return the minimum or maximum value of a set of values that meet certain criteria. For example, the following query would return the minimum and maximum prices of products that are in stock:

    SQL
    SELECT MIN(Price), MAX(Price) FROM Products WHERE InStock = 1;

    The MIN() and MAX() functions can also be used with window functions to return the minimum or maximum value of a set of values within a window. For example, the following query would return the minimum and maximum prices of products within each category:

    SQL
    SELECT Category, MIN(Price), MAX(Price)
    FROM Products
    WINDOW w AS (PARTITION BY Category);

    The MIN() and MAX() functions are powerful tools that can be used to find the minimum and maximum values in a set of values. They can be used with a variety of clauses and window functions to return the minimum or maximum value of a set of values that meet certain criteria.


    Here are some additional things to keep in mind about the MIN() and MAX() functions:

    They are aggregate functions, which means that they can only be used with GROUP BY or HAVING clauses.
    They ignore NULL values.
    They can be used with any data type that can be compared, such as numeric, date, and datetime data types.



    About the Author



    Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

    We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





     PreviousNext