Back

Explore Courses Blog Tutorials Interview Questions
0 votes
1 view
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.4k 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)
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers

500 comments

94k users

Browse Categories

...