SQL random number between 1 and 3 – Programming, Pseudocode Example, C# Programming Example

SQL random number between 1 and 3

In various database scenarios, the need to generate random numbers within a specific range arises frequently. Whether you’re working with test data, creating a sampling mechanism, or introducing an element of unpredictability into your queries, generating random numbers is a valuable skill. This article delves into the techniques to generate random numbers between 1 and 3 in SQL, providing insights applicable across different database management systems.

Using Standard SQL:

In standard SQL, you can leverage the RAND() function to generate random float values between 0 and 1. To restrict the range and obtain integers between 1 and 3:


SQLite, being a lightweight and widely used embedded database, allows you to achieve this with the RANDOM() function:


In Oracle databases, the DBMS_RANDOM package comes in handy for randomization. To generate numbers between 1 and 3:


MySQL’s RAND() function can be utilized similarly:

MSSQL (Microsoft SQL Server):

In MSSQL, combining NEWID() and CHECKSUM() provides a solution:


Generating random numbers between 1 and 3 in SQL is achievable across various database systems using their respective functions. Whether you’re working with Standard SQL, SQLite, Oracle, MySQL, or MSSQL, understanding these techniques empowers you to add a layer of unpredictability to your database operations. Incorporating randomization can be beneficial in scenarios such as testing, sampling, or any situation that requires an element of randomness in your SQL queries.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: