0
0
SQLquery~5 mins

LIMIT vs TOP vs FETCH FIRST syntax in SQL

Choose your learning style9 modes available
Introduction
These commands help you get only a small part of your data instead of everything. This makes it faster and easier to see what you want.
When you want to see just the first few rows of a big table.
When you need to show a preview of data on a website or app.
When you want to test a query without loading all data.
When you want to limit results for reports or summaries.
Syntax
SQL
SELECT columns FROM table LIMIT number;

SELECT TOP number columns FROM table;

SELECT columns FROM table FETCH FIRST number ROWS ONLY;
LIMIT is used mostly in MySQL, PostgreSQL, and SQLite.
TOP is used in Microsoft SQL Server.
FETCH FIRST is part of the SQL standard and used in Oracle, DB2, and newer versions of PostgreSQL.
Examples
Shows the first 5 rows from the employees table using LIMIT.
SQL
SELECT * FROM employees LIMIT 5;
Shows the top 3 employees with their name and salary using TOP.
SQL
SELECT TOP 3 name, salary FROM employees;
Shows the first 4 rows from employees using FETCH FIRST syntax.
SQL
SELECT * FROM employees FETCH FIRST 4 ROWS ONLY;
Sample Program
This creates a small employees table, adds 5 rows, then selects the first 3 rows using LIMIT.
SQL
CREATE TABLE employees (id INT, name VARCHAR(20), salary INT);
INSERT INTO employees VALUES (1, 'Alice', 5000), (2, 'Bob', 6000), (3, 'Carol', 5500), (4, 'Dave', 7000), (5, 'Eve', 6500);

-- Using LIMIT
SELECT * FROM employees LIMIT 3;
OutputSuccess
Important Notes
LIMIT and FETCH FIRST both limit rows but syntax and support vary by database.
TOP is placed right after SELECT, LIMIT and FETCH FIRST come at the end of the query.
Always check your database's documentation to use the right syntax.
Summary
LIMIT, TOP, and FETCH FIRST all limit how many rows you get back.
Use LIMIT in MySQL/PostgreSQL, TOP in SQL Server, FETCH FIRST in Oracle and some others.
They help make queries faster and results easier to handle.