{"id":1172,"date":"2011-01-18T10:36:31","date_gmt":"2011-01-18T09:36:31","guid":{"rendered":"http:\/\/www.galhano.com\/blog\/?p=1172"},"modified":"2011-01-18T10:36:31","modified_gmt":"2011-01-18T09:36:31","slug":"sql-to-select-a-random-row-from-a-database-table","status":"publish","type":"post","link":"https:\/\/galhano.com\/blog\/?p=1172","title":{"rendered":"SQL to Select a random row from a database table"},"content":{"rendered":"<h3>Select a random row with MySQL:<\/h3>\n<pre>SELECT column FROM table\r\nORDER BY <strong>RAND()<\/strong>\r\nLIMIT 1<\/pre>\n<h3>Select a random row with PostgreSQL:<\/h3>\n<pre>SELECT column FROM table\r\nORDER BY <strong>RANDOM()<\/strong>\r\nLIMIT 1<\/pre>\n<h3>Select a random row with Microsoft SQL Server:<\/h3>\n<pre>SELECT TOP 1 column FROM table\r\nORDER BY <strong>NEWID()<\/strong><\/pre>\n<h3>Select a random row with IBM DB2<\/h3>\n<pre>SELECT column, RAND() as IDX \r\nFROM table \r\nORDER BY IDX FETCH FIRST 1 ROWS ONLY<\/pre>\n<p><a title=\"DB2 Random Row Selection\" href=\"http:\/\/www.timfanelli.com\/cgi-bin\/pyblosxom.cgi\/database\/random_row_selection\"><\/a><\/p>\n<h3>Select a random record with Oracle:<\/h3>\n<pre>SELECT column FROM\r\n( SELECT column FROM table\r\nORDER BY dbms_random.value )\r\nWHERE rownum = 1\r\n\r\nsource link: http:\/\/www.petefreitag.com\/item\/466.cfm<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Select a random row with MySQL: SELECT column FROM table ORDER BY RAND() LIMIT 1 Select a random row with PostgreSQL: SELECT column FROM table ORDER BY RANDOM() LIMIT 1 Select a random row with Microsoft SQL Server: SELECT TOP 1 column FROM table ORDER BY NEWID() Select a random row with IBM DB2 SELECT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"close","ping_status":"close","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22,9,2,104],"tags":[173],"class_list":["post-1172","post","type-post","status-publish","format-standard","hentry","category-microsoft","category-sql-server","category-truques-dicas","category-transact-sql","tag-sql-server","author-admin"],"_links":{"self":[{"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1172","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1172"}],"version-history":[{"count":2,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1172\/revisions"}],"predecessor-version":[{"id":1174,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/1172\/revisions\/1174"}],"wp:attachment":[{"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1172"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1172"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/galhano.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1172"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}