Bird
Raised Fist0
SQLquery~10 mins

LIMIT vs TOP vs FETCH FIRST syntax in SQL - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - LIMIT vs TOP vs FETCH FIRST syntax
Start Query
↓
Check SQL Dialect
↓
Use TOP
↓
Apply Row Limit
↓
Return Limited Rows
↓
End
The query starts, checks which SQL dialect is used, then applies the appropriate syntax (TOP, LIMIT, or FETCH FIRST) to limit the number of rows returned.
Execution Sample
SQL
SELECT * FROM employees LIMIT 3;
This query returns the first 3 rows from the employees table using LIMIT syntax.
Execution Table
StepSQL DialectSyntax UsedActionRows Returned
1MySQL/PostgreSQLLIMIT 3Apply LIMIT clauseFirst 3 rows
2SQL ServerTOP 3Apply TOP clauseFirst 3 rows
3Oracle/DB2FETCH FIRST 3 ROWS ONLYApply FETCH FIRST clauseFirst 3 rows
4AnyN/ANo limit clauseAll rows
5EndN/AQuery execution completeRows returned as per limit
💡 Execution stops after returning the limited number of rows based on the syntax used.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
Rows to ReturnAll3 (LIMIT)3 (TOP)3 (FETCH FIRST)3
Key Moments - 3 Insights
Why do we use different syntax like LIMIT, TOP, or FETCH FIRST?
Different SQL databases support different syntax for limiting rows. For example, MySQL uses LIMIT, SQL Server uses TOP, and Oracle uses FETCH FIRST. See execution_table rows 1-3.
Does LIMIT 3 and TOP 3 return the same rows?
Yes, both limit the output to the first 3 rows, but the syntax depends on the database. Refer to execution_table rows 1 and 2.
What happens if no limit syntax is used?
All rows from the table are returned without any limit, as shown in execution_table row 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, which syntax is used in SQL Server to limit rows?
ATOP
BFETCH FIRST
CLIMIT
DWHERE
💡 Hint
Check execution_table row 2 under 'Syntax Used' column.
At which step does the query return all rows without any limit?
AStep 1
BStep 4
CStep 3
DStep 5
💡 Hint
Look at execution_table row 4 where 'No limit clause' is applied.
If you want to limit rows in Oracle, which syntax should you use?
ATOP 3
BLIMIT 3
CFETCH FIRST 3 ROWS ONLY
DROWNUM <= 3
💡 Hint
Refer to execution_table row 3 under 'Syntax Used' for Oracle/DB2.
Concept Snapshot
LIMIT vs TOP vs FETCH FIRST syntax:
- LIMIT n: Used in MySQL/PostgreSQL to return first n rows.
- TOP n: Used in SQL Server to return first n rows.
- FETCH FIRST n ROWS ONLY: Used in Oracle/DB2 for limiting rows.
- All limit the number of rows returned by a query.
- Syntax depends on the SQL database dialect.
Full Transcript
This visual execution shows how SQL queries limit the number of rows returned using different syntax depending on the database. The flow starts by checking the SQL dialect, then applies the appropriate syntax: LIMIT for MySQL/PostgreSQL, TOP for SQL Server, and FETCH FIRST for Oracle/DB2. The execution table traces these steps and shows the rows returned at each step. Variables track how the number of rows to return changes from all rows to the limited number. Key moments clarify why different syntax exists and what happens without limits. The quiz tests understanding of which syntax applies to which database and when all rows are returned. The snapshot summarizes the key points for quick reference.

Practice

(1/5)
1. Which SQL clause is used to limit the number of rows returned in a query in MySQL and PostgreSQL?
easy
A. LIMIT
B. TOP
C. FETCH FIRST
D. ROWNUM

Solution

  1. Step 1: Identify the database systems mentioned

    MySQL and PostgreSQL use the LIMIT clause to restrict rows.
  2. Step 2: Match the clause to the database

    TOP is for SQL Server, FETCH FIRST is for Oracle and others, ROWNUM is Oracle-specific but older style.
  3. Final Answer:

    LIMIT -> Option A
  4. Quick Check:

    MySQL/PostgreSQL use LIMIT [OK]
