SQL Random Row Generator
Generate SQL queries that select random rows — techniques for MySQL, PostgreSQL, SQLite, and SQL Server.
About Random Row Selection
Selecting random rows from a database table is a common requirement for sampling, A/B testing, randomized surveys, recommendation systems, and generating test data. However, the naive approach — ORDER BY RAND() — can be extremely slow on large tables because it requires generating a random number for every row in the table before sorting.
For large tables, better approaches include: selecting a random OFFSET (get the table count first, then SELECT with LIMIT 1 OFFSET random_number), using the random ID technique (WHERE id >= FLOOR(RAND() * MAX(id)) LIMIT N), or using database-specific functions like TABLESAMPLE in PostgreSQL. This tool generates efficient random selection queries for each major SQL database using the best method for your chosen dialect.