---
title: "Select n random rows from SQL Server table"  
description: "Select n random rows from SQL Server table"  
author: "Anonymous User"  
published: 2013-05-04  
updated: 2013-05-04  
canonical: https://www.mindstick.com/forum/815/select-n-random-rows-from-sql-server-table  
category: "mssql server"  
tags: ["mssql server"]  
reading_time: 2 minutes  

---

# Select n random rows from SQL Server table

Hi Everyone!\
I've got a SQL [Server table](https://www.mindstick.com/forum/396/how-to-selecte-random-records-from-sql-server-table) with about 50,000 rows in it. I want to [select](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) about 5,000 of those rows at [random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c). I've thought of a complicated way, creating a temp \
table with a "random number" [column](https://www.mindstick.com/forum/33860/how-to-calculate-column-summary-in-sql-server), copying my table into that, looping through the [temp table](https://www.mindstick.com/forum/156791/how-to-create-temp-table-for-stored-procedure-in-sql-server) and updating each row with RAND(), and then selecting from that table \
where the random number column < 0.1. I'm looking for a simpler way to do it, in a single statement if possible.\
This [article](https://www.mindstick.com/articles/95867/are-you-looking-for-500-credit-score-mortgage-lenders-houston-continue-reading-this-article) suggest using the NEWID() function. That looks promising, but I can't see how I could reliably select a [certain percentage](https://answers.mindstick.com/qa/116273/video-upload-stuck-at-a-certain-percentage) of rows.\
Anybody ever do this before? Any ideas?\
Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by AVADHESH PATEL

Hi Jeet!\
select top 10 percent * from [yourtable] order by newid()In response to the "pure trash" comment concerning large tables: you could do it like this to improve performance.\
select * from [yourtable] where [yourPk] in (select top 10 percent [yourPk] from [yourtable] order by newid())The cost of this will be the key scan of values plus the join cost, which on a large table with a small percentage selection should be reasonable.\


---

Original Source: https://www.mindstick.com/forum/815/select-n-random-rows-from-sql-server-table

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
