Updated March 13, 2023
Introduction to SQL SELECT RANDOM
SQL Random function is used to get random rows from the result set. We use random function in online exams to display the questions randomly for each student. The usage of the SQL SELECT RANDOM is done differently in each database. Let us check the usage of it in different database. The RAND() function returns the random number between 0 to 1.
In MYSQL:
SELECT < column_name > FROM < table_name >
ORDER BY RAND()
n MYSQL we use the RAND() function to get the random rows from the database.
In POSTGRE SQL:
SELECT < column_name > FROM < table_name >
ORDER BY RAND()
In postgre sql the representation of the random function is similar to the SQL.
In ORACLE:
SELECT < column_name > FROM
( SELECT < column_name > FROM < table_name >
ORDER BY dbms_random.value )
In Netezza:
SELECT RANDOM()
FROM < source >
Syntax:
SELECT RAND()
FROM < source >
In MYSQL we use the RAND() function to get the random rows from the database.
How to Implement SQL SELECT RANDOM?
Let us implement the usage of the Random in the SQL. Below is to get the random number from 0 to 1.
Select RAND() AS <RESULT1>
Here in the above example, we get the values randomly between 0 to 1. Let us execute the scripts and get the result.
Example 1
Select RAND() AS result1
Output:
Below is the screenshot for the same.
Example 2
Select RAND() AS result2
Output:
Below is the screenshot for the same.
Example 3
Select RAND() AS result3;
Output:
Below is the screenshot for the same.
Example 4
Select RAND() AS result4;
Output:
Below is the screenshot for the same.
Example 5
Select RAND() AS result5;
Output:
Below is the screenshot for the same.
Example 6
Select RAND() AS result6;
Output:
Below is the screenshot for the same.
Examples of SQL SELECT RANDOM
Below are some different examples
Example #1
Let us consider an example regarding the random function in tables. Let us consider the table “work_from_home”. This table consists of the data, which related to the people working from home.
Table: WORK_FROM_HOME
Now let us apply RAND and check out the result.
Query:
SELECT *
FROM WORK_FROM_HOME
ORDER BY RAND();
Execution Output 1:
Execution Output 2:
Execution Output 3:
Example#2
Let us consider example regarding the random function in tables. Let us consider the table “PAYMENT_DETAILS”. This table consists of the data, which related to amount to be paid to the people working from home.
Execution Output 1:
Execution Output 2:
Conclusion
To be precise of the Random function we need to keep in mind the below points:
- Whenever we want random value to be calculated and send the output, we use random function. Example as mentioned in online exams to display the questions randomly for each student.
- The usage of the SQL SELECT RANDOM is done differently in each database. Some database it is shown as RAND() and other as RANDOM().
- The RAND() function returns the random number between 0 to 1.
Recommended Articles
We hope that this EDUCBA information on “SQL SELECT RANDOM” was beneficial to you. You can view EDUCBA’s recommended articles for more information.