RAND Function in SQLThe 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 FunctionIn 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 functionExample 1: This example returns any random number: Output:
Example 2: This example returns the Rand value of the specified number: Output:
Example 3: This example returns the Rand value of 90: Output:
Example 4: This example returns the Rand value of -45: Output:
Example 5: This example returns the Rand value of the given expression: Output:
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:
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:
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:
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:
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:
Next TopicATN2 Function in SQL |