Example 1
Take the two newest rows in a deterministic order
The query returns Bravo and Charlie because they are the two newest posts by published_at. The ORDER BY makes the limit meaningful and repeatable. Without that ordering, the database would be free to return any two rows.
CREATE TABLE posts (
id INT,
title VARCHAR(50),
published_at VARCHAR(19)
);
INSERT INTO
posts (id, title, published_at)
VALUES
(1, 'Alpha', '2026-04-01 09:00:00'),
(2, 'Bravo', '2026-04-03 09:00:00'),
(3, 'Charlie', '2026-04-02 09:00:00');SELECT
id,
title
FROM
posts
ORDER BY
published_at DESC,
id DESC
LIMIT
2;| id | title |
|---|---|
| 2 | Bravo |
| 3 | Charlie |
CREATE TABLE posts (
id INT,
title VARCHAR(50),
published_at VARCHAR(19)
);
INSERT INTO
posts (id, title, published_at)
VALUES
(1, 'Alpha', '2026-04-01 09:00:00'),
(2, 'Bravo', '2026-04-03 09:00:00'),
(3, 'Charlie', '2026-04-02 09:00:00');SELECT
TOP 2 id,
title
FROM
posts
ORDER BY
published_at DESC,
id DESC;| id | title |
|---|---|
| 2 | Bravo |
| 3 | Charlie |
The result is the same across engines, but SQL Server uses `TOP` instead of `LIMIT`.