sqlcmd.net validated sql reference
beginner pagination MySQL MariaDB SQL Server PostgreSQL SQLite

Limit Returned Rows

Return only part of a result set using the row-limiting syntax each engine supports.

Docker-validated Not currently validation-green

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.

MySQL MariaDB PostgreSQL SQLite
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;
idtitle
1Alpha
2Bravo
SQL Server
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;
idtitle
1Alpha
2Bravo

The output is the same, but the query syntax is materially different on SQL Server.

Where this command helps.

  • showing only the first few rows in a UI
  • sampling a result set before working on the full query

What the command is doing.

Row limiting is a common area where engines diverge. MySQL, MariaDB, and PostgreSQL use LIMIT, while SQL Server commonly uses TOP for a basic query like this.