Amitabh
Amitabh

Reputation: 61187

How to get Top 5 records in SqLite?

I have tried this which did not work.

select top 5 * from [Table_Name]

Upvotes: 391

Views: 303329

Answers (8)

panofish
panofish

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

Android
Android

Reputation: 2393

Replace only YOUR table name[TABLE_NAME] and use.

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

Upvotes: -2

SGDemo
SGDemo

Reputation: 129

Select TableName.* from  TableName DESC LIMIT 5

Upvotes: 8

Bharathiraja
Bharathiraja

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

Nix
Nix

Reputation: 58522

SELECT * FROM Table_Name LIMIT 5;

Upvotes: 690

Chris J
Chris J

Reputation: 9252

An equivalent statement would be

select * from [TableName] limit 5

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

Upvotes: 48

newtover
newtover

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

YOU
YOU

Reputation: 123831

select * from [Table_Name] limit 5

Upvotes: 32

Related Questions