What is RAND() in SQL?

The RAND() function returns a random float value greater than or equal to 0 and less than 10 <= value < 1.

Figure 1 shows a visual representation of the RAND() function.

Figure 1: Visual representation of RAND() function

Syntax

RAND(value)

Parameter

The RAND() function takes a value as a parameter, which acts as a seed value.

This is an optional parameter.

Return value

The RAND() function returns a random float value greater than or equal to 0 and less than 1.

If the value of the optional parameter is specified, then this function returns a repeatable sequence of random numbers.

The same seed values will produce the same random number.

Code

-- without seed value
SELECT RAND();
SELECT RAND();
-- with seed value and it shows that two rand() functions
-- with same seed value produce the same random number
SELECT RAND(10);
SELECT RAND(10);
-- rand() used with floor() to produce random integer
-- 10<=number<=20
SELECT FLOOR(10 + RAND()*(20-10+1))

Free Resources