378

I have tried this which did not work.

select top 5 * from [Table_Name]
Antonio Bakula
  • 20,445
  • 6
  • 75
  • 102
Amitabh
  • 59,111
  • 42
  • 110
  • 159
  • 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) https://www.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.... – Alejadro Xalabarder Jan 18 '23 at 20:03

8 Answers8

671
SELECT * FROM Table_Name LIMIT 5;
Nix
  • 57,072
  • 29
  • 149
  • 198
48

An equivalent statement would be

select * from [TableName] limit 5

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

Chris J
  • 9,164
  • 7
  • 40
  • 39
37
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

Bharathiraja
  • 1,949
  • 20
  • 19
32
select * from [Table_Name] limit 5
YOU
  • 120,166
  • 34
  • 186
  • 219
32

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

select * from `Table_Name` LIMIT 5;
newtover
  • 31,286
  • 11
  • 84
  • 89
  • 9
    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". – jarlh Jun 09 '15 at 10:53
8
Select TableName.* from  TableName DESC LIMIT 5
JKennedy
  • 18,150
  • 17
  • 114
  • 198
SGDemo
  • 129
  • 3
  • 11
1

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
panofish
  • 7,578
  • 13
  • 55
  • 96
-2

Replace only YOUR table name[TABLE_NAME] and use.

"select * from "+TABLE_NAME+ " LIMIT " +5;
Android
  • 2,383
  • 1
  • 26
  • 44