Challenge - 5 Problems
Default Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
What is the default value inserted?
Consider the table users with a column
created_at TIMESTAMP DEFAULT now(). What will be the value of created_at after running INSERT INTO users DEFAULT VALUES RETURNING created_at;?PostgreSQL
CREATE TABLE users (id SERIAL PRIMARY KEY, created_at TIMESTAMP DEFAULT now()); INSERT INTO users DEFAULT VALUES RETURNING created_at;
Attempts:
2 left
💡 Hint
The DEFAULT clause with now() sets the current time automatically.
✗ Incorrect
The DEFAULT value now() is a function that returns the current timestamp when the row is inserted. So the inserted row gets the current time automatically.
📝 Syntax
intermediate2:00remaining
Which DEFAULT expression is valid?
Which of the following DEFAULT expressions for a column
score INTEGER is syntactically valid in PostgreSQL?Attempts:
2 left
💡 Hint
DEFAULT expressions can be arithmetic expressions but must be enclosed in parentheses if complex.
✗ Incorrect
Option D uses parentheses to enclose the arithmetic expression, which is valid. Option D is invalid syntax. Option D sets a string, which is invalid for INTEGER. Option D mixes integer and string causing error.
❓ optimization
advanced2:00remaining
Optimizing DEFAULT expressions with volatile functions
You have a column
random_val INTEGER DEFAULT floor(random() * 100). What is a potential downside of this DEFAULT expression when inserting multiple rows in a single statement?Attempts:
2 left
💡 Hint
DEFAULT expressions with volatile functions are evaluated once per statement, not per row.
✗ Incorrect
In PostgreSQL, DEFAULT expressions are evaluated once per statement, so all rows inserted in one statement get the same random value. To get different values per row, use triggers or generate values in the insert query.
🔧 Debug
advanced2:00remaining
Why does this DEFAULT expression cause an error?
Given the column definition
price NUMERIC DEFAULT 'abc', what error will occur when inserting a row without specifying price?PostgreSQL
CREATE TABLE products (id SERIAL PRIMARY KEY, price NUMERIC DEFAULT 'abc'); INSERT INTO products DEFAULT VALUES;
Attempts:
2 left
💡 Hint
The DEFAULT value must be compatible with the column type.
✗ Incorrect
The DEFAULT value 'abc' is a string that cannot be cast to numeric, so PostgreSQL raises an invalid input syntax error.
🧠 Conceptual
expert2:00remaining
How does PostgreSQL handle DEFAULT expressions with volatile functions in multi-row inserts?
Consider a table with a column
val INTEGER DEFAULT (floor(random() * 10)). You run INSERT INTO table DEFAULT VALUES, DEFAULT VALUES, DEFAULT VALUES;. What will be the values of val in the inserted rows?Attempts:
2 left
💡 Hint
Think about how PostgreSQL evaluates DEFAULT expressions with volatile functions in multi-row inserts.
✗ Incorrect
PostgreSQL evaluates DEFAULT expressions once per statement, so all rows inserted in one statement get the same value from the volatile function random().