0

I wanted to set the number rows returned per query say 5. How can I set this in the sql query. Can any one please help me

1
  • 2
    Which DBMS ? MySQL ? MSSQL ? Oracle ? Commented Jun 6, 2014 at 7:51

4 Answers 4

1

Highly dependent on what RDBMS you are using.

For Oracle

 SELECT * FROM the_table WHERE ROWNUM < 6

(since 12c there is another option, too).

For Postgresql

 SELECT * FROM the_table LIMIT 5
Sign up to request clarification or add additional context in comments.

1 Comment

And Also I want this for pagination
1

According to the MySQL manual you can do this by adding LIMIT statement to your query:

SELECT * FROM tbl_name
LIMIT offset, row_numbers

or

SELECT * FROM tbl_name
LIMIT row_numbers OFFSET offset

offset option is very useful in case of pagination.

Comments

0
SELECT TOP 5 *
FROM dbo.MyTable

Comments

0

As someone suggest zou can use:

select top X from table_name

where X is the numer of rows that you want

or you can use row_number

With cte AS 
( SELECT *, 
ROW_NUMBER() OVER (order by table_column) as RowNumber  
FROM table_name) 
select * 
from cte
Where RowNumber <= 5

or even:

With cte AS 
( SELECT *, 
ROW_NUMBER() OVER (order by table_column) as RowNumber  
FROM table_name) 
select * 
from cte
Where RowNumber between 5 and 10

1 Comment

In tha case when we are talking about MSSQL

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.