Golang Tutorials - Learn Go Programming with Easy Step-by-Step Guides

Explore comprehensive Golang tutorials for beginners and advanced programmers. Learn Go programming with easy-to-follow, step-by-step guides, examples, and practical tips to master Go language quickly.

Order By Random in SQL

Order By Random in SQL

ORDER BY RANDOM() in SQL

ORDER BY RANDOM() is used to return rows in a random order. It’s useful when you want to shuffle or randomly select rows from a table.


Syntax:

SELECT *FROM table_nameORDER BY RANDOM();

Examples:

Select all rows in random order:

SELECT *FROM employeesORDER BY RANDOM();

Select 5 random rows:

SELECT *FROM employeesORDER BY RANDOM()LIMIT 5;

Notes:

  • PostgreSQL and SQLite use RANDOM() for random ordering.

  • MySQL uses ORDER BY RAND() instead.

  • Using random order can be slow on large tables because it must assign a random value to every row before sorting.


MySQL version:

SELECT *FROM employeesORDER BY RAND()LIMIT 5;

Want help with efficient random sampling from large tables?

Disclaimer for AI-Generated Content:
The content provided in these tutorials is generated using artificial intelligence and is intended for educational purposes only.
html
docker
php
kubernetes
golang
mysql
postgresql
mariaDB
sql