• Monday, September 15, 2014

    SQL Database - Select a random row from table in MySQL, PostgreSQL, Oracle, Microsoft SQLServer

    For MySQL:

    SELECT * FROM table_name ORDER BY RAND() LIMIT 1

    You can also use below code to make query faster:

    SELECT * FROM table_name WHERE RAND() > 0.9 ORDER BY RAND()


    For PostgreSQL:


    SELECT * FROM table_name ORDER BY RANDOM() LIMIT 1


    For Orace:

    SELECT * FROM ( SELECT * FROM table_name ORDER BY dbms_random.value )
    WHERE rownum = 1


    For Microsoft SQL Server:

    SELECT TOP 1 column FROM table_name ORDER BY NEWID()

    Syed Masoom Rizvi

    Author & Editor

    Hi, This is Syed Masoom Rizvi, who is continue exploring the world of programming ,programming and programming... :)

    0 comments:

    Post a Comment