MIN Function in SQLThe MIN is an aggregate function in SQL which returns the minimum or smallest value from the specified column of the table. Syntax of MIN FunctionIn the Structured Query Language, we use the MIN function with the columns of the table as shown in the following block: Example of MIN functionHere, we will create the new table through which we will perform the MIN function with the columns of the table: The following shows the syntax to create the new table in SQL: The following CREATE statement creates the Product_Details table for storing the price and quantity of products: The following multiple INSERT queries insert the records of products with their selling and purchasing price into the Product_Details table: The following SELECT statement displays the inserted records of the above Product_Details table:
Query 1: The following SELECT query uses the MIN function with the Product_Quantity column of the above Product_Details table: This query shows the smallest value of product quantity column. Output:
Query 2: The following SELECT query uses the MIN function with the Selling_Price column of the above Product_Details table: This query returns the minimum value of selling price products. Output:
Query 3: The following SELECT query uses the MIN function with the Product_Rating column of the above Product_Details table: This query returns the smallest values of Product_rating columns. Output:
Query 4: The following SELECT query uses the MIN function with the addition of two columns of the above Product_Details table: This query returns the smallest values from the addition of purchasing and selling price. Output:
MIN Function with WHERE clauseWe can also use the WHERE clause with the MIN function which returns the smallest value from the filtere rows. The syntax for using the MIN function with the WHERE clause is as follows: Query 1: The following SELECT query uses the MIN function with WHERE clause in the above Product_Details table: This query returns the smallest quantity from those products whose product id is greater than 200. Output:
Query 2: The following SELECT query uses the MIN function with WHERE clause in the above Product_Details table: This query returns the smallest purchasing price from those products which are release on 2022-01-28. Output:
MIN Function with GROUP BY clauseWe can also use the GROUP BY clause with the MIN function which returns the minimum value from the same group. The syntax for using the MIN function with the GROUP BY clause is as follows: Query 1: The following SELECT query uses the MIN function with the GROUP BY clause in the above Product_Details table: This query returns the minimum value of each specified group. Output:
Next TopicMOD Function in SQL |