question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Getting random rows

See original GitHub issue

What would be an efficient way to get random rows from a table? I saw rand() function being mentioned but don’t know how to call it or use it. Thank you!

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
vincentlauvlwjcommented, Feb 22, 2020

Oh, sorry. I forgot asc()/desc():

Employees.select().orderBy(rand().asc()).limit(0, 1)
0reactions
ashokgelalcommented, Feb 21, 2020

It works by converting rand() to an OrderByExpression first though:

val randExpression = OrderByExpression(rand().asExpression(), OrderType.ASCENDING)
Employees.select().orderBy(randExpression).limit(0, 1)
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to request a random row in SQL? - Stack Overflow
In SQL Server 2005 and up, you can use TABLESAMPLE to get a random sample that's repeatable: SELECT FirstName, LastName FROM Contact TABLESAMPLE...
Read more >
SQL Select Random Rows from Table - BeginnersBook
Selecting random rows from table in MySQL. Syntax: Here N specifies the number of random rows, you want to fetch. For example: If...
Read more >
SQL to Select a random row from a database table - Pete Freitag
Another great solution to selecting a random row: count number of rows in the table, pick a random value from 0 to that...
Read more >
How to randomly select rows from Pandas DataFrame
A random selection of rows from a DataFrame can be achieved in different ways. Create a simple dataframe with dictionary of lists.
Read more >
SQL SELECT RANDOM - javatpoint
SQL SELECT RANDOM. The SQL SELECT RANDOM() function returns the random row. It can be used in online exam to display the random...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found