Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • SQL Random Function

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 197
    Comment on it

    Using this function we can retrieve random row from a table in database . Mostly it is used in online exam , for random passwords etc .
    Every Database has their own syntax for random function .
    Syntax for MySQL Database

    select col_name from table_name order by rand ( ) limit 1 ;
    

    Syntax for SQL Database

    select top 1 col_name from table order by new id ( ) ;
    

    Syntax for Oracle Database

    select col_name from ( select col_name from table name  order by dbmsName_random.value ) where rownum =1
    

    Syntax for PostgreSQL Database

    select col_name from table_name order by rand ( ) limit 1 ;
    

    Example ->
    Table name -> student

     **Student_name**     **Student_Age**    **Student_Id**
         Mukesh                    23            1
         Ayush                     24            2
         Ishan                     20            4
         Pranav                    35            7   
         Abhishek                  26            8
         Ravi                      25            3
    

    If you want student name should be display in random order then you can do with rand ( ) function . See below

    Select * from student order by rand ( ) ;
    

    Output ->

    **Student_name**     **Student_Age**    **Student_Id**
      Abhishek                  26                  8
      Ayush                     24                  2
      Mukesh                    23                  1
      Ravi                      25                  3
      Ishan                     20                  4
      Pranav                    35                  7   
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: