Question:

Randomly select rows from sql

by Guest2325  |  12 years, 7 month(s) ago

0 LIKES UnLike

Randomly select rows from sql

 Tags: randomly, rows, select, SQL

   Report

3 ANSWERS

  1. Guest3173
    SELECT TOP 5 column FROM table ORDER BY NEWID()

  2. Leonardo
    NEWID() is MS SQL specific.

    For MS Access use:

    SELECT TOP 1 Field1 , ..., FieldN
    FROM Table1
    ORDER BY Rnd(Field1)


    For MySQL use:

    SELECT Field1, ..., FieldN
    FROM Table1
    ORDER BY RAND()
    LIMIT 1


    For Oracle use:

    SELECT Field1, ..., FieldN
    FROM (     SELECT Field1, …, FieldN
         FROM Table1
         ORDER BY dbms_random.value)
    WHERE rownum <= 1


    For SQL Lite use:

    SELECT Field1, ..., Field2
    FROM Table1
    ORDER BY Random()
    LIMIT 1


    For DB2 use:

    SELECT Field1, ..., FieldN, RAND() as RID
    FROM Table1
    ORDER BY RID
    FETCH FIRST 1 ROWS ONLY


    For PostgreSQL use:

    SELECT "Field1", "...", "FieldN"
    FROM "Table1"
    ORDER BY RANDOM()
    LIMIT 1
  3. Guest2035
    There are lots of ways to select a random record or row from a database table. Here are some example SQL statements that don't require additional application logic, but each database server requires different SQL syntax

    SELECT column FROM table
    ORDER BY RAND()

    SELECT column FROM table
    ORDER BY RANDOM()
    LIMIT 1
Sign In or Sign Up now to answser this question!

Question Stats

Latest activity: 15 years, 1 month(s) ago.
This question has 3 answers.

BECOME A GUIDE

Share your knowledge and help people by answering questions.