LEAST Function in SQLThe LEAST is a SQL numeric function which shows the least value from the specified inputs in Structured Query Language. Syntax of LEAST FunctionIn the LEAST syntax, we have to pass those numbers from which we want to find least value. In the Structured Query Language, we can also use the LEAST function with the column of the table as shown in the following block: In this syntax, we have to define the name and columns of that table on which we want to perform the LEAST function. Examples of LEAST functionExample 1: This example returns the least from the specified numbers: Output:
Example 2: This example returns the least from the specified numbers: Output:
Example 3: This example returns the least value from the given inputs: Output:
Example 4: This example returns the least representation of 255: Output:
Example 5: This example returns the least value: Output:
Example 6: This example uses the LEAST function with the SQL table. In this example, we will create the new table through which we will perform the LEAST function on 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 LEAST function with the Product_Quantity and Product_ID column of the above Product_Details table: This query shows the least value from the product id and quantity of each product. Output:
Query 2: The following SELECT query uses the LEAST function with Selling_Price and Purchasing_Price column of the above Product_Details table: This query shows the least value from purchasing and selling price of each product. Output:
Query 3: The following SELECT query uses the LEAST function with the Purchasing_Price and Product_ID column of the above Product_Details table: This query shows the least value from the product id and purchasing price of each product. Output:
LEAST Function with WHERE ClauseIn the Structured Query Language, we can also use the LEAST function with the WHERE clause in the SELECT statement. Syntax of LEAST Function with WHERE ClauseIn this syntax, we have to sepcify the condition in the WHERE clause for performing least function on the filtered rows. Example of LEAST Function with WHERE ClauseLet's take the above Product_Details table to understand the where clause with least function. Query: The following SELECT query uses the WHERE Clause with LEAST function on the Product_Quantity and Product_ID columns of the above Product_Details table: This query shows the least value from product id and quantity of those products whose quantity is greater than 10.250. Output:
LEAST Function with SUM functionIn the Structured Query Language, we can also use the LEAST function with the SUM SQL function in the SELECT statement. Syntax of LEAST Function with WHERE ClauseExample of LEAST Function with WHERE ClauseLet's take the above Product_Details table to understand the sum function with the least function. Query: The following SELECT query uses the SUM function with LEAST function on the Product_Quantity and Product_ID columns of the above Product_Details table: This query shows the least value from sum of product id and quantity. Output:
Next TopicLN Function in SQL |