Objective: Fetch a Sample Row
It’s surprising how frequently we need an SQL query that returns a random result. This seems to go against the principles of repeatability and deterministic programming. However, it’s ordinary to ask for a sample from a large data set. The following are some examples:
-
Displaying rotating content, such as an advertisement or a news story to highlight
-
Auditing a subset of records
-
Assigning incoming calls to available operators
-
Generating test data
It’s better to query the database for this sample, as an alternative to fetching the entire data set into your application just so you can pick a sample from the set.
The objective is to write an efficient SQL query that returns only a random sample of data. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access