i




 
Thread Rating:
  • 0 Votes - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
SQL to Select a random row from a database table
02-07-2011, 12:53 PM
Post: #1
SQL to Select a random row from a database table
SQL to Select a random row from a database table

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 a random row with Microsoft SQL Server:
Code:
SELECT TOP 1 column FROM table
ORDER BY NEWID()


Select a random row with MySQL:
Code:
SELECT column FROM table
ORDER BY RAND()
LIMIT 1

Select a random record with Oracle:
Code:
SELECT column FROM
( SELECT column FROM table
ORDER BY dbms_random.value )
WHERE rownum = 1

Select a random row with PostgreSQL:
Code:
SELECT column FROM table
ORDER BY RANDOM()
LIMIT 1

Select a random row with IBM DB2
Code:
SELECT column, RAND() as IDX
FROM table
ORDER BY IDX FETCH FIRST 1 ROWS ONLY


Thanks & Regards.



Thanks & Regards,
Quote
02-09-2011, 04:26 PM
Post: #2
RE: SQL to Select a random row from a database table
nice sharing


Thanks and Regards
Raju
Quote
02-10-2011, 11:52 AM
Post: #3
RE: SQL to Select a random row from a database table
Good Information Smile

Please give reputation if my answer/post is useful for you
Best Regards
Arti Gupta
Quote


Possibly Related Threads...
Thread: Author Replies Views: Last Post
  Why can I save the error information into the table of SQL server ? dongtrien 0 893 09-29-2015 04:36 AM
Last Post: dongtrien
  Generate Random file name in c# nisar87 0 2,286 11-30-2012 09:15 PM
Last Post: nisar87
  Getting table info of all tables in a database with c# haider.ali 0 2,053 12-06-2011 05:49 PM
Last Post: haider.ali
  Sql server Database restore using c# haider.ali 0 6,981 08-23-2011 04:52 PM
Last Post: haider.ali
  Sql server Database backup using c# haider.ali 0 4,547 08-18-2011 04:39 PM
Last Post: haider.ali



User(s) browsing this thread: 1 Guest(s)