Bird
Raised Fist0
SQLquery~10 mins

TOP vs LIMIT across databases in SQL - Visual Side-by-Side Comparison

Choose your learning style10 modes available
Concept Flow - TOP vs LIMIT across databases
Write SELECT query
↓
Choose database
↓
If SQL Server or MS Access
↓
Use TOP N
↓
If MySQL, PostgreSQL, SQLite
↓
Use LIMIT N
↓
Execute query
↓
Get top N rows as result
The flow shows how to write a query to get top N rows, choosing TOP for SQL Server and LIMIT for MySQL/PostgreSQL/SQLite.
Execution Sample
SQL
SELECT TOP 3 * FROM Employees ORDER BY Salary DESC;

SELECT * FROM Employees ORDER BY Salary DESC LIMIT 3;
Two queries that get the top 3 highest paid employees, one using TOP (SQL Server) and one using LIMIT (MySQL/PostgreSQL).
Execution Table
StepQuery TypeDatabaseSyntax UsedActionResult Rows
1Select top N rowsSQL ServerTOP 3Select top 3 rows ordered by Salary descending3 rows with highest Salary
2Select top N rowsMySQL/PostgreSQLLIMIT 3Select rows ordered by Salary descending, limit to 33 rows with highest Salary
3Select top N rowsSQLiteLIMIT 3Select rows ordered by Salary descending, limit to 33 rows with highest Salary
4Select top N rowsOracleROWNUM or FETCH FIRSTUse ROWNUM or FETCH FIRST 3 ROWS ONLY3 rows with highest Salary
5ExitAllN/AQuery executed and results returnedTop N rows returned
💡 Execution stops after returning the top N rows as per database syntax.
Variable Tracker
VariableStartAfter SQL Server QueryAfter MySQL/PostgreSQL QueryAfter SQLite QueryAfter Oracle Query
Result RowsEmpty3 highest Salary rows3 highest Salary rows3 highest Salary rows3 highest Salary rows
Key Moments - 3 Insights
Why can't I use TOP in MySQL or PostgreSQL?
TOP is specific to SQL Server and some others; MySQL and PostgreSQL use LIMIT instead, as shown in execution_table rows 1 and 2.
Can I use LIMIT without ORDER BY to get top rows?
Without ORDER BY, LIMIT just returns any N rows, not necessarily the top ones. Ordering is needed to define 'top'. See execution_table rows 1 and 2 where ORDER BY Salary DESC is used.
How does Oracle get top N rows if it doesn't support TOP or LIMIT?
Oracle uses ROWNUM or FETCH FIRST syntax to limit rows, 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 get top N rows?
ALIMIT N
BTOP N
CROWNUM
DFETCH FIRST N ROWS ONLY
💡 Hint
Check execution_table row 1 under Syntax Used column.
At which step in the execution_table does the query for MySQL/PostgreSQL appear?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look for MySQL/PostgreSQL in the Database column.
If you remove ORDER BY from the queries, what changes in the result rows according to variable_tracker?
AQuery will fail with syntax error
BResult rows remain the same, top salaries returned
CResult rows become random N rows, not necessarily top salaries
DResult rows will be empty
💡 Hint
Refer to key_moments question 2 explanation about ORDER BY necessity.
Concept Snapshot
TOP vs LIMIT in SQL:
- SQL Server uses: SELECT TOP N ...
- MySQL/PostgreSQL/SQLite use: SELECT ... LIMIT N
- Oracle uses: ROWNUM or FETCH FIRST N ROWS ONLY
- ORDER BY needed to define 'top' rows
- Syntax differs by database, but goal is same: limit rows returned
Full Transcript
This visual execution compares how different databases limit query results to top N rows. SQL Server uses TOP N syntax, while MySQL, PostgreSQL, and SQLite use LIMIT N. Oracle uses ROWNUM or FETCH FIRST syntax. The queries must include ORDER BY to get meaningful top rows. The execution table shows each step with syntax and results. Variable tracking shows the result rows after each query. Key moments clarify common confusions about syntax differences and the importance of ORDER BY. The quiz tests understanding of which syntax belongs to which database and the effect of removing ORDER BY. The snapshot summarizes the key syntax differences and usage rules.

Practice

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

Solution

  1. Step 1: Understand SQL Server syntax

    SQL Server uses the TOP clause immediately after SELECT to limit rows.
  2. Step 2: Compare with other databases

    Other databases like MySQL and PostgreSQL use LIMIT at the end, not SQL Server.
  3. Final Answer:

    TOP -> Option B
  4. Quick Check:

    SQL Server = TOP [OK]
