Introduction
We use TOP or LIMIT to get only a few rows from a big list. This helps us see just what we need quickly.
We use TOP or LIMIT to get only a few rows from a big list. This helps us see just what we need quickly.
SELECT TOP number column_names FROM table_name; -- Used in SQL Server, MS Access SELECT column_names FROM table_name LIMIT number; -- Used in MySQL, PostgreSQL, SQLite
TOP is placed right after SELECT.
LIMIT is placed at the end of the query.
SELECT TOP 3 name FROM employees;
SELECT name FROM employees LIMIT 3;
SELECT TOP 5 * FROM orders ORDER BY order_date DESC;
SELECT * FROM orders ORDER BY order_date DESC LIMIT 5;
This example creates a small table of employees and then selects the first 2 names using LIMIT.
CREATE TABLE employees (id INT, name VARCHAR(20)); INSERT INTO employees VALUES (1, 'Alice'), (2, 'Bob'), (3, 'Carol'), (4, 'Dave'); -- Using LIMIT (MySQL/PostgreSQL/SQLite style) SELECT name FROM employees LIMIT 2;
TOP is mostly used in Microsoft SQL Server and MS Access.
LIMIT is common in MySQL, PostgreSQL, and SQLite.
Some databases like Oracle use different methods (e.g., ROWNUM) to limit rows.
Use TOP right after SELECT in SQL Server to limit rows.
Use LIMIT at the end of the query in MySQL and PostgreSQL.
Both help you get a small part of data quickly.
TOP clause immediately after SELECT to limit rows.LIMIT at the end, not SQL Server.LIMIT at the end of the query to restrict rows.LIMIT 5 correctly; SELECT TOP 5 * FROM table_name; uses SQL Server syntax; Options C and D are for other databases.Employees with columns Id and Name, what will this query return in SQL Server?SELECT TOP 3 Name FROM Employees ORDER BY Id DESC;
SELECT TOP 10 * FROM Customers;
LIMIT at the end, not TOP.TOP 10, which is invalid syntax in PostgreSQL and causes an error.Employees table. Which query works correctly in both SQL Server and MySQL?TOP after SELECT; MySQL uses LIMIT at the end.SELECT TOP 5 Salary FROM Employees ORDER BY Salary DESC; works only in SQL Server; SELECT Salary FROM Employees ORDER BY Salary DESC LIMIT 5; only in MySQL; Use UNION of SQL Server and MySQL queries doesn't provide a single query that parses in both; SELECT Salary FROM Employees ORDER BY Salary DESC FETCH FIRST 5 ROWS ONLY; is standard SQL supported by both.Pass the test and this pattern moves unknown