Challenge - 5 Problems
Computed Values Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Output of a computed column in a SELECT query
Given a table Orders with columns
quantity and unit_price, what is the output of the following query?SELECT quantity, unit_price, quantity * unit_price AS total_price FROM Orders WHERE order_id = 101;
MySQL
SELECT quantity, unit_price, quantity * unit_price AS total_price FROM Orders WHERE order_id = 101;
Attempts:
2 left
💡 Hint
Think about how multiplication works in SQL expressions.
✗ Incorrect
The computed column
total_price multiplies quantity by unit_price. For quantity 3 and unit_price 20, total_price is 60.🧠 Conceptual
intermediate1:30remaining
Why use computed columns in a database?
Which of the following is the best reason to use computed columns in a database table?
Attempts:
2 left
💡 Hint
Think about how computed columns help keep data consistent.
✗ Incorrect
Computed columns calculate values automatically from other columns, reducing manual errors and improving data consistency.
📝 Syntax
advanced2:30remaining
Identify the correct syntax for creating a computed column
Which option correctly creates a computed column named
full_name that concatenates first_name and last_name in MySQL?MySQL
CREATE TABLE Employees ( id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), full_name VARCHAR(101) AS (CONCAT(first_name, ' ', last_name)) );
Attempts:
2 left
💡 Hint
MySQL uses GENERATED ALWAYS AS syntax for computed columns.
✗ Incorrect
In MySQL, computed columns are created using GENERATED ALWAYS AS with either VIRTUAL or STORED keyword. Option B uses correct syntax.
❓ optimization
advanced2:00remaining
Performance impact of computed columns
Which statement about computed columns and query performance is true?
Attempts:
2 left
💡 Hint
Think about how storing computed values affects query speed.
✗ Incorrect
Stored computed columns save calculated values physically, allowing indexes on them and improving query speed.
🔧 Debug
expert3:00remaining
Debugging a computed column error
A developer tries to create a computed column
discounted_price as price - discount but gets an error. Which option explains the cause?MySQL
CREATE TABLE Products ( id INT PRIMARY KEY, price DECIMAL(10,2), discount DECIMAL(10,2), discounted_price DECIMAL(10,2) GENERATED ALWAYS AS (price - discount) );
Attempts:
2 left
💡 Hint
Check the syntax requirements for computed columns in MySQL.
✗ Incorrect
MySQL requires specifying VIRTUAL or STORED for computed columns. Omitting this causes a syntax error.