Answer by Jack Briner for Select n random rows from SQL Server table
If you know you have approximately N rows and you want approximately K random rows, you just need to pull any given row with a chance K/N. Using the RAND() function which gives you a fair distribution...
View ArticleAnswer by user2864740 for Select n random rows from SQL Server table
Here is an updated and improved form of sampling. It is based on the same concept of some other answers that use CHECKSUM / BINARY_CHECKSUM and modulus.Reasons to use an implementation similar to this...
View ArticleAnswer by Santosh Karanam for Select n random rows from SQL Server table
select * from tablewhere id in ( select id from tableorder by random()limit ((select count(*) from table)*55/100))// to select 55 percent of rows randomly
View ArticleAnswer by SpacePhoenix for Select n random rows from SQL Server table
The server-side processing language in use (eg PHP, .net, etc) isn't specified, but if it's PHP, grab the required number (or all the records) and instead of randomising in the query use PHP's shuffle...
View ArticleAnswer by VISHMAY for Select n random rows from SQL Server table
I was using it in subquery and it returned me same rows in subquery SELECT ID , ( SELECT TOP 1 ImageURL FROM SubTable ORDER BY NEWID() ) AS ImageURL, GETUTCDATE() , 1 FROM Mytablethen i solved with...
View ArticleAnswer by Nanki for Select n random rows from SQL Server table
This is a combination of the initial seed idea and a checksum, which looks to me to give properly random results without the cost of NEWID():SELECT TOP [number] FROM table_nameORDER BY RAND(CHECKSUM(*)...
View ArticleAnswer by Sarsaparilla for Select n random rows from SQL Server table
It appears newid() can't be used in where clause, so this solution requires an inner query:SELECT *FROM ( SELECT *, ABS(CHECKSUM(NEWID())) AS Rnd FROM MyTable) vwWHERE Rnd % 100 < 10 --10%
View ArticleAnswer by RJardines for Select n random rows from SQL Server table
This link have a interesting comparison between Orderby(NEWID()) and other methods for tables with 1, 7, and 13 millions of rows.Often, when questions about how to select random rows are asked in...
View ArticleAnswer by klyd for Select n random rows from SQL Server table
Didn't quite see this variation in the answers yet. I had an additional constraint where I needed, given an initial seed, to select the same set of rows each time.For MS SQL:Minimum example:select top...
View ArticleAnswer by Oskar Austegard for Select n random rows from SQL Server table
If you (unlike the OP) need a specific number of records (which makes the CHECKSUM approach difficult) and desire a more random sample than TABLESAMPLE provides by itself, and also want better speed...
View ArticleAnswer by Kyle McClellan for Select n random rows from SQL Server table
Selecting Rows Randomly from a Large Table on MSDN has a simple, well-articulated solution that addresses the large-scale performance concerns. SELECT * FROM Table1 WHERE (ABS(CAST( (BINARY_CHECKSUM(*)...
View ArticleAnswer by Ravi Parashar for Select n random rows from SQL Server table
Try this:SELECT TOP 10 Field1, ..., FieldNFROM Table1ORDER BY NEWID()
View ArticleAnswer by Deep for Select n random rows from SQL Server table
This works for me:SELECT * FROM table_nameORDER BY RANDOM()LIMIT [number]
View ArticleAnswer by Rob Boek for Select n random rows from SQL Server table
newid()/order by will work, but will be very expensive for large result sets because it has to generate an id for every row, and then sort them.TABLESAMPLE() is good from a performance standpoint, but...
View ArticleAnswer by Patrick Taylor for Select n random rows from SQL Server table
Depending on your needs, TABLESAMPLE will get you nearly as random and better performance.this is available on MS SQL server 2005 and later. TABLESAMPLE will return data from random pages instead of...
View ArticleAnswer by Daniel Brückner for Select n random rows from SQL Server table
Just order the table by a random number and obtain the first 5,000 rows using TOP.SELECT TOP 5000 * FROM [Table] ORDER BY newid();UPDATEJust tried it and a newid() call is sufficent - no need for all...
View ArticleAnswer by Jeff Ferland for Select n random rows from SQL Server table
In MySQL you can do this:SELECT `PRIMARY_KEY`, rand() FROM table ORDER BY rand() LIMIT 5000;
View ArticleAnswer by Ralph Shillington for Select n random rows from SQL Server table
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]...
View ArticleSelect n random rows from SQL Server table
I've got a SQL Server table with about 50,000 rows in it. I want to select about 5,000 of those rows at random. I've thought of a complicated way, creating a temp table with a "random number" column,...
View ArticleAnswer by Shahriar Khazaei for Select n random rows from SQL Server table
In SQl Server 2017 and later you can use SAMPLETABLE phrase in your code like this :SELECT * FROM TableName TABLESAMPLE (100 ROWS)orSELECT * FROM TableName TABLESAMPLE (10 PERCENT)
View Article