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.
SELECT columns FROM table LIMIT number; SELECT TOP number columns FROM table; SELECT columns FROM table FETCH FIRST number ROWS ONLY;
SELECT * FROM employees LIMIT 5;
SELECT TOP 3 name, salary FROM employees;
SELECT * FROM employees FETCH FIRST 4 ROWS ONLY;
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;
LIMIT clause to restrict rows.TOP is for SQL Server, FETCH FIRST is for Oracle and others, ROWNUM is Oracle-specific but older style.TOP n after SELECT to limit rows.SELECT * FROM employees FETCH FIRST 3 ROWS ONLY;
FETCH FIRST n ROWS ONLY to limit rows returned.SELECT * FROM customers FETCH FIRST 10 ROWS ONLY;
FETCH FIRST; it uses TOP instead.sales in a database that supports both FETCH FIRST and LIMIT. Which query is correct and why?SELECT * FROM sales LIMIT 10;SELECT * FROM sales FETCH FIRST 10 ROWS ONLY;SELECT TOP 10 * FROM sales;SELECT * FROM sales WHERE ROWNUM <= 10;FETCH FIRST, while others like MySQL use LIMIT.FETCH FIRST is the ANSI standard and preferred for portability. LIMIT is not standard SQL. TOP is SQL Server specific, and ROWNUM is Oracle-specific older syntax.Pass the test and this pattern moves unknown