INSERT with DEFAULT values in SQL - Time & Space Complexity
When we add new rows to a database table using default values, it is important to understand how the time it takes grows as we add more rows.
We want to know how the work done changes when inserting many rows with default values.
Analyze the time complexity of the following code snippet.
INSERT INTO employees DEFAULT VALUES;
INSERT INTO employees DEFAULT VALUES;
-- Repeat many times to add multiple rows
This code inserts one new row into the employees table using all default values each time it runs.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Inserting a single row with default values.
- How many times: Once per INSERT statement executed.
Each time we run the INSERT, the database does a fixed amount of work to add one row.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 insert operations |
| 100 | 100 insert operations |
| 1000 | 1000 insert operations |
Pattern observation: The work grows directly with the number of rows inserted.
Time Complexity: O(n)
This means the time to insert rows grows in a straight line as you add more rows one by one.
[X] Wrong: "Inserting rows with default values takes the same time no matter how many rows I add."
[OK] Correct: Each row requires a separate insert operation, so more rows mean more work and more time.
Understanding how inserting rows scales helps you explain database performance clearly and shows you know how operations grow with data size.
"What if we insert multiple rows in a single INSERT statement instead of one at a time? How would the time complexity change?"
Practice
INSERT INTO table_name DEFAULT VALUES; do?Solution
Step 1: Understand the INSERT statement
The statement inserts a new row into the table.Step 2: Recognize DEFAULT VALUES usage
Using DEFAULT VALUES means the new row uses default values defined for each column.Final Answer:
Inserts a new row using all default values for columns -> Option AQuick Check:
INSERT ... DEFAULT VALUES inserts default row [OK]
- Thinking it deletes or updates rows
- Confusing DEFAULT VALUES with SELECT
- Assuming it inserts NULLs instead of defaults
Solution
Step 1: Recall correct syntax for default insertion
The correct syntax isINSERT INTO table_name DEFAULT VALUES;to insert a row with all default values.Step 2: Check each option
INSERT INTO table_name DEFAULT VALUES; matches the correct syntax exactly; others are invalid or incorrect.Final Answer:
INSERT INTO table_name DEFAULT VALUES; -> Option DQuick Check:
Correct syntax = INSERT INTO ... DEFAULT VALUES [OK]
- Placing DEFAULT inside VALUES parentheses
- Swapping DEFAULT and VALUES keywords
- Using incorrect keyword order
users(id INT DEFAULT 0, name VARCHAR(20) DEFAULT 'guest'), what will be the result of INSERT INTO users DEFAULT VALUES; SELECT * FROM users; after running on an empty table?Solution
Step 1: Understand table defaults
The table has default values: id = 0, name = 'guest'.Step 2: Effect of INSERT DEFAULT VALUES
Inserting default values adds a row with id=0 and name='guest'.Step 3: SELECT result
After insertion, selecting all rows returns one row with those default values.Final Answer:
[{"id": 0, "name": "guest"}] -> Option BQuick Check:
Defaults inserted row = id 0, name guest [OK]
- Expecting NULL instead of default values
- Assuming syntax error due to DEFAULT VALUES
- Thinking table remains empty
products(id INT NOT NULL, name VARCHAR(20) DEFAULT 'unknown'). What error occurs when running INSERT INTO products DEFAULT VALUES;?Solution
Step 1: Check columns for defaults
The 'id' column has no default and is NOT nullable.Step 2: Effect of INSERT DEFAULT VALUES
Trying to insert default values fails because 'id' has no default and no value is given.Final Answer:
Error: Column 'id' has no default and no value provided -> Option AQuick Check:
Missing default for NOT NULL column causes error [OK]
- Assuming NULL is inserted automatically
- Thinking DEFAULT VALUES always works
- Confusing syntax error with constraint error
orders(order_id INT PRIMARY KEY DEFAULT nextval('order_seq'), status VARCHAR(10) DEFAULT 'pending', created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP). Which SQL statement correctly inserts a new order using all defaults except setting status to 'shipped'?Solution
Step 1: Understand default and explicit values
We want to use defaults for order_id and created_at, but set status explicitly.Step 2: Check each option
INSERT INTO orders (status) VALUES ('shipped'); correctly lets the DBMS use defaults implicitly for unspecified columns order_id and created_at. INSERT INTO orders DEFAULT VALUES SET status = 'shipped'; is invalid syntax. INSERT INTO orders (order_id, status, created_at) VALUES (DEFAULT, DEFAULT, 'shipped'); incorrectly sets status to its default ('pending') and attempts to set created_at to 'shipped' causing a type mismatch error. INSERT INTO orders (DEFAULT, 'shipped', DEFAULT); is invalid syntax (missing VALUES keyword).Final Answer:
INSERT INTO orders (status) VALUES ('shipped'); -> Option CQuick Check:
Unspecified columns use their default values automatically [OK]
- Thinking omitting columns always causes errors
- Using DEFAULT VALUES with SET syntax
- Misplacing values and DEFAULT keywords in VALUES clause
Ready to prove it?
Pass the test and this pattern moves unknown
