Search code examples
sqlsql-serverselect

How to get 50% records from a table in SQL Server?


Suppose I have a table with 1000 rows and I want 50% of it in the output. How can I do that? Does it have any in-built function?


Solution

  • Use :

    SELECT 
        TOP 50 PERCENT * 
    FROM 
        Table1;
    

    with Row_number

    SELECT 
            TOP 50 PERCENT Row_Number() over (order by Column1) ,* 
        FROM 
            Table1;
    

    Note: Row_number should have a over clause with order by column or partition by columns