Complete the code to create a table with a generated column that stores the sum of two columns.
CREATE TABLE sales (
price NUMERIC,
quantity INT,
total NUMERIC GENERATED ALWAYS AS (price [1] quantity) STORED
);The generated column total calculates the sum of price and quantity using the plus operator +.
Complete the code to create a virtual generated column that concatenates first and last names.
CREATE TABLE users (
first_name TEXT,
last_name TEXT,
full_name TEXT GENERATED ALWAYS AS (first_name [1] last_name) VIRTUAL
);+ for string concatenation, which is invalid in SQL.&&.In PostgreSQL, the string concatenation operator is ||. To add a space between names, we use || ' ' ||.
Fix the error in the generated column definition to calculate the discounted price.
CREATE TABLE products (
price NUMERIC,
discount_rate NUMERIC,
discounted_price NUMERIC GENERATED ALWAYS AS (price [1] discount_rate) STORED
);The discounted price is calculated by multiplying price by discount_rate. Using * is correct.
Fill both blanks to create a stored generated column that calculates the area of a rectangle.
CREATE TABLE rectangles ( width INT, height INT, area INT GENERATED ALWAYS AS (width [1] height) [2] );
The area is width multiplied by height, so * is used. The column is stored, so STORED is correct.
Fill all three blanks to create a virtual generated column that calculates the full address by combining street, city, and zip code.
CREATE TABLE addresses ( street TEXT, city TEXT, zip_code TEXT, full_address TEXT GENERATED ALWAYS AS (street [1] city [2] zip_code) [3] );
To build the full address, we concatenate street and city with a comma and space (|| ', ' ||), then city and zip code with a space (|| ' ' ||). The column is virtual, so VIRTUAL is used.