Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 947
Ashish Karnavat Avatar asked Nov 18 '25 12:11

Ashish Karnavat


2 Answers

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

like image 168
Ravi Avatar answered Nov 21 '25 01:11

Ravi


The top syntax supports a percent modifier, which you can use:

SELECT TOP 50 PERCENT *
FROM   mytable
like image 45
Mureinik Avatar answered Nov 21 '25 02:11

Mureinik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!