LEAST Function in SQL

The LEAST is a SQL numeric function which shows the least value from the specified inputs in Structured Query Language.

Syntax of LEAST Function

In 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 function

Example 1: This example returns the least from the specified numbers:

Output:

least_of_numbers
1

Example 2: This example returns the least from the specified numbers:

Output:

least_of_numbers
22

Example 3: This example returns the least value from the given inputs:

Output:

least_of_numbers
3

Example 4: This example returns the least representation of 255:

Output:

least_of_numbers
3.08

Example 5: This example returns the least value:

Output:

least_of_numbers
Shows Error

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:


Product_IDProduct_NameProduct_QuantityPurchasing_PriceSelling_PriceRelease_DateProduct_Rating
104P110945NULL2022-04-30NULL
202P41545752022-01-285
103P21825NULL2022-02-184
111P7255152021-12-259
210P61550702021-10-15NULL
212P8191102502022-01-284
112P10105508352022-04-11NULL

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:

Product_IDProduct_Quantityleast_of_Product_ID_quantity
10410.25010.250
20215.50015.500
10318.25018.250
11125.25025.250
21015.50015.500
21219.75019.750
11210.25010.250

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:

Selling_PricePurchasing_Priceleast_of_Selling_PurchasingPrice
NULL945-
754545
NULL25-
1555
705050
250110110
835550550

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:

Product_IDPurchasing_Priceleast_of_Product_ID_and_Purchasing
104945104
2024545
1032525
11155
2105050
212110110
112550112

LEAST Function with WHERE Clause

In 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 Clause

In 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 Clause

Let'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:

Product_IDProduct_Quantityleast_of_Product_ID_quantity
20215.50015.500
10318.25018.250
11125.25025.250
21015.50015.500
21219.75019.750

LEAST Function with SUM function

In 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 Clause

Example of LEAST Function with WHERE Clause

Let'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:

least_of_SUM_Product_ID_quantity
114.75





Latest Courses