---
title: "SQL selecting random rows, but not reselecting them next time"  
description: "SQL selecting random rows, but not reselecting them next time"  
author: "Jayden Bell"  
published: 2013-05-06  
updated: 2013-05-06  
canonical: https://www.mindstick.com/forum/822/sql-selecting-random-rows-but-not-reselecting-them-next-time  
category: "mssql server"  
tags: ["mssql server"]  
reading_time: 1 minute  

---

# SQL selecting random rows, but not reselecting them next time

Hi Everyone!\
I'm making a [random](https://www.mindstick.com/forum/33419/generating-random-numbers-in-objective-c) [selection](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) of 50 [items](https://www.mindstick.com/forum/33812/getting-number-of-items-selected-in-uicollectionview-in-ios), but when another call is made, I want to [select](https://www.mindstick.com/forum/160534/orderby-then-select-vs-select-then-orderby-performance) another random 50 that DOES NOT include anything selected previously. How \
best to do this? Is there an [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) statement that handles this?\
// [Collect](https://www.mindstick.com/interview/34468/how-do-you-collect-and-clean-datasets) 50 random entries$stmt = $db->[query](https://www.mindstick.com/blog/202/sub-query-in-sqlserver)('SELECT * FROM db[ORDER](https://www.mindstick.com/articles/12276/how-timely-order-deliveries-can-improve-customer-experience) BY RAND() [LIMIT](https://www.mindstick.com/forum/159824/explain-the-purpose-of-the-limit-and-offset-clauses-and-how-are-they-used-for-pagination) 50 ');\
Thanks in [advance](https://www.mindstick.com/blog/33258/jee-mains-and-jee-advance-exams)!

## Replies

### Reply by AVADHESH PATEL

Hi Jayden!\
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/822/sql-selecting-random-rows-but-not-reselecting-them-next-time

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
