0
0
SQLquery~20 mins

SELECT with expressions and calculations in SQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Master of SELECT Calculations
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Calculate total price with tax
Given a table sales with columns item, price, and quantity, what is the output of this query?
SELECT item, price * quantity * 1.1 AS total_price_with_tax FROM sales ORDER BY item;
SQL
SELECT item, price * quantity * 1.1 AS total_price_with_tax FROM sales ORDER BY item;
A[{"item": "Apple", "total_price_with_tax": 10.0}, {"item": "Banana", "total_price_with_tax": 5.5}]
B[{"item": "Apple", "total_price_with_tax": 10.0}, {"item": "Banana", "total_price_with_tax": 5.0}]
C[{"item": "Apple", "total_price_with_tax": 11.0}, {"item": "Banana", "total_price_with_tax": 5.0}]
D[{"item": "Apple", "total_price_with_tax": 11.0}, {"item": "Banana", "total_price_with_tax": 5.5}]
Attempts:
2 left
💡 Hint
Remember to multiply price by quantity first, then apply the 10% tax.
query_result
intermediate
2:00remaining
Calculate age from birth year
Given a table users with columns name and birth_year, what is the output of this query if the current year is 2024?
SELECT name, 2024 - birth_year AS age FROM users ORDER BY age DESC;
SQL
SELECT name, 2024 - birth_year AS age FROM users ORDER BY age DESC;
A[{"name": "Alice", "age": 40}, {"name": "Bob", "age": 31}]
B[{"name": "Alice", "age": 39}, {"name": "Bob", "age": 30}]
C[{"name": "Alice", "age": 40}, {"name": "Bob", "age": 30}]
D[{"name": "Alice", "age": 39}, {"name": "Bob", "age": 31}]
Attempts:
2 left
💡 Hint
Subtract birth year from 2024 to get age.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in calculation
Which option contains a syntax error in the SELECT statement that calculates the discounted price?
SELECT product, price - price * discount AS discounted_price FROM products;
SQL
SELECT product, price - price * discount AS discounted_price FROM products;
ASELECT product, price - price * discount AS discounted_price products;
BSELECT product, price - (price * discount) AS discounted_price FROM products;
CSELECT product, price - price * discount discounted_price FROM products;
DSELECT product, price - price * discount AS discounted_price FROM products;
Attempts:
2 left
💡 Hint
Check the FROM clause syntax carefully.
optimization
advanced
2:00remaining
Optimize calculation in SELECT for performance
You want to calculate the total revenue as price * quantity for each sale in the sales table. Which query is more efficient and why?
ASELECT price * quantity AS total_revenue FROM sales;
BSELECT price, quantity, price * quantity AS total_revenue FROM sales;
CSELECT price * quantity AS total_revenue, price * quantity * 1.05 AS revenue_with_tax FROM sales;
DSELECT price + quantity AS total_revenue FROM sales;
Attempts:
2 left
💡 Hint
Consider the amount of data returned and calculation complexity.
🧠 Conceptual
expert
2:00remaining
Understanding expression evaluation order in SELECT
Consider the query:
SELECT price, quantity, price * quantity AS total, total * 0.9 AS discounted_total FROM sales;

What will happen when this query runs?
SQL
SELECT price, quantity, price * quantity AS total, total * 0.9 AS discounted_total FROM sales;
AThe query will run successfully and return price, quantity, total, and discounted_total columns with correct calculations.
BThe query will raise an error because the alias 'total' cannot be used in the same SELECT clause for calculation.
CThe query will return NULL for discounted_total because 'total' is not recognized.
DThe query will ignore the discounted_total column and return only price, quantity, and total.
Attempts:
2 left
💡 Hint
Think about how SQL processes SELECT expressions and aliases.