rand() function in SQL

The rand() function in SQL is used to generate a random floating-point number between 0 (inclusive) and 1 (exclusive).

The rand() function is a “good” example of how database designers went different ways: in half of the modern databases this function is named rand() and in the other half random(). Talking about engineers being lazy saving 2 characters from a function name. 😄

Syntax

The syntax for the rand() function is straightforward, as it doesn’t require any arguments:

💡 We skipped the FROM keyword since we don’t need a table to call a simple scalar function. This is a great way to test other scalar functions (that return a single value).

Generating random values

In practical use, rand() is often combined with columns in a table to generate random values for each row. For example, to assign a random value to each user in the users table:

Generating random values in ranges

If an integer within a specific range is needed, we can use the rand() function in combination with a range formula:

$\Large{RandomValue = floor(low + rand() * (high - low + 1))}$

  • floor() is a function that rounds down a number to the closest integer
  • high is the highest number in your distribution (inclusive)
  • low is the lowest number in your distribution (inclusive)

For example, here’s a query that generates a random age for the users table:

Shuffling rows with rand()

The most common use case for the rand() function is to shuffle records. For that we need to use the rand() function as a sorting value:

Sampling rows with rand()

Now that we have shuffled records, we can retrieve a random subset of users from the users table with a LIMIT clause:

Database compatibility

MySQL
PostgreSQL
SQLite
Redshift
Big Query
Snowflake
rand
rand
MySQL
PostgreSQL
SQLite
Redshift
Big Query
Snowflake

Find a problem on this page?

Want to get more involved? SQL MDN Docs is an open-source project, check out contribution guidelines.
This page was last modified on October 31, 2024.