395

I have tried this which did not work.

select top 5 * from [Table_Name] 
1
  • the meaning of TOP x records makes no sense applied to an entire table or select, for that SQL uses ORDER BY and LIMIT as pointed in all answers here. TOP x records makes sense when talking about groups or "windows". This has been introduced in sqlite in 3.25.0 (2018) sqlite.org/windowfunctions.html. A very simple example of that. Selecting the top orders by date could be done with the SQL: "SELECT date, desc, row_number() OVER (PARTITION BY date ORDER BY amount DESC) AS topn", when we build a new field topn that will have the values 1, 2, 3 etc for the top orders of each date.... Commented Jan 18, 2023 at 20:03

8 Answers 8

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

Comments

50

An equivalent statement would be

select * from [TableName] limit 5 

http://www.w3schools.com/sql/sql_top.asp

Comments

41
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

Comments

32
select * from [Table_Name] limit 5 

Comments

32

TOP and square brackets are specific to Transact-SQL. In ANSI SQL one uses LIMIT and backticks (`).

select * from `Table_Name` LIMIT 5; 

1 Comment

LIMIT is not ANSI SQL. The ANSI SQL way is FETCH FIRST 5 ROWS ONLY. Also double quotes are used for delimited identifiers, e.g. "Table_Name".
8
Select TableName.* from TableName DESC LIMIT 5 

1 Comment

Improper use of DESC. Has no effect.
2

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 

1 Comment

I did this and still worked: SELECT * FROM TABLE_1 WHERE COLUMN_A = '1' LIMIT 10;
-2

Replace only YOUR table name[TABLE_NAME] and use.

"select * from "+TABLE_NAME+ " LIMIT " +5; 

1 Comment

This suffers from SQL injection easily if either the table name or the limit come from untrusted sources

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.