Reputation: 61187
I have tried this which did not work.
select top 5 * from [Table_Name]
Upvotes: 391
Views: 303329
Reputation: 7889
The selected answer works, EXCEPT when you need to add a where clause. If you need a where clause and still need to limit that where clause to the top 5 records, then you need to create a subselect like the following.
Why? because in my case the where-clause excludes some of the top 5 records. You still get a limit of 5 records, but they won't be the first 5 records.
SELECT sum(quantity) as total
FROM (
SELECT quantity, weight
FROM mytable
order by id
limit 5
) AS T
where weight > 2
Upvotes: 1
Reputation: 2393
Replace only YOUR table name[TABLE_NAME] and use.
"select * from "+TABLE_NAME+ " LIMIT " +5;
Upvotes: -2
Reputation: 2019
select price from mobile_sales_details order by price desc limit 5
Note: i have mobile_sales_details table
syntax
select column_name from table_name order by column_name desc limit size.
if you need top low price just remove the keyword desc from order by
Upvotes: 41
Reputation: 9252
An equivalent statement would be
select * from [TableName] limit 5
http://www.w3schools.com/sql/sql_top.asp
Upvotes: 48
Reputation: 32094
TOP and square brackets are specific to Transact-SQL. In ANSI SQL one uses LIMIT and backticks (`).
select * from `Table_Name` LIMIT 5;
Upvotes: 32