Javatpoint Logo
Javatpoint Logo

GREATEST Function in SQL

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

Syntax of GREATEST Function

In the GREATEST syntax, we have to pass those numbers from which we want to find greatest value.

In the Structured Query Language, we can also use the GREATEST 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 GREATEST function.

Examples of GREATEST function

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

Output:

greatest_of_numbers
8

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

Output:

greatest_of_numbers
108

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

Output:

greatest_of_numbers
34

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

Output:

greatest_of_numbers
3.82

Example 5: This example returns the greatest value:

Output:

greatest_of_numbers
Shows Error

Example 6: This example uses the GREATEST function with the SQL table.In this example, we will create the new table through which we will perform the GREATEST 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_ID Product_Name Product_Quantity Purchasing_Price Selling_Price Release_Date Product_Rating
104 P1 10.250 945 NULL 2022-04-30 NULL
202 P4 15.500 45 75 2022-01-28 5
103 P2 18.250 25 NULL 2022-02-18 4
111 P7 25.250 5 15 2021-12-25 9
210 P6 15.500 50 70 2021-10-15 NULL
212 P8 19.750 110 250 2022-01-28 4
112 P10 10.250 550 835 2022-04-11 NULL

Query 1: The following SELECT query uses the GREATEST function with the Product_Quantity and Product_ID column of the above Product_Details table:

This query shows the greatest value from the product id and quantity of each product.

Output:

Product_ID Product_Quantity greatest_of_Product_ID_quantity
104 10.250 150
202 15.500 312
103 18.250 147
111 25.250 157
210 15.500 322
212 19.750 324
112 10.250 160

Query 2: The following SELECT query uses the GREATEST function with Selling_Price and Purchasing_Price column of the above Product_Details table:

This query shows the greatest value from purchasing and selling price of each product.

Output:

Selling_Price Purchasing_Price greatest_of_Selling_PurchasingPrice
NULL 945 -
75 45 75
NULL 25 -
15 5 15
70 50 70
250 110 250
835 550 835

Query 3: The following SELECT query uses the GREATEST function with the Purchasing_Price and Product_ID column of the above Product_Details table:

This query shows the greatest value from the product id and purchasing price of each product.

Output:

Product_ID Purchasing_Price greatest_of_Product_ID_and_Purchasing
104 945 945
202 45 202
103 25 103
111 5 111
210 50 210
212 110 212
112 550 550

Next Topic#





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA