How can I select random rows from a SQL table using CockroachDB? For example, if I have a number of questions and I want to generate a different sequence each time a student loads them.
CockroachDB doesn't offer an efficient way to do this yet! For a non-efficient way you can use SELECT ... FROM ... ORDER BY random() LIMIT 1;
Alternatively, you can handle shuffling the results of a SELECT
statement in your application itself. After putting the results into an array (or any other aggregate-like structure), you can also shuffle the order there.
I'm using the following statement to select random number of rows from cockroach db.
SELECT ... FROM ... WHERE round(random()*10) % 10 = 0 LIMIT 10
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With