How to get first/top row of the table in Sqlite via Sql Query

SqlSqlite

Sql Problem Overview


I need to fetch the first/top row of a table in a Sqlite database.

But my program throws an SQLException "Sqlite Syntax Error: Syntax error near '1' " for the query that I am using:

SELECT TOP 1 * 
FROM SAMPLE_TABLE

That I guess is a syntax particularly for MS SQL SERVER and MS ACCESS. Right now I am using.

SELECT *
FROM SAMPLE_TABLE
LIMIT 1

What is the best solution for this problem?

Sql Solutions


Solution 1 - Sql

Use the following query:

SELECT * FROM SAMPLE_TABLE ORDER BY ROWID ASC LIMIT 1

Note: Sqlite's row id references are detailed here.

Solution 2 - Sql

LIMIT 1 is what you want. Just keep in mind this returns the first record in the result set regardless of order (unless you specify an order clause in an outer query).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOmayrView Question on Stackoverflow
Solution 1 - SqlAchimView Answer on Stackoverflow
Solution 2 - SqlJordan ParmerView Answer on Stackoverflow