Complete the code to create a generated column that concatenates first and last names.
CREATE TABLE users (id SERIAL PRIMARY KEY, first_name TEXT, last_name TEXT, full_name TEXT GENERATED ALWAYS AS (first_name || ' ' || [1]) STORED);
The generated column full_name combines first_name and last_name. So, last_name is the correct field to concatenate.
Complete the code to create a generated column that calculates the total price with tax.
CREATE TABLE orders (id SERIAL PRIMARY KEY, price NUMERIC, tax_rate NUMERIC, total_price NUMERIC GENERATED ALWAYS AS (price * (1 + [1])) STORED);
The total_price is calculated by multiplying price by 1 + tax_rate. So, tax_rate is the correct field to use.
Fix the error in the generated column that calculates age from birth year.
CREATE TABLE people (id SERIAL PRIMARY KEY, birth_year INT, age INT GENERATED ALWAYS AS ([1] - birth_year) STORED);To calculate age, we subtract birth year from the current year. Using EXTRACT(YEAR FROM CURRENT_DATE) gets the current year dynamically.
Fill both blanks to create a generated column that stores the domain from an email address.
CREATE TABLE contacts (id SERIAL PRIMARY KEY, email TEXT, domain TEXT GENERATED ALWAYS AS (SUBSTRING(email FROM POSITION([1] IN email) + 1)) STORED);
The domain part of an email starts after the '@' symbol. So, we find the position of '@' and extract the substring after it.
Fill all three blanks to create a generated column that calculates the full address by combining street, city, and zip code.
CREATE TABLE addresses (id SERIAL PRIMARY KEY, street TEXT, city TEXT, zip TEXT, full_address TEXT GENERATED ALWAYS AS (street || [1] || city || [2] || [3]) STORED);
The full address combines street, city, and zip code separated by commas and spaces. So, we use ', ' between street and city, ', ' between city and zip, and include the zip field.
