Challenge - 5 Problems
Generated Columns Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a stored generated column calculation
Consider the following table definition and insert statement in PostgreSQL:
What will be the output of the query
CREATE TABLE sales ( price numeric, quantity int, total numeric GENERATED ALWAYS AS (price * quantity) STORED ); INSERT INTO sales (price, quantity) VALUES (10, 3);
What will be the output of the query
SELECT total FROM sales;?PostgreSQL
CREATE TABLE sales ( price numeric, quantity int, total numeric GENERATED ALWAYS AS (price * quantity) STORED ); INSERT INTO sales (price, quantity) VALUES (10, 3); SELECT total FROM sales;
Attempts:
2 left
💡 Hint
Stored generated columns calculate and store the value automatically based on the expression.
✗ Incorrect
The stored generated column 'total' is calculated as price * quantity, so 10 * 3 = 30 is stored and returned.
📝 Syntax
intermediate2:00remaining
Correct syntax for generated column definition
Which of the following CREATE TABLE statements correctly defines a generated column in PostgreSQL?
Attempts:
2 left
💡 Hint
PostgreSQL supports only STORED generated columns, not VIRTUAL.
✗ Incorrect
PostgreSQL currently supports only STORED generated columns. The option with VIRTUAL keyword causes syntax error.
🧠 Conceptual
advanced2:00remaining
Difference between stored and virtual generated columns
Which statement correctly describes the difference between stored and virtual generated columns in databases?
Attempts:
2 left
💡 Hint
Think about when the value is calculated and if it is saved on disk.
✗ Incorrect
Stored generated columns save the computed value physically in the table, while virtual columns compute the value on the fly when queried without storing it.
🔧 Debug
advanced2:00remaining
Why does this generated column cause an error?
Given the table definition:
Why does the insert statement fail?
CREATE TABLE orders ( unit_price numeric, quantity int, total numeric NOT NULL GENERATED ALWAYS AS (unit_price * quantity) STORED ); INSERT INTO orders (unit_price, quantity) VALUES (5, NULL);
Why does the insert statement fail?
PostgreSQL
CREATE TABLE orders ( unit_price numeric, quantity int, total numeric NOT NULL GENERATED ALWAYS AS (unit_price * quantity) STORED ); INSERT INTO orders (unit_price, quantity) VALUES (5, NULL);
Attempts:
2 left
💡 Hint
Consider how NULL values affect expressions in generated columns.
✗ Incorrect
The expression unit_price * quantity evaluates to NULL when quantity is NULL. Stored generated columns cannot store NULL if the column is defined NOT NULL or if the database disallows it.
❓ optimization
expert2:00remaining
Optimizing queries using stored generated columns
You have a table with a stored generated column that calculates
total_price as unit_price * quantity. Which query will generally perform better in PostgreSQL?Attempts:
2 left
💡 Hint
Think about how stored generated columns can be indexed and reused.
✗ Incorrect
Using the stored generated column in the WHERE clause allows PostgreSQL to use indexes on that column, improving performance. Calculating the expression repeatedly in the query is less efficient.