Hint: MySQL/PostgreSQL use LIMIT to restrict rows [OK]
Common Mistakes:
  • Confusing TOP with LIMIT
  • Using FETCH FIRST in MySQL
  • Using ROWNUM in PostgreSQL
2. Which of the following is the correct syntax to get the top 5 rows in SQL Server?
easy
A. SELECT * FROM table WHERE ROWNUM <= 5;
B. SELECT * FROM table LIMIT 5;
C. SELECT * FROM table FETCH FIRST 5 ROWS ONLY;
D. SELECT TOP 5 * FROM table;

Solution

  1. Step 1: Identify SQL Server syntax for limiting rows

    SQL Server uses TOP n after SELECT to limit rows.
  2. 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.
  3. Final Answer:

    SELECT TOP 5 * FROM table; -> Option D
  4. Quick Check:

    SQL Server uses TOP [OK]
Hint: TOP goes right after SELECT in SQL Server [OK]
Common Mistakes:
  • Using LIMIT in SQL Server
  • Placing TOP after FROM
  • Using FETCH FIRST in SQL Server
3. Given this query in Oracle:
SELECT * FROM employees FETCH FIRST 3 ROWS ONLY;

What will be the result?
medium
A. Syntax error because FETCH FIRST is not valid in Oracle
B. Returns the first 3 rows from employees
C. Returns all rows because FETCH FIRST is ignored
D. Returns 3 random rows from employees

Solution

  1. Step 1: Understand FETCH FIRST in Oracle

    Oracle supports FETCH FIRST n ROWS ONLY to limit rows returned.
  2. 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).
  3. Final Answer:

    Returns the first 3 rows from employees -> Option B
  4. Quick Check:

    Oracle FETCH FIRST limits rows [OK]
Hint: FETCH FIRST limits rows in Oracle [OK]
Common Mistakes:
  • Thinking FETCH FIRST causes syntax error in Oracle
  • Assuming FETCH FIRST returns random rows
  • Believing FETCH FIRST is ignored
4. Identify the error in this SQL Server query:
SELECT * FROM customers FETCH FIRST 10 ROWS ONLY;
medium
A. Should use LIMIT instead of FETCH FIRST
B. Missing ORDER BY clause with FETCH FIRST
C. FETCH FIRST is not supported in SQL Server
D. Syntax is correct, no error

Solution

  1. Step 1: Check FETCH FIRST support in SQL Server

    SQL Server does not support FETCH FIRST; it uses TOP instead.
  2. 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.
  3. Final Answer:

    FETCH FIRST is not supported in SQL Server -> Option C
  4. Quick Check:

    SQL Server uses TOP, not FETCH FIRST [OK]
Hint: SQL Server uses TOP, not FETCH FIRST [OK]
Common Mistakes:
  • Using FETCH FIRST in SQL Server
  • Confusing LIMIT with FETCH FIRST
  • Assuming FETCH FIRST works without ORDER BY
5. You want to write a query that returns the top 10 rows from a table named 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;
hard
A. Correct if the database supports LIMIT syntax
B. Correct if the database supports FETCH FIRST syntax
C. Correct only in SQL Server
D. Correct only in Oracle with ROWNUM

Solution

  1. Step 1: Understand the database supports both FETCH FIRST and LIMIT

    Some modern databases like DB2 or Oracle 12c+ support FETCH FIRST, while others like MySQL use LIMIT.
  2. Step 2: Choose the correct syntax for the database

    If the database supports both, 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.
  3. Final Answer:

    SELECT * FROM sales FETCH FIRST 10 ROWS ONLY; -> Option A
  4. Quick Check:

    FETCH FIRST is ANSI standard for limiting rows [OK]
Hint: Use FETCH FIRST for ANSI standard row limiting [OK]
Common Mistakes:
  • Using TOP in non-SQL Server databases
  • Assuming LIMIT is standard SQL
  • Using ROWNUM outside Oracle
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard