0
0
MySQLquery~20 mins

Why computed values add flexibility in MySQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Computed Values Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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;
A[{"quantity": 3, "unit_price": 20, "total_price": 23}]
B[{"quantity": 3, "unit_price": 20}]
C[{"quantity": 3, "unit_price": 20, "total_price": 60}]
DSyntaxError
Attempts:
2 left
💡 Hint
Think about how multiplication works in SQL expressions.
🧠 Conceptual
intermediate
1:30remaining
Why use computed columns in a database?
Which of the following is the best reason to use computed columns in a database table?
ATo store redundant data that never changes
BTo prevent any changes to the table structure
CTo make queries slower by adding extra calculations
DTo automatically calculate values based on other columns, reducing manual errors
Attempts:
2 left
💡 Hint
Think about how computed columns help keep data consistent.
📝 Syntax
advanced
2: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))
);
ACREATE TABLE Employees (id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), full_name VARCHAR(101) AS (CONCAT(first_name, ' ', last_name)));
BCREATE TABLE Employees (id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), full_name VARCHAR(101) GENERATED ALWAYS AS (CONCAT(first_name, ' ', last_name)) VIRTUAL);
CCREATE TABLE Employees (id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), full_name AS VARCHAR(101) (CONCAT(first_name, ' ', last_name)));
DCREATE TABLE Employees (id INT PRIMARY KEY, first_name VARCHAR(50), last_name VARCHAR(50), full_name VARCHAR(101) COMPUTED AS CONCAT(first_name, ' ', last_name));
Attempts:
2 left
💡 Hint
MySQL uses GENERATED ALWAYS AS syntax for computed columns.
optimization
advanced
2:00remaining
Performance impact of computed columns
Which statement about computed columns and query performance is true?
AStored computed columns can improve query performance by pre-calculating values
BComputed columns cannot be indexed, so they never improve performance
CComputed columns always slow down queries because they calculate values on the fly
DUsing computed columns requires disabling indexes on the table
Attempts:
2 left
💡 Hint
Think about how storing computed values affects query speed.
🔧 Debug
expert
3: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)
);
AThe computed column must specify VIRTUAL or STORED keyword in MySQL
BYou cannot use arithmetic operations in computed columns
CThe discount column must be an integer type
DComputed columns cannot reference other columns in the same table
Attempts:
2 left
💡 Hint
Check the syntax requirements for computed columns in MySQL.