SELECT all columns in SQL - Time & Space Complexity
We want to understand how the time to run a query changes when we select all columns from a table.
Specifically, how does the work grow as the table gets bigger?
Analyze the time complexity of the following SQL query.
SELECT *
FROM employees;
This query retrieves every column and every row from the employees table.
Look for repeated actions in the query execution.
- Primary operation: Reading each row from the employees table.
- How many times: Once for every row in the table.
As the number of rows grows, the work grows too.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads of all columns |
| 100 | 100 reads of all columns |
| 1000 | 1000 reads of all columns |
Pattern observation: The work increases directly with the number of rows.
Time Complexity: O(n)
This means the time to run the query grows in a straight line as the table gets bigger.
[X] Wrong: "Selecting all columns is slower because of the * symbol itself."
[OK] Correct: The * just means all columns; the real time depends on how many rows and columns there are, not the symbol.
Understanding how query time grows with table size helps you explain database performance clearly and confidently.
"What if we added a WHERE clause to filter rows? How would the time complexity change?"