SQL

Select N Random Records from a Table

Discover how to retrieve a specified number of random records from your database table using SQL's RANDOM() function and LIMIT, ideal for dynamic content.

SELECT *
FROM products
ORDER BY RANDOM()
LIMIT 5;
How it works: This query selects a random set of records from the `products` table. The `ORDER BY RANDOM()` clause shuffles the entire result set randomly before any filtering. `LIMIT 5` then selects only the first 5 records from this newly randomized order, providing a different set of 5 random products each time the query is executed. This is useful for "featured items" or "explore" sections.

Need help integrating this into your project?

Our team of expert developers can help you build your custom application from scratch.

Hire DigitalCodeLabs