LIMIT vs TOP vs FETCH FIRST syntax in SQL - Performance Comparison
When we use LIMIT, TOP, or FETCH FIRST in SQL, we ask the database to return only a small part of the data.
We want to know how this affects the time it takes to get results as the data grows.
Analyze the time complexity of these queries that limit output rows.
-- Using LIMIT (common in MySQL, PostgreSQL)
SELECT * FROM employees ORDER BY salary DESC LIMIT 10;
-- Using TOP (common in SQL Server)
SELECT TOP 10 * FROM employees ORDER BY salary DESC;
-- Using FETCH FIRST (SQL standard)
SELECT * FROM employees ORDER BY salary DESC FETCH FIRST 10 ROWS ONLY;
Each query returns only the top 10 employees by salary from a large table.
Look at what the database does repeatedly to answer these queries.
- Primary operation: Scanning and sorting the employee rows by salary.
- How many times: Once over all rows to order them before picking the top 10.
The database must look at all rows to find the top 10 salaries.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 rows scanned and sorted |
| 100 | About 100 rows scanned and sorted |
| 1000 | About 1000 rows scanned and sorted |
Pattern observation: As the number of rows grows, the work grows roughly in proportion to the number of rows.
Time Complexity: O(n log n)
This means the time grows a bit faster than the number of rows because sorting takes extra steps.
[X] Wrong: "Using LIMIT or TOP means the database only looks at 10 rows, so it's always very fast."
[OK] Correct: The database usually must check all rows to find the top 10, so the work depends on the total number of rows, not just 10.
Understanding how limiting rows affects query time helps you explain database behavior clearly and shows you know how data size impacts performance.
"What if the employees table already has an index on salary? How would that change the time complexity when using LIMIT or TOP?"
Practice
Solution
Step 1: Identify the database systems mentioned
MySQL and PostgreSQL use theLIMITclause to restrict rows.Step 2: Match the clause to the database
TOPis for SQL Server,FETCH FIRSTis for Oracle and others,ROWNUMis Oracle-specific but older style.Final Answer:
LIMIT -> Option AQuick Check:
MySQL/PostgreSQL use LIMIT [OK]
- Confusing TOP with LIMIT
- Using FETCH FIRST in MySQL
- Using ROWNUM in PostgreSQL
Solution
Step 1: Identify SQL Server syntax for limiting rows
SQL Server usesTOP nafter SELECT to limit rows.Step 2: Check each option
SELECT * FROM table LIMIT 5; uses LIMIT (MySQL/PostgreSQL), SELECT * FROM table FETCH FIRST 5 ROWS ONLY; uses FETCH FIRST (Oracle), SELECT * FROM table WHERE ROWNUM <= 5; uses ROWNUM (Oracle). Only SELECT TOP 5 * FROM table; is correct for SQL Server.Final Answer:
SELECT TOP 5 * FROM table; -> Option DQuick Check:
SQL Server uses TOP [OK]
- Using LIMIT in SQL Server
- Placing TOP after FROM
- Using FETCH FIRST in SQL Server
SELECT * FROM employees FETCH FIRST 3 ROWS ONLY;
What will be the result?
Solution
Step 1: Understand FETCH FIRST in Oracle
Oracle supportsFETCH FIRST n ROWS ONLYto limit rows returned.Step 2: Determine query behavior
The query returns exactly the first 3 rows from the employees table as ordered by the query (default order if none specified).Final Answer:
Returns the first 3 rows from employees -> Option BQuick Check:
Oracle FETCH FIRST limits rows [OK]
- Thinking FETCH FIRST causes syntax error in Oracle
- Assuming FETCH FIRST returns random rows
- Believing FETCH FIRST is ignored
SELECT * FROM customers FETCH FIRST 10 ROWS ONLY;
Solution
Step 1: Check FETCH FIRST support in SQL Server
SQL Server does not supportFETCH FIRST; it usesTOPinstead.Step 2: Analyze other options
Missing ORDER BY clause with FETCH FIRST is incorrect because FETCH FIRST requires ORDER BY in some databases but SQL Server does not support FETCH FIRST at all. Should use LIMIT instead of FETCH FIRST is wrong because SQL Server uses TOP, not LIMIT. Syntax is correct, no error is false because syntax is invalid.Final Answer:
FETCH FIRST is not supported in SQL Server -> Option CQuick Check:
SQL Server uses TOP, not FETCH FIRST [OK]
- Using FETCH FIRST in SQL Server
- Confusing LIMIT with FETCH FIRST
- Assuming FETCH FIRST works without ORDER BY
sales in a database that supports both FETCH FIRST and LIMIT. Which query is correct and why?Options:
A)
SELECT * FROM sales LIMIT 10;B)
SELECT * FROM sales FETCH FIRST 10 ROWS ONLY;C)
SELECT TOP 10 * FROM sales;D)
SELECT * FROM sales WHERE ROWNUM <= 10;Solution
Step 1: Understand the database supports both FETCH FIRST and LIMIT
Some modern databases like DB2 or Oracle 12c+ supportFETCH FIRST, while others like MySQL useLIMIT.Step 2: Choose the correct syntax for the database
If the database supports both,FETCH FIRSTis the ANSI standard and preferred for portability.LIMITis not standard SQL.TOPis SQL Server specific, andROWNUMis Oracle-specific older syntax.Final Answer:
SELECT * FROM sales FETCH FIRST 10 ROWS ONLY; -> Option AQuick Check:
FETCH FIRST is ANSI standard for limiting rows [OK]
- Using TOP in non-SQL Server databases
- Assuming LIMIT is standard SQL
- Using ROWNUM outside Oracle
Ready to prove it?
Pass the test and this pattern moves unknown
