Thursday, December 6, 2012

Select random records from database (MySQL, Oracle, MS SQL)

Sometimes we need to select random records from a table in our database. For example, if you created online test application then every time we need to get the random questions from our questions table.
Today I will give you some examples for selecting random records in some popular databases.


Select random rows in MySQL

SELECT column FROM table
ORDER BY RAND()
LIMIT 10

Select random rows in Oracle

SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum <= 10

Select random rows in Microsoft SQL Server

SELECT TOP 10 column FROM table
ORDER BY NEWID()

Happy reading!!

No comments:

Post a Comment

^ Scroll to Top