Select a Random Row from a SQL Server Result

For a modest result set, TOP (1) combined with ORDER BY NEWID() is a convenient way to choose a random row. Put eligibility conditions in WHERE so that the random choice is made among the rows you actually permit.

Select One Eligible Row

SQL
DECLARE @Choices TABLE
(
    ChoiceId int NOT NULL PRIMARY KEY,
    Label nvarchar(30) NOT NULL,
    IsEnabled bit NOT NULL
);
INSERT @Choices VALUES
    (1, N'Blue', 1), (2, N'Green', 1), (3, N'Red', 0);

SELECT TOP (1) ChoiceId, Label
FROM @Choices
WHERE IsEnabled = 1
ORDER BY NEWID();

The result is either Blue or Green, with one row returned. Red is excluded before selection. Repeated runs may return the same row, which is normal. With no eligible rows, the query returns none.

Select Several Rows

Change TOP (1) to TOP (2) to return two distinct source rows from this set. It does not sample the same source row repeatedly. If the input itself contains duplicate logical entities, remove that duplication according to your data model before sampling.

Understand the Cost and Limits

SQL Server generally needs to inspect the qualifying set and perform random-order work. This is straightforward for small lists but can be expensive for millions of rows. Review the execution plan and I/O on realistic data before using it in a high-frequency request path.

The technique is not a reproducible seeded shuffle or a security-grade lottery specification. Do not replace it casually with a random integer between the minimum and maximum identity values. Gaps and uneven data can bias a subsequent lookup, and a missing key can produce no result.

For large workloads, choose a sampling strategy around the actual requirement for fairness, repeatability, latency and exact count. A preselected candidate pool can lower the work but changes the population being sampled. Make that choice explicit rather than hiding it as an optimization.

Microsoft References

Author: Herman Schoenfeld