Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Select the first 5 rows of any table or a view

Status
Not open for further replies.

lalleima

Programmer
Feb 13, 2003
11
0
0
IN
HI

I'm some what confuse in this little prob.
I wanted to know if we can select the first 5 rows of any table with a single SQL query? If yes then How?

And mind U, it is not Indexed or a ordered View

Thank U for anyone who have read this and I'm open to any kind of sugestion.

Nishi
 
In SQL Server you can use:

Code:
SELECT TOP 5 *
FROM tablename
ORDER BY columnname

I seem to remember this isn't strictly ANSI SQL though. --James
 
the question has meaning only if you have a column that you can sort on

let's say that column is foo --

[tt]select foo
, bar
from yourtable X
where ( select count(*)
from yourtable
where foo > X.foo ) < 5[/tt]

rudy
 
Hi!

You can use ROWNUM :

select *
from your_table
where rownum <= 5;

Best regards,

Yipyeppp..
 
This is an ANSI forum, not Oracle ;-)

But there's a ROW_NUMBER OLAP-function in SQL:1999 (and Oracle)

Dieter
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top