RAND Function in SQL

The RAND() is a mathematical function in Structured Query Language which returns the random number between 0 and 1. 0 and 1 may also be returned by the function in the output.

Syntax of RAND Function

In this SELECT syntax, we have to pass that numeric number in the function whose rand value we want to find.

In the Structured Query Language, we can also use the RAND function in SELECT query with the table field:

In this SELECT query, we have to define the name and field of that table on which we want to perform the RAND function.

Examples of RAND function

Example 1: This example returns any random number:

Output:

Randvalue
0.63537333961116604

Example 2: This example returns the Rand value of the specified number:

Output:

Randvalue_of_180
0.71692729506814135

Example 3: This example returns the Rand value of 90:

Output:

Randvalue_of_90
0.71525032765490337

Example 4: This example returns the Rand value of -45:

Output:

Randvalue_of_-45
0.71441184394828439

Example 5: This example returns the Rand value of the given expression:

Output:

Randvalue_of_Expression
0.71469133851

Example 6: This example uses the RAND function with the SQL table.

In this example, we will create the new table through which we will perform the RAND function on the columns:

The following shows the syntax to create the new table in SQL:

The following CREATE statement creates the Vehicle_Details table for storing the details of purchasing and selling vehicles:

The following multiple INSERT queries insert the records of vehicles with their quantity, and number of selling and purchasing vehicles into the Vehicle_Details table:

The following SELECT statement displays the inserted records of the above Vehicle_Details table:


Vehicle_IDVehicle_NameVehicle_QuantityPurchasing_VehiclesSelling_VehiclesRelease_DateVehicle_Rating
140P129045802022-04-3080
190P4151801802022-01-2890.85
145P2202702902022-02-1880
90P710360802021-12-25180
45P63545-152021-10-1580
210P830160-602022-01-2895
185P102904504702022-04-1180

Query 1: The following SELECT query uses the RAND function with the Vehicle_ID column of the above Vehicle_Details table:

This query shows the Rand value of vehicle id of each vehicle.

Output:

Vehicle_IDRand value_of_Vehicle_ID
1400.716819
1900.717113
1450.7162751
900.71525032765490337
450.71441184394828439
2100.71748628420588734
1850.71702045992443231

Query 2: The following SELECT query uses the RAND function with the Purchasing_Vehicles column of the above Vehicle_Details table:

This query shows the Rand value of purchasing vehicles.

Output:

Purchasing_VehiclesRand value_of_PurchasingVehicles
450.71441184394828439
1800.71692729506814135
2700.71860426248137932
3600.72028122989461729
450.71441184394828439
1600.71655463564297739
4500.72195819730785538

Query 3: The following SELECT query uses the RAND function with the Vehicle_Rating column of the above Vehicle_Details table:

This query shows the Rand value of rating of each vehicle from the above table.

Output:

Vehicle_RatingRand value_of_vehiclerating
800.71506399794232134
90.850.71525032765490337
800.71506399794232134
1800.71692729506814135
800.71506399794232134
950.71534349251119433
800.71506399794232134

Query 4: The following SELECT query uses the RAND function with the and Purchasing_Vehicles and Selling_Vehicles column of the above Vehicle_Details table:

This query shows the Rand value of selling vehicle.

Output:

Selling_VehiclesRand value_of_SellingVehicles
800.71506399794232134
1800.71692729506814135
2900.71897692190654339
800.71506399794232134
-150.71385285481053828
-600.71469133851715738
4700.72233085673301933