Hint: Use TOP right after SELECT in SQL Server [OK]
Common Mistakes:
  • Using LIMIT in SQL Server
  • Placing TOP at the end of the query
  • Confusing FETCH FIRST with TOP
2. Which of the following is the correct syntax to get the first 5 rows in MySQL?
easy
A. SELECT * FROM table_name LIMIT 5;
B. SELECT TOP 5 * FROM table_name;
C. SELECT * FROM table_name FETCH FIRST 5 ROWS;
D. SELECT * FROM table_name WHERE ROWNUM <= 5;

Solution

  1. Step 1: Recall MySQL syntax for limiting rows

    MySQL uses LIMIT at the end of the query to restrict rows.
  2. Step 2: Check each option

    SELECT * FROM table_name LIMIT 5; uses LIMIT 5 correctly; SELECT TOP 5 * FROM table_name; uses SQL Server syntax; Options C and D are for other databases.
  3. Final Answer:

    SELECT * FROM table_name LIMIT 5; -> Option A
  4. Quick Check:

    MySQL = LIMIT at end [OK]
Hint: Use LIMIT at the end in MySQL queries [OK]
Common Mistakes:
  • Using TOP in MySQL
  • Placing LIMIT before FROM
  • Using ROWNUM which is Oracle syntax
3. Given the table Employees with columns Id and Name, what will this query return in SQL Server?
SELECT TOP 3 Name FROM Employees ORDER BY Id DESC;
medium
A. The first 3 employees ordered by Id ascending
B. The last 3 employees ordered by Id descending
C. Syntax error due to TOP placement
D. The top 3 employees ordered by Id descending

Solution

  1. Step 1: Understand TOP with ORDER BY in SQL Server

    The query selects the top 3 rows after ordering by Id descending, so highest Ids first.
  2. Step 2: Analyze the result

    The result is the 3 employees with the highest Id values, ordered descending.
  3. Final Answer:

    The top 3 employees ordered by Id descending -> Option D
  4. Quick Check:

    TOP 3 + ORDER BY DESC = highest 3 rows [OK]
Hint: TOP limits rows after ORDER BY sorting [OK]
Common Mistakes:
  • Thinking TOP returns first rows before ORDER BY
  • Confusing ascending and descending order
  • Assuming syntax error with TOP
4. Identify the error in this PostgreSQL query:
SELECT TOP 10 * FROM Customers;
medium
A. TOP is not valid in PostgreSQL
B. Missing ORDER BY clause
C. LIMIT should be placed before FROM
D. Syntax is correct

Solution

  1. Step 1: Recall PostgreSQL syntax for limiting rows

    PostgreSQL uses LIMIT at the end, not TOP.
  2. Step 2: Check the query

    The query uses TOP 10, which is invalid syntax in PostgreSQL and causes an error.
  3. Final Answer:

    TOP is not valid in PostgreSQL -> Option A
  4. Quick Check:

    PostgreSQL = LIMIT, not TOP [OK]
Hint: PostgreSQL uses LIMIT, not TOP [OK]
Common Mistakes:
  • Using TOP in PostgreSQL
  • Placing LIMIT before FROM
  • Assuming ORDER BY is mandatory for LIMIT
5. You want to write a query that returns the top 5 highest salaries from an Employees table. Which query works correctly in both SQL Server and MySQL?
hard
A. Use UNION of SQL Server and MySQL queries
B. SELECT Salary FROM Employees ORDER BY Salary DESC LIMIT 5;
C. SELECT Salary FROM Employees ORDER BY Salary DESC FETCH FIRST 5 ROWS ONLY;
D. SELECT TOP 5 Salary FROM Employees ORDER BY Salary DESC;

Solution

  1. Step 1: Understand syntax differences

    SQL Server uses TOP after SELECT; MySQL uses LIMIT at the end.
  2. Step 2: Check each option for cross-DB compatibility

    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.
  3. Final Answer:

    SELECT Salary FROM Employees ORDER BY Salary DESC FETCH FIRST 5 ROWS ONLY; -> Option C
  4. Quick Check:

    FETCH FIRST = cross-DB standard [OK]
Hint: Use FETCH FIRST ROWS ONLY for cross-DB compatibility [OK]
Common Mistakes:
  • Using LIMIT (MySQL only)
  • Using TOP (SQL Server only)
  • Proposing UNION which fails parsing
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