0
0
SQLquery~5 mins

TOP vs LIMIT across databases in SQL - Performance Comparison

Choose your learning style9 modes available
Time Complexity: TOP vs LIMIT across databases
O(n log n)
Understanding Time Complexity

When we use TOP or LIMIT in SQL, we want to get only a few rows from a big table.

We ask: How does the time to get these rows grow as the table gets bigger?

Scenario Under Consideration

Analyze the time complexity of these two queries:


-- Using TOP (SQL Server)
SELECT TOP 10 * FROM Employees ORDER BY EmployeeID;

-- Using LIMIT (MySQL, PostgreSQL)
SELECT * FROM Employees ORDER BY EmployeeID LIMIT 10;
    

Both queries get the first 10 employees sorted by ID from a large table.

Identify Repeating Operations

Look for repeated work done by the database engine:

  • Primary operation: Scanning and sorting the table rows.
  • How many times: The database processes all rows to sort before picking the top 10.
How Execution Grows With Input

As the table grows, sorting takes more time:

Input Size (n)Approx. Operations
10About 10 log 10 operations to sort, then pick 10 rows
100About 100 log 100 operations to sort, then pick 10
1000About 1000 log 1000 operations to sort, then pick 10

Pattern observation: Sorting work grows faster than the number of rows because sorting is more than just looking at each row once.

Final Time Complexity

Time Complexity: O(n log n)

This means the time to get the top rows grows a bit faster than the number of rows because sorting takes extra steps.

Common Mistake

[X] Wrong: "Getting only 10 rows means the query runs in constant time no matter the table size."

[OK] Correct: The database still needs to look at and sort many rows before it can find the top 10, so time grows with table size.

Interview Connect

Understanding how TOP and LIMIT work helps you explain query speed clearly and shows you know how databases handle big data efficiently.

Self-Check

"What if the Employees table has an index on EmployeeID? How would that change the time complexity?"