1

I need you help...for a little problem. I have a java service that should access in a table and get a random row from table.

My table is simply: it contains only two cols:

"Id"        INT IDENTITY(1,1) NOT NULL Primary Key
"Datas"     Varchar(64)       NOT NULL

Values Id is an progressive number, so you should think it could be enough to create a random number and get the row where id=randomic_number.

But I have lots of gap in table. So for example, a sample of table could be this:

  ID    Datas
  1     Row1
  2     Row2
  3     Row3
  8     Row4
 10     Row5
 25     Row6
639     Row7

Is there a very stylish way to get one row randomly? No condition must be...only random! I use sql srv 2000.

I would avoid to to...

select *

and then cycling the entire Resultset using a random number...because it can contain a very large number of rows....

5
  • see titov.net/2005/09/21/…
    – Leo
    Commented Aug 11, 2014 at 13:37
  • What database are you using? I assume MSSQL (due to the IDENTITY keyword)?
    – exhuma
    Commented Aug 11, 2014 at 13:38
  • Alright. Thanks for clarifying. I added the tsql tag to reflect this.
    – exhuma
    Commented Aug 11, 2014 at 13:47
  • possible duplicate of How to request a random row in SQL?
    – exhuma
    Commented Aug 11, 2014 at 13:53
  • EXHUMA!!! FANTASTIC!!! SELECT TOP 1 column FROM table ORDER BY NEWID() THANK you VERY MUCH!!! Commented Aug 11, 2014 at 13:59

2 Answers 2

4

You should be able to do something along the lines of:

SELECT TOP 1 * FROM mytable ORDER BY newid()

Note: this is a duplicate of #52964 which is in turn a duplicate of #19412

12
  • 1
    In SQL, the rand function will generate one random value for the statement and apply that one value to every row returned. Commented Aug 11, 2014 at 13:35
  • Great answer, I just tried it on PostgreSQL and it works. Using the database to return the random row is the best option as doing it in java on the client will not be nearly as elegant or efficient Commented Aug 11, 2014 at 13:36
  • 1
    @PhilipKelley How does checksum further randomise this? To my knowledge checksum is a -- ducks -- hashing function (i.e. deterministic). So the same ID will always return the same result. I don't see how that would aid in randomisation.
    – exhuma
    Commented Aug 11, 2014 at 13:49
  • 1
    Indeed TABLESAMPLE is not completely random, as the unit of selection is page not a row. But if you have significant data size then the trade off is necessary. One can combine TABLESAMPLE with top(1) ... order by newid() to get 'quite random' w/o paying the penalty of large data scans. If the table is small then why bother, indeed... Commented Aug 11, 2014 at 13:54
  • 1
    @PhilipKelley newid() guids have no 'sequentialness' whatsoever. for all purposes, bare crypto, they are as random as it gets. Commented Aug 11, 2014 at 13:56
0

I would suggest to get the last id in the table like so

SELECT TOP 1 Id FROM table_name ORDER BY Id DESC

then assuming this is stored in the maxId variable, you could generate a random number index between 1 and maxId and do :

SELECT TOP 1 * FROM table_name WHERE Id > index

And that's it

1
  • Not randomic at all if I have a big gap. For example: I have the following ID: 1,2,3,650,651,652,etc... Randomic numbers beetween 4 and 649...will return me always the same row (n° 650)... Commented Aug 11, 2014 at 13:56

Not the answer you're looking for? Browse other questions tagged or ask your own question.