Bird
Raised Fist0
SQLquery~20 mins

TOP vs LIMIT across databases in SQL - Practice Questions

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of TOP vs LIMIT
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
1:30remaining
Understanding TOP and LIMIT usage

Which statement correctly describes the difference between TOP and LIMIT clauses in SQL?

A<code>TOP</code> and <code>LIMIT</code> are interchangeable and work identically in all SQL databases.
B<code>LIMIT</code> is used only in SQL Server; <code>TOP</code> is used in MySQL and PostgreSQL.
C<code>TOP</code> is used to filter rows based on a condition; <code>LIMIT</code> is used to sort rows.
D<code>TOP</code> is used in SQL Server and returns a specified number of rows; <code>LIMIT</code> is used in MySQL and PostgreSQL for the same purpose.
Attempts:
2 left
💡 Hint

Think about which databases you have heard use TOP and which use LIMIT.

❓ query_result
intermediate
1:30remaining
Result of TOP vs LIMIT queries

Given a table Employees with 5 rows, what will be the output of these two queries?

-- Query 1 (SQL Server):
SELECT TOP 3 * FROM Employees ORDER BY EmployeeID;

-- Query 2 (MySQL):
SELECT * FROM Employees ORDER BY EmployeeID LIMIT 3;

What is true about their outputs?

ABoth queries return the first 3 rows ordered by EmployeeID.
BQuery 1 returns 3 rows; Query 2 returns all 5 rows.
CQuery 1 returns rows unordered; Query 2 returns 3 rows ordered.
DBoth queries return all 5 rows.
Attempts:
2 left
💡 Hint

Consider how TOP and LIMIT work with ORDER BY.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in LIMIT usage

Which of the following MySQL queries will cause a syntax error?

SELECT * FROM Orders LIMIT;
ASELECT * FROM Orders LIMIT;
BSELECT * FROM Orders LIMIT 5;
CSELECT * FROM Orders LIMIT 5 OFFSET 10;
DSELECT * FROM Orders LIMIT 10, 5;
Attempts:
2 left
💡 Hint

Check if LIMIT requires a number after it.

❓ optimization
advanced
2:00remaining
Performance difference between TOP and LIMIT

In a large SQL Server database, which query is generally more efficient to retrieve the first 10 rows?

A) SELECT TOP 10 * FROM LargeTable ORDER BY CreatedDate DESC;
B) SELECT * FROM LargeTable ORDER BY CreatedDate DESC LIMIT 10;
AQuery B is more efficient because LIMIT is faster than TOP in SQL Server.
BQuery A is efficient because SQL Server optimizes TOP; Query B causes syntax error in SQL Server.
CBoth queries are equally efficient and valid in SQL Server.
DQuery A causes syntax error; Query B works fine in SQL Server.
Attempts:
2 left
💡 Hint

Think about which clause is supported by SQL Server.

🔧 Debug
expert
2:00remaining
Debugging incorrect use of TOP and LIMIT

A developer writes this query in PostgreSQL:

SELECT TOP 5 * FROM Customers ORDER BY CustomerID;

What error will PostgreSQL raise?

AError: LIMIT clause missing.
BUnknown column 'TOP'.
CSyntax error near 'TOP'.
DNo error; query runs successfully.
Attempts:
2 left
💡 Hint

Recall which databases support TOP.

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