Thursday, June 13, 2013

Limiting SQL Query Result Set to few number

Hi All,

With TABLESAMPLE keyword we can restrict the Result of the SQL Query, where rows will be selected randomly from the table

For example one table have 10000 Rows, then with following query we will get approximate 10 % of the rows from table

SELECT * FROM [TABLE_NAME] TABLESAMPLE (10 PERCENT)

If the requirement is for the same set of records every time then we have to use REPEATABLE keyword at end with some seed value as shown below

SELECT * FROM [TABLE_NAME] TABLESAMPLE (10 PERCENT) REPEATABLE (205)