![]() ![]() Whenever we need to sort a given SQL query result set, we have to use the ORDER BY clause. We want to shuffle the song list so that each new playlist is different than a previously generated one. Using a RANDOM function in the SQL ORDER BY clause The song table is populated as follows: id Let’s assume we are developing a music player desktop application, and we have the following song table in our database. ![]() If you have to shuffle a large result set and limit it afterward, then it’s better to use something like the Oracle SAMPLE(N) or the TABLESAMPLE in SQL Server or PostgreSQL instead of a random function in the ORDER BY clause. Note that sorting a large result set using a RANDOM function might turn out to be very slow, so make sure you do that on small result sets. This is a very handy trick, especially when you want to shuffle a given result set. In this article, we are going to see how we can sort an SQL query result set using an ORDER BY clause that takes a RANDOM function provided by a database-specific function. Follow you are trading with Kraken or Revolut, then you are going to love
0 Comments
Leave a Reply. |