RAND Function - SQL


Overview


The RAND function in SQL generates a pseudo-random floating-point number between 0 and 1. It is useful for obtaining random values in queries.

Example:

SELECT RAND(5)

Syntax:

SELECT RAND([seed])

[seed] is optional and if provided, initializes the random number generator with a specific starting point, ensuring repeatable results. If omitted, the function returns a different random number each time it is called.


Example: No input


In this example, we are returning a random number, it will be different every time you run the function.


Example: Hard-coded value


In this example, we are also returning a random number but when we use the number 5 in the function, we always get the same random result.