Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

How can I best write a query that selects 10 rows randomly from a total of 600k?

1 Answer

0 votes
by (40.7k points)

You can try using the below code:

QUERY:

SELECT Name

  FROM random AS r JOIN

       (SELECT CEIL(RAND() *

                     (SELECT MAX(Id)

                        FROM random)) AS Id)

        AS r1

 WHERE r.Id>= r1.Id

 ORDER BY r.Id ASC

 LIMIT 1

Here, you can observe that the distribution of Ids is equal and that there can be gaps in the Id list as well.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 30, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Nov 21, 2020 in SQL by Appu (6.1k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...