Example 1
Return the first two rows in sorted order
A row limit is only meaningful when the query also defines an order, otherwise the chosen rows may vary.
Engine-specific syntax
Setup
CREATE TABLE articles (id INT, title VARCHAR(50));
INSERT INTO
articles (id, title)
VALUES
(1, 'Alpha'),
(2, 'Bravo'),
(3, 'Charlie');SQL
SELECT
id,
title
FROM
articles
ORDER BY
id
LIMIT
2;| id | title |
|---|---|
| 1 | Alpha |
| 2 | Bravo |
Engine-specific syntax
Setup
CREATE TABLE articles (id INT, title VARCHAR(50));
INSERT INTO
articles (id, title)
VALUES
(1, 'Alpha'),
(2, 'Bravo'),
(3, 'Charlie');SQL
SELECT
TOP 2 id,
title
FROM
articles
ORDER BY
id;| id | title |
|---|---|
| 1 | Alpha |
| 2 | Bravo |
The output is the same, but the query syntax is materially different on SQL Server.