AI basics & verificationPractical AI workflows

Why LIMIT 1 does not necessarily return the latest record

LIMIT 1 sets a row count; it does not define which record comes first. When reviewing an AI-generated latest-record query, specify the timestamp field, a consistent time representation and the policy for ties before choosing a limit.

Archive drawers containing rows of paper records
Illustrative photo: Maksym Kaharlytskyi · Unsplash LicenseStock illustration for the topic, not a screenshot, test of the pictured equipment or product recommendation.

Key Takeaways

With the sample’s non-null, fixed-format UTC timestamps and unique integer IDs, ORDER BY occurred_at DESC, event_id DESC LIMIT 1 selects record 30 under the stated rule. LIMIT alone does not guarantee the latest row. Ordering only by time leaves a tie; if all latest-time records matter, retain both 20 and 30.

In this article

Translate latest into an explicit selection rule

Here, latest representative means descending occurred_at, then descending event_id for equal times, retaining one row. Every timestamp uses the same UTC format and precision, timestamps are non-null and integer IDs are unique. Mixed offsets, inconsistent text formats and missing times are outside this sample.

The SQLite ORDER BY documentation states that order is undefined without ORDER BY, and remains undefined between rows equal on every ordering expression. Repeatedly observing the same row does not turn an unstated order into a reliable contract.

Run three queries against three authored rows

On December 2, 2026, the editor executed this complete sample using in-memory SQLite 3.51.3 through Node.js 22.23.2. The events are fictional. No business database or model was queried; AI-generated SQL is the review scenario, not a claim about a captured model response.

Run in a separate SQLite practice database

CREATE TABLE events(event_id INTEGER PRIMARY KEY, occurred_at TEXT NOT NULL);
INSERT INTO events VALUES
 (10,'2026-12-01T09:00:00Z'),
 (20,'2026-12-02T09:00:00Z'),
 (30,'2026-12-02T09:00:00Z');

SELECT event_id FROM events LIMIT 1;

SELECT event_id, occurred_at FROM events
ORDER BY occurred_at DESC, event_id DESC LIMIT 1;

SELECT event_id FROM events
WHERE occurred_at = (SELECT MAX(occurred_at) FROM events)
ORDER BY event_id;
CREATE and INSERT write to the practice database. The observed run used memory only; do not paste this setup into a business database.

Observed selection and its meaning
QueryReturned IDsInterpretation
LIMIT 1 without ORDER BY10An older record happened to appear first
Time DESC, ID DESC, LIMIT 130One representative under the explicit rule
Equal to maximum time, list by ID20, 30All records sharing the latest timestamp

Decide whether a tie may be discarded

Records 20 and 30 both have timestamp 2026-12-02T09:00:00Z. A larger-ID rule can select one stable representative when that is the requirement. If every event at the latest time is required, LIMIT 1 drops relevant data; use the maximum-time filter shown in the sample instead.

A larger ID does not inherently mean a later occurrence, and cannot reconstruct sequence missing from a seconds-resolution timestamp. If actual order matters, inspect a more precise trusted timestamp or a business sequence. Without that evidence, preserve the tie rather than inventing event chronology from IDs.

Verify identity as well as row count

Check the selected ID on the small sample before applying the query to data with the same time and uniqueness assumptions. This query selects one row from the table, not one row per customer, and is not a consistency strategy for changing paginated data. Group selection and concurrent writes need their own requirements.

Use the SQL month-boundary guide for interval filtering, and the numeric-sort contract guide for JavaScript arrays. This article addresses SQL row identity through ordering and tie policy; attractive formatting or the correct row count cannot establish that the right record was chosen.

Further reading & sources