Wednesday 18 February 2015

Return Random Records From SQL Server Table


Suppose you have a table in SQL Server as in the following:

table
                                                                  Image 1.

You want to get 5 random records on every select request. Then use the following SQL Statement:
  1. SELECT TOP 5 * FROM Employee ORDER BY NEWID()  
Now see the result:

Result
                                                                  Image 2.

Now again execute the query:

Execute your Query
                                                                   Image 3.

output
                                                                        Image 4.

No comments:

Post a Comment