Coder Perfect

In Sqlite, how do I get the first/top row of a table using a SQL query?

Problem

In a Sqlite database, I need to get the first/top row of a table.

However, for the query I’m running, my software throws a SQLException “Sqlite Syntax Error: Syntax error near ‘1’ “:

SELECT TOP 1 * 
FROM SAMPLE_TABLE

That appears to be a syntax for MS SQL SERVER and MS ACCESS. Right now, I’m utilizing.

SELECT *
FROM SAMPLE_TABLE
LIMIT 1

What is the most effective solution to this issue?

Asked by Omayr

Solution #1

Use this query to get started:

SELECT * FROM SAMPLE_TABLE ORDER BY ROWID ASC LIMIT 1

Sqlite’s row id references are described in depth here.

Answered by Achim

Solution #2

LIMIT 1 is what you’re looking for. Keep in mind that regardless of the order, this returns the first entry in the result set (unless you specify an order clause in an outer query).

Answered by Jordan Parmer

Post is based on https://stackoverflow.com/questions/5408201/how-to-get-first-top-row-of-the-table-in-sqlite-via-sql-query