SQL LIMIT / TOP

LIMIT and TOP are used to restrict the number of rows returned by a SELECT query.

LIMIT → used in MySQL, PostgreSQL, SQLite
TOP → used in Microsoft SQL Server

Syntax: TOP in Microsoft SQL Server

SELECT TOP number column1, column2
FROM table_name;

Example: Get First 2 Rows

SELECT TOP 2 *
FROM Orders;

Example: Highest Price (TOP with ORDER BY)

SELECT TOP 1 *
FROM Orders
ORDER BY Price DESC;

Example: Returns a percentage of rows.(TOP with PERCENT)

SELECT TOP 50 PERCENT *
FROM Orders;

LIMIT (MySQL Syntax)

SELECT column1, column2
FROM table_name
LIMIT number;

Example:

SELECT *
FROM Orders
LIMIT 2;


Simple Interview Definition:
TOP (SQL Server) or LIMIT (MySQL) is used to restrict the number of rows returned by a SELECT query.


Topics