EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials SQL Tutorial SQL SELECT RANDOM
 

SQL SELECT RANDOM

Roja Metla
Article byRoja Metla
EDUCBA
Reviewed byRavi Rathore

Updated March 13, 2023

SQL SELECT RANDOM

 

 

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.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

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: 

SQL Select Random 1

SQL Select Random 2

Below is the screenshot for the same.

SQL Select Random 3

SQL Select Random 4

Example 2

Select RAND() AS result2

Output: 

SQL Select Random 5

SQL Select Random 6

Below is the screenshot for the same.

SQL Select Random 7

Drag a SQL

Example 3

Select RAND()  AS result3;

Output:

Select SQL

Result 3

Below is the screenshot for the same.

Result 3-1

Result 3-2

Example 4

Select RAND()  AS result4;

Output: 

result 4-1

result 4-2

Below is the screenshot for the same.

result 4-3

result 4-4

Example 5

Select RAND()  AS result5;

Output:

result 5

result 5-1

Below is the screenshot for the same.

result 5-2

result 5-3

Example 6

Select RAND()  AS result6;

Output:

result 6-1

result 6-2

Below is the screenshot for the same.

result 6-3

result 6-4

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

example 2

Now let us apply RAND and check out the result.

Query:

SELECT *
FROM WORK_FROM_HOME
ORDER BY RAND();

Execution Output 1:

example 2-1

Execution Output 2:

Example -SQL 1

Execution Output 3:

Example -SQL 2

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.

SQL SELECT RANDOM 2-2

Execution Output 1:

SQL SELECT RANDOM 2-1

Execution Output 2: 

SQL SELECT RANDOM 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.

  1. BETWEEN in SQL
  2. Triggers in SQL
  3. PostgreSQL COMMIT
  4. SQL Subquery
  5. ANY in SQL | Examples

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - ENROLL NOW