Beginner

302. RANDOM Function

Learn how SQL databases can generate pseudo-random numeric values.

📘 Lesson 302 of 728 ⏱ 9 min 🎯 Intermediate

A random-number function generates a pseudo-random value. The function name and returned range vary between database systems.

Random values can be useful when selecting sample records, testing applications, or creating randomized analytical datasets.

Generate a random value for each customer record.

SQL
SELECT
    CustomerID,
    RANDOM() AS RandomValue
FROM Customers;

Random functions can support sampling, testing, simulations, and randomized record selection.

  • Random functions generate pseudo-random values.
  • Function names vary by database.
  • They can support data sampling.
  • They are useful for testing.
  • Random results are not always reproducible.
Why are random functions used in SQL?

They can be used for sampling, testing, simulations, and randomized record selection.

Is RANDOM syntax the same in every database?

No. Different databases provide different random-number functions.

Have you finished this lesson?

Introduction to SQL – All Lessons