INSERT with specific columns in SQL - Time & Space Complexity
When we add new rows to a database table using INSERT with specific columns, it's important to understand how the time taken grows as we add more rows.
We want to know how the work changes when the number of rows inserted increases.
Analyze the time complexity of the following SQL INSERT statement.
INSERT INTO employees (name, department, salary)
VALUES
('Alice', 'HR', 50000),
('Bob', 'IT', 60000),
('Carol', 'Finance', 55000);
This code adds three new employees to the employees table by specifying only some columns.
Look for repeated actions that take time.
- Primary operation: Inserting each row into the table.
- How many times: Once for each row in the VALUES list.
As you add more rows, the database does more work for each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 insert actions |
| 100 | About 100 insert actions |
| 1000 | About 1000 insert actions |
Pattern observation: The work grows directly with the number of rows you add.
Time Complexity: O(n)
This means the time to insert rows grows in a straight line as you add more rows.
[X] Wrong: "Inserting multiple rows at once takes the same time as inserting one row."
[OK] Correct: Each row still needs to be added, so the total time grows with the number of rows.
Understanding how insert operations scale helps you explain database performance clearly and confidently in real situations.
"What if we insert rows without specifying columns? How would the time complexity change?"