57

I would like to take a database of say, 1000 users and select 20 random ones (ORDER BY rand(),LIMIT 20) then order the resulting set by the names. I came up with the following query which is not working like I hoped.

SELECT * FROM users WHERE 1 ORDER BY rand(), name ASC LIMIT 20

4
  • What exactly isn't working for you? May 21, 2010 at 14:00
  • When I try order by rand() limit 3 on a database here of 1600 users I consistently get random results back.
    – vfilby
    May 21, 2010 at 14:01
  • @vfilby: Yes, now try order by rand(), name asc and you'll see that it isn't sorted by name.
    – Josh K
    May 21, 2010 at 14:05
  • Since secondary order by clause will work only if those rows with same value for the primary sorting column, primary sorting column are all unique values if you use order by rand() first. May 17, 2023 at 7:57

5 Answers 5

77

Use a subquery:

SELECT * FROM 
(
    SELECT * FROM users ORDER BY rand() LIMIT 20
) T1
ORDER BY name 

The inner query selects 20 users at random and the outer query orders the selected users by name.

3
  • 6
    Awesome, two things. Why the T1 after the query, what's it's purpose? Second: Do you know if it's possible to use a subquery in datamapper?
    – Josh K
    May 21, 2010 at 14:04
  • Ah, so you're casting that inner select as a new temporary table. Got it.
    – Josh K
    May 21, 2010 at 14:06
  • 6
    @Josh K: Regarding your first question, it's called an alias. If you omit it you get this error: Every derived table must have its own alias.
    – Mark Byers
    May 21, 2010 at 14:08
15

Beware of ORDER BY RAND() because of performance and results. Check this article out: http://jan.kneschke.de/projects/mysql/order-by-rand/

2
  • Agree, its possibly the worst performing query.
    – mhughes
    May 21, 2010 at 14:50
  • Additional WHERE constraints put the RAND load on about at most 500 rows. This is not a huge table, performance is not an issue.
    – Josh K
    May 21, 2010 at 17:55
10

Instead of using a subquery, you could use two separate queries, one to get the number of rows and the other to select the random rows.

SELECT COUNT(id) FROM users; #id is the primary key

Then, get a random twenty rows.

$start_row = mt_rand(0, $total_rows - 20);

The final query:

SELECT * FROM users ORDER BY name ASC LIMIT $start_row, 20;
2
  • I believe this should be the correct answer, simply because I think the resources might be lower than calling RAND() or using a 'subquery'.
    – Decoded
    Jan 21, 2016 at 17:45
  • 4
    You would always get 20 consecutive users ("consecutive" with regard to their names), not 20 random ones.
    – Imanuel
    Jun 7, 2016 at 10:56
3
SELECT  *
FROM    (
        SELECT  *
        FROM    users
        WHERE   1
        ORDER BY
                rand()
        LIMIT 20
        ) q
ORDER BY
        name
2

Use a subquery:

SELECT * FROM (
    SELECT * FROM users ORDER BY RAND() LIMIT 20
) u
ORDER BY name

or a join to itself:

SELECT * FROM users u1
INNER JOIN (
    SELECT id FROM users ORDER BY RAND() LIMIT 20
) u2 USING(id)
ORDER BY u1.name

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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