0
0
PostgreSQLquery~20 mins

GENERATED columns (stored and virtual) in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Generated Columns Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Output of a stored generated column calculation
Consider the following table definition and insert statement in PostgreSQL:

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;
A30
BNULL
C13
DSyntax error
Attempts:
2 left
💡 Hint
Stored generated columns calculate and store the value automatically based on the expression.
📝 Syntax
intermediate
2:00remaining
Correct syntax for generated column definition
Which of the following CREATE TABLE statements correctly defines a generated column in PostgreSQL?
ACREATE TABLE items (a int, b int GENERATED ALWAYS AS (a * 2) STORED);
BCREATE TABLE items (a int, b int GENERATED ALWAYS AS (a * 2) VIRTUAL);
CCREATE TABLE items (a int, b int AS (a * 2) VIRTUAL);
DCREATE TABLE items (a int, b int GENERATED ALWAYS AS (a * 2));
Attempts:
2 left
💡 Hint
PostgreSQL supports only STORED generated columns, not VIRTUAL.
🧠 Conceptual
advanced
2:00remaining
Difference between stored and virtual generated columns
Which statement correctly describes the difference between stored and virtual generated columns in databases?
AStored columns compute values on query time; virtual columns store values physically.
BStored columns store computed values physically; virtual columns compute values on query time without storing.
CBoth stored and virtual columns store computed values physically but differ in update speed.
DVirtual columns are deprecated and replaced by stored columns in all databases.
Attempts:
2 left
💡 Hint
Think about when the value is calculated and if it is saved on disk.
🔧 Debug
advanced
2:00remaining
Why does this generated column cause an error?
Given the table definition:

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);
ABecause generated columns cannot reference nullable columns.
BBecause the table definition is missing a primary key.
CBecause the multiplication operator is invalid for numeric and NULL types.
DBecause quantity is NULL, the generated column expression results in NULL which is not allowed for stored columns.
Attempts:
2 left
💡 Hint
Consider how NULL values affect expressions in generated columns.
optimization
expert
2: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?
ASELECT unit_price, quantity FROM sales WHERE unit_price * quantity > 100;
BSELECT unit_price * quantity AS total_price FROM sales WHERE unit_price * quantity > 100;
CSELECT total_price FROM sales WHERE total_price > 100;
DSELECT total_price FROM sales WHERE unit_price * quantity > 100;
Attempts:
2 left
💡 Hint
Think about how stored generated columns can be indexed and reused.