0
0
PostgreSQLquery~20 mins

DEFAULT values and expressions in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Default Values Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
AThe timestamp 1970-01-01 00:00:00
BNULL
CThe current timestamp at insertion time (e.g., 2024-06-01 12:00:00)
DAn error because no value was provided
Attempts:
2 left
💡 Hint
The DEFAULT clause with now() sets the current time automatically.
📝 Syntax
intermediate
2:00remaining
Which DEFAULT expression is valid?
Which of the following DEFAULT expressions for a column score INTEGER is syntactically valid in PostgreSQL?
ADEFAULT 10 + 5
BDEFAULT '10 + 5'
CDEFAULT 10 + '5'
DDEFAULT (10 + 5)
Attempts:
2 left
💡 Hint
DEFAULT expressions can be arithmetic expressions but must be enclosed in parentheses if complex.
optimization
advanced
2: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?
AEach row gets a different random value because DEFAULT is evaluated per row
BAll rows get the same random value because DEFAULT is evaluated once per statement
CThe query will fail because random() cannot be used in DEFAULT
DThe random values are always zero due to floor()
Attempts:
2 left
💡 Hint
DEFAULT expressions with volatile functions are evaluated once per statement, not per row.
🔧 Debug
advanced
2: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;
AERROR: invalid input syntax for type numeric: "abc"
BERROR: column price does not exist
CERROR: syntax error at or near 'abc'
DNo error, price is set to 0
Attempts:
2 left
💡 Hint
The DEFAULT value must be compatible with the column type.
🧠 Conceptual
expert
2: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?
AAll three rows have the same random value because DEFAULT is evaluated once per statement
BEach row has a different random value because DEFAULT is evaluated per row
CThe insert fails because random() cannot be used in DEFAULT
DThe values are NULL because DEFAULT cannot use functions
Attempts:
2 left
💡 Hint
Think about how PostgreSQL evaluates DEFAULT expressions with volatile functions in multi-row inserts.