Bird
Raised Fist0
PostgreSQLquery~10 mins

GENERATED columns (stored and virtual) in PostgreSQL - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a table with a generated column that stores the sum of two columns.

PostgreSQL
CREATE TABLE sales (
  price NUMERIC,
  quantity INT,
  total NUMERIC GENERATED ALWAYS AS (price [1] quantity) STORED
);
Drag options to blanks, or click blank then click option'
A+
B/
C*
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using multiplication or division instead of addition.
Forgetting to specify STORED for the generated column.
2fill in blank
medium

Complete the code to create a virtual generated column that concatenates first and last names.

PostgreSQL
CREATE TABLE users (
  first_name TEXT,
  last_name TEXT,
  full_name TEXT GENERATED ALWAYS AS (first_name [1] last_name) VIRTUAL
);
Drag options to blanks, or click blank then click option'
A&&
B|| ' ' ||
CCONCAT
D+ ' ' +
Attempts:
3 left
💡 Hint
Common Mistakes
Using plus + for string concatenation, which is invalid in SQL.
Using unsupported operators like &&.
3fill in blank
hard

Fix the error in the generated column definition to calculate the discounted price.

PostgreSQL
CREATE TABLE products (
  price NUMERIC,
  discount_rate NUMERIC,
  discounted_price NUMERIC GENERATED ALWAYS AS (price [1] discount_rate) STORED
);
Drag options to blanks, or click blank then click option'
A/
B+
C-
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using subtraction instead of multiplication.
Using division which changes the meaning.
4fill in blank
hard

Fill both blanks to create a stored generated column that calculates the area of a rectangle.

PostgreSQL
CREATE TABLE rectangles (
  width INT,
  height INT,
  area INT GENERATED ALWAYS AS (width [1] height) [2]
);
Drag options to blanks, or click blank then click option'
A*
B+
CSTORED
DVIRTUAL
Attempts:
3 left
💡 Hint
Common Mistakes
Using addition instead of multiplication.
Using VIRTUAL when STORED is required.
5fill in blank
hard

Fill all three blanks to create a virtual generated column that calculates the full address by combining street, city, and zip code.

PostgreSQL
CREATE TABLE addresses (
  street TEXT,
  city TEXT,
  zip_code TEXT,
  full_address TEXT GENERATED ALWAYS AS (street [1] city [2] zip_code) [3]
);
Drag options to blanks, or click blank then click option'
A|| ', ' ||
B|| ' ' ||
CVIRTUAL
DSTORED
Attempts:
3 left
💡 Hint
Common Mistakes
Using plus signs for string concatenation.
Mixing up STORED and VIRTUAL keywords.

Practice

(1/5)
1. What is a GENERATED column in PostgreSQL?
easy
A. A column that stores user input without changes
B. A column that automatically calculates its value from other columns
C. A column that can only store text data
D. A column that requires manual updates every time

Solution

  1. Step 1: Understand GENERATED column concept

    A GENERATED column automatically computes its value based on other columns in the same row.
  2. Step 2: Compare options with definition

    Only A column that automatically calculates its value from other columns describes this automatic calculation feature correctly.
  3. Final Answer:

    A column that automatically calculates its value from other columns -> Option B
  4. Quick Check:

    GENERATED column = auto-calculated column [OK]
Hint: GENERATED means auto-calculated, not manual input [OK]
Common Mistakes:
  • Thinking GENERATED columns store manual input
  • Confusing GENERATED with regular stored columns
  • Assuming GENERATED columns only store text
2. Which of the following is the correct syntax to create a STORED GENERATED column in PostgreSQL?
easy
A. CREATE TABLE t (a INT, b INT GENERATED ALWAYS AS (a * 2) STORED);
B. CREATE TABLE t (a INT, b INT AS (a * 2));
C. CREATE TABLE t (a INT, b INT GENERATED ALWAYS AS (a * 2) VIRTUAL);
D. CREATE TABLE t (a INT, b INT COMPUTED AS (a * 2));

Solution

  1. Step 1: Recall PostgreSQL syntax for STORED GENERATED columns

    PostgreSQL uses 'GENERATED ALWAYS AS (expression) STORED' to define such columns.
  2. Step 2: Check each option

    CREATE TABLE t (a INT, b INT GENERATED ALWAYS AS (a * 2) STORED); matches the correct syntax. CREATE TABLE t (a INT, b INT AS (a * 2)); misses 'GENERATED ALWAYS' and 'STORED'. CREATE TABLE t (a INT, b INT GENERATED ALWAYS AS (a * 2) VIRTUAL); uses 'VIRTUAL' which PostgreSQL does not support. CREATE TABLE t (a INT, b INT COMPUTED AS (a * 2)); uses 'COMPUTED' which is invalid in PostgreSQL.
  3. Final Answer:

    CREATE TABLE t (a INT, b INT GENERATED ALWAYS AS (a * 2) STORED); -> Option A
  4. Quick Check:

    PostgreSQL STORED syntax = GENERATED ALWAYS AS (...) STORED [OK]
Hint: Use 'GENERATED ALWAYS AS (...) STORED' for PostgreSQL generated columns [OK]
Common Mistakes:
  • Using VIRTUAL keyword which PostgreSQL does not support
  • Omitting GENERATED ALWAYS clause
  • Using COMPUTED instead of GENERATED
3. Given the table definition:
CREATE TABLE sales (
  price NUMERIC,
  quantity INT,
  total NUMERIC GENERATED ALWAYS AS (price * quantity) STORED
);

What will be the result of:
INSERT INTO sales (price, quantity) VALUES (10, 3);
SELECT total FROM sales;
medium
A. Syntax error
B. [13]
C. [NULL]
D. [30]

Solution

  1. Step 1: Understand the GENERATED column calculation

    The 'total' column is calculated as price * quantity, so 10 * 3 = 30.
  2. Step 2: Check the INSERT and SELECT results

    After inserting (10, 3), selecting 'total' returns 30 as the stored generated value.
  3. Final Answer:

    [30] -> Option D
  4. Quick Check:

    price * quantity = 10 * 3 = 30 [OK]
Hint: GENERATED columns auto-calculate on insert [OK]
Common Mistakes:
  • Expecting manual input for generated column
  • Confusing addition with multiplication
  • Thinking generated columns can be NULL after insert
4. Consider this table creation:
CREATE TABLE products (
  price NUMERIC,
  discount NUMERIC,
  final_price NUMERIC GENERATED ALWAYS AS (price - discount)
);

What is wrong with this statement?
medium
A. final_price must be declared as VIRTUAL
B. Cannot use subtraction in GENERATED columns
C. Missing STORED keyword for the GENERATED column
D. price and discount must be integers

Solution

  1. Step 1: Check PostgreSQL GENERATED column syntax

    PostgreSQL requires the STORED keyword for generated columns; omitting it causes an error.
  2. Step 2: Analyze the given statement

    The statement misses 'STORED' after the expression, so it is invalid syntax.
  3. Final Answer:

    Missing STORED keyword for the GENERATED column -> Option C
  4. Quick Check:

    GENERATED columns must include STORED in PostgreSQL [OK]
Hint: Always add STORED after GENERATED expression in PostgreSQL [OK]
Common Mistakes:
  • Assuming VIRTUAL columns are supported
  • Thinking arithmetic is not allowed
  • Believing data types must be integers
5. You want to create a table to store employee data with columns first_name, last_name, and a generated column full_name that combines both names separated by a space. Which is the correct way to define full_name in PostgreSQL?
hard
A. full_name TEXT GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED
B. full_name TEXT GENERATED ALWAYS AS (first_name + ' ' + last_name) STORED
C. full_name TEXT AS (first_name || ' ' || last_name) VIRTUAL
D. full_name TEXT COMPUTED AS (first_name || ' ' || last_name)

Solution

  1. Step 1: Use correct string concatenation operator in PostgreSQL

    PostgreSQL uses '||' to concatenate strings, not '+'.
  2. Step 2: Use correct GENERATED column syntax

    PostgreSQL requires 'GENERATED ALWAYS AS (expression) STORED' for generated columns; VIRTUAL and COMPUTED are invalid.
  3. Final Answer:

    full_name TEXT GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED -> Option A
  4. Quick Check:

    String concat with || and STORED keyword [OK]
Hint: Use || for string concat and STORED for generated columns [OK]
Common Mistakes:
  • Using + instead of || for string concatenation
  • Using VIRTUAL or COMPUTED keywords
  • Omitting STORED keyword
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard