LIMIT clause behavior in SQL - Time & Space Complexity
We want to understand how using the LIMIT clause affects the time it takes to get results from a database query.
Specifically, how does the number of rows we ask for change the work the database does?
Analyze the time complexity of the following code snippet.
SELECT *
FROM employees
ORDER BY hire_date DESC
LIMIT 10;
This query gets the 10 most recently hired employees from the employees table.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Scanning and sorting the employees table rows by hire_date.
- How many times: The database looks at each row once to sort, then picks the top 10 rows.
As the number of rows in the employees table grows, the database must sort more rows before picking the top 10.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Sorting 10 rows, then returning 10 rows |
| 100 | Sorting 100 rows, then returning 10 rows |
| 1000 | Sorting 1000 rows, then returning 10 rows |
Pattern observation: The sorting work grows as the table grows, but the number of rows returned stays the same.
Time Complexity: O(n log n)
This means the time to run the query grows a bit faster than the number of rows because sorting takes more work as the table gets bigger.
[X] Wrong: "LIMIT 10 means the database only looks at 10 rows, so it's always fast."
[OK] Correct: The database usually must look at all rows to sort them before picking the top 10, so the work depends on the total number of rows, not just the limit.
Understanding how LIMIT affects query time helps you explain how databases handle large data efficiently and shows you think about performance in real situations.
"What if we added an index on hire_date? How would the time complexity change?"
Practice
LIMIT clause do in an SQL query?Solution
Step 1: Understand the purpose of LIMIT
The LIMIT clause is used to restrict the number of rows returned by a query.Step 2: Compare with other options
Sorting is done by ORDER BY, grouping by GROUP BY, and deleting by DELETE statement, not LIMIT.Final Answer:
It restricts the number of rows returned by the query. -> Option AQuick Check:
LIMIT = restrict rows [OK]
- Confusing LIMIT with ORDER BY
- Thinking LIMIT deletes rows
- Mixing LIMIT with GROUP BY
employees?Solution
Step 1: Identify correct LIMIT syntax
The standard SQL syntax to limit rows is using LIMIT followed by the number, e.g., LIMIT 5.Step 2: Check other options
TOP 5 is SQL Server syntax, FETCH FIRST 5 ROWS is standard SQL but typically requires 'ONLY', and WHERE LIMIT = 5 is invalid.Final Answer:
SELECT * FROM employees LIMIT 5; -> Option AQuick Check:
LIMIT 5 = first 5 rows [OK]
- Using TOP instead of LIMIT in standard SQL
- Misplacing LIMIT inside WHERE clause
- Using FETCH without OFFSET incorrectly
products with rows:id | name1 | Apple
2 | Banana
3 | Cherry
4 | Date
5 | Elderberry
What is the result of the query:
SELECT name FROM products ORDER BY id DESC LIMIT 3;?Solution
Step 1: Understand ORDER BY id DESC
Ordering by id descending means rows are sorted from highest id to lowest: 5,4,3,2,1.Step 2: Apply LIMIT 3
LIMIT 3 returns the first 3 rows after sorting: ids 5,4,3 which correspond to Elderberry, Date, Cherry.Final Answer:
["Elderberry", "Date", "Cherry"] -> Option BQuick Check:
ORDER BY DESC + LIMIT 3 = top 3 highest ids [OK]
- Ignoring ORDER BY and taking first rows as is
- Mixing ascending and descending order
- Confusing LIMIT number with offset
SELECT * FROM orders LIMIT;Solution
Step 1: Check LIMIT syntax
LIMIT must be followed by a number indicating how many rows to return.Step 2: Validate other options
LIMIT can be used without ORDER BY, it comes after FROM, and works on any query regardless of column types.Final Answer:
LIMIT requires a number to specify how many rows to return. -> Option DQuick Check:
LIMIT needs a number [OK]
- Omitting the number after LIMIT
- Thinking ORDER BY is mandatory with LIMIT
- Placing LIMIT before FROM
price ascending. Which query correctly uses LIMIT and OFFSET to achieve this?Solution
Step 1: Calculate OFFSET for page 3 with 10 rows per page
Page 1 shows rows 0-9, page 2 shows 10-19, so page 3 starts at offset 20.Step 2: Use LIMIT 10 to get 10 rows for the page
LIMIT 10 returns 10 rows starting from OFFSET 20.Final Answer:
SELECT * FROM products ORDER BY price ASC LIMIT 10 OFFSET 20; -> Option CQuick Check:
Page 3 offset = (3-1)*10 = 20 [OK]
- Using wrong OFFSET calculation
- Mixing LIMIT and OFFSET values
- Forgetting ORDER BY for consistent paging
Ready to prove it?
Pass the test and this pattern moves unknown
