Computed columns let you create new fields in your database that automatically calculate values based on other fields. This helps keep data consistent and saves time.
You want to store a full name by combining first and last names automatically.
You need to calculate the total price by multiplying quantity and unit price.
You want to create a URL-friendly slug from a title field.
You want to keep track of a user's age based on their birthdate.
You want to automatically format a phone number from raw digits.
2. Which of the following is the correct syntax to create a computed column named full_name that concatenates first_name and last_name in Supabase?
easy
A. ALTER TABLE users ADD COLUMN full_name TEXT GENERATED BY DEFAULT AS (first_name || last_name);
B. ALTER TABLE users ADD full_name TEXT AS (first_name + last_name);
C. CREATE COLUMN full_name AS CONCAT(first_name, last_name);
D. ALTER TABLE users ADD COLUMN full_name TEXT GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED;
Solution
Step 1: Identify correct syntax for generated columns
Supabase uses PostgreSQL syntax: GENERATED ALWAYS AS (expression) STORED.
Step 2: Check concatenation and syntax correctness
ALTER TABLE users ADD COLUMN full_name TEXT GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED; correctly uses || for string concatenation and the full syntax.
Final Answer:
ALTER TABLE users ADD COLUMN full_name TEXT GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED; -> Option D
Quick Check:
Correct syntax = ALTER TABLE users ADD COLUMN full_name TEXT GENERATED ALWAYS AS (first_name || ' ' || last_name) STORED; [OK]
Hint: Use GENERATED ALWAYS AS (expression) STORED for computed columns [OK]
Common Mistakes:
Using + instead of || for string concatenation
Omitting STORED keyword
Using GENERATED BY DEFAULT instead of ALWAYS
3. Given the table orders with columns quantity (integer) and price_per_unit (numeric), what will be the value of the computed column total_price defined as GENERATED ALWAYS AS (quantity * price_per_unit) STORED for a row where quantity = 3 and price_per_unit = 10.5?
medium
A. 3
B. 315
C. 31.5
D. 13.5
Solution
Step 1: Understand the computed column expression
The total_price is calculated by multiplying quantity by price_per_unit.
Step 2: Calculate the value for given inputs
3 * 10.5 = 31.5
Final Answer:
31.5 -> Option C
Quick Check:
3 x 10.5 = 31.5 [OK]
Hint: Multiply quantity and price_per_unit for total_price [OK]
Common Mistakes:
Multiplying incorrectly (e.g., 3 * 105)
Confusing addition with multiplication
Ignoring decimal in price_per_unit
4. You tried to create a computed column with this command: ALTER TABLE products ADD COLUMN discount_price NUMERIC GENERATED ALWAYS AS (price - discount) STORED; But you get an error. What is the most likely cause?
medium
A. One or both columns price or discount do not exist
B. The syntax for GENERATED ALWAYS AS is incorrect
C. Computed columns cannot use arithmetic operations
D. The column discount_price already exists
Solution
Step 1: Check syntax correctness
The syntax is correct for a computed column using arithmetic.
Step 2: Identify common error causes
An error often occurs if referenced columns like price or discount do not exist in the table.
Final Answer:
One or both columns price or discount do not exist -> Option A
Quick Check:
Missing columns cause errors [OK]
Hint: Verify referenced columns exist before adding computed column [OK]
Common Mistakes:
Assuming syntax is wrong without checking columns
Thinking arithmetic is not allowed in computed columns
Not checking if column already exists
5. You want to create a computed column age_group in a users table that categorizes users as 'minor' if age < 18, 'adult' if age is between 18 and 64, and 'senior' if 65 or older. Which of the following is the correct way to define this computed column in Supabase?
hard
A. ALTER TABLE users ADD COLUMN age_group TEXT GENERATED ALWAYS AS (CASE WHEN age < 18 THEN 'minor' WHEN age BETWEEN 18 AND 64 THEN 'adult' ELSE 'senior' END) STORED;
B. ALTER TABLE users ADD COLUMN age_group TEXT GENERATED ALWAYS AS (IF age < 18 THEN 'minor' ELSE IF age < 65 THEN 'adult' ELSE 'senior' END) STORED;
C. ALTER TABLE users ADD COLUMN age_group TEXT GENERATED ALWAYS AS (age < 18 ? 'minor' : age < 65 ? 'adult' : 'senior') STORED;
D. ALTER TABLE users ADD COLUMN age_group TEXT GENERATED ALWAYS AS (SWITCH(age) { CASE < 18: 'minor'; CASE < 65: 'adult'; DEFAULT: 'senior'; }) STORED;
Solution
Step 1: Identify correct conditional syntax in PostgreSQL
PostgreSQL uses CASE WHEN ... THEN ... ELSE ... END for conditional expressions.
Step 2: Verify the CASE expression matches the age groups
ALTER TABLE users ADD COLUMN age_group TEXT GENERATED ALWAYS AS (CASE WHEN age < 18 THEN 'minor' WHEN age BETWEEN 18 AND 64 THEN 'adult' ELSE 'senior' END) STORED; correctly uses CASE with conditions for minor, adult, and senior.
Step 3: Confirm full syntax for computed column
ALTER TABLE users ADD COLUMN age_group TEXT GENERATED ALWAYS AS (CASE WHEN age < 18 THEN 'minor' WHEN age BETWEEN 18 AND 64 THEN 'adult' ELSE 'senior' END) STORED; uses GENERATED ALWAYS AS (...) STORED correctly.
Final Answer:
ALTER TABLE users ADD COLUMN age_group TEXT GENERATED ALWAYS AS (CASE WHEN age < 18 THEN 'minor' WHEN age BETWEEN 18 AND 64 THEN 'adult' ELSE 'senior' END) STORED; -> Option A
Quick Check:
Use CASE WHEN for conditions in computed columns [OK]
Hint: Use CASE WHEN for multi-condition computed columns [OK]
Common Mistakes:
Using IF or SWITCH which are not valid in PostgreSQL expressions
Incorrect ternary syntax in SQL
Missing END in CASE expression
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.