Why does SELECT * FROM TABLE ORDER BY RAND() Work? I thought ORDER BY only works for columns.
So what exactly does it mean to ORDER BY RAND() or ORDER BY SUM()?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
ORDERwill work with any value you can put in your results (but doesn’t have to be one of the values in the results). This can be a column in any of the source tables or calculated using a function. For example, you could useORDER UPPER(name)for a case-insensitive sort.If you
ORDER BY RAND()you’re ordering by a random number generated for each row in the results, i.e. returning the rows in a random order. If you’re ordering bySUM()you’ve probably got aGROUP BYin there too so you could order customers by total calculated invoice total for example.Ideally you want to use a column from an index as this will be much faster.