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.
- LIMIT sets quantity and ORDER BY sets order; observed default order is not a time guarantee.Read the supporting section: Translate latest into an explicit selection rule
- The unordered sample returned 10 on this run; explicit two-key ordering selected 30.Read the supporting section: Run three queries against three authored rows
- The larger ID is a declared selection rule, not evidence of a later real-world event.Read the supporting section: Decide whether a tie may be discarded
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.
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;| Query | Returned IDs | Interpretation |
|---|---|---|
| LIMIT 1 without ORDER BY | 10 | An older record happened to appear first |
| Time DESC, ID DESC, LIMIT 1 | 30 | One representative under the explicit rule |
| Equal to maximum time, list by ID | 20, 30 | All 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.


