Bird
Raised Fist0
SQLquery~20 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. What does the SQL expression SELECT price * quantity AS total_cost FROM sales; do?
easy
A. Calculates total cost by multiplying price and quantity for each row.
B. Adds price and quantity values together for each row.
C. Divides price by quantity for each row.
D. Filters rows where price equals quantity.

Solution

  1. Step 1: Understand the multiplication in SELECT

    The expression multiplies the values in columns price and quantity for each row.
  2. Step 2: Recognize the alias usage

    The result of multiplication is renamed as total_cost for clarity in output.
  3. Final Answer:

    Calculates total cost by multiplying price and quantity for each row. -> Option A
  4. Quick Check:

    price * quantity = total_cost [OK]
Hint: Look for * operator to identify multiplication [OK]
Common Mistakes:
  • Confusing multiplication with addition or division
  • Ignoring the AS alias
  • Thinking it filters rows
2. Which of the following is the correct syntax to calculate the average price and rename it as avg_price?
easy
A. SELECT AVG(price) AS avg_price FROM products;
B. SELECT AVG(price) avg_price FROM products;
C. SELECT AVG(price) TO avg_price FROM products;
D. SELECT AVG(price) RENAME avg_price FROM products;

Solution

  1. Step 1: Identify correct alias syntax

    In SQL, the keyword AS is used to rename columns or expressions in SELECT.
  2. Step 2: Check each option's syntax

    SELECT AVG(price) AS avg_price FROM products; uses AVG(price) AS avg_price which is the correct syntax; others use invalid keywords or omit AS.
  3. Final Answer:

    SELECT AVG(price) AS avg_price FROM products; -> Option A
  4. Quick Check:

    Use AS to rename columns [OK]
Hint: Use AS keyword to rename calculated columns [OK]
Common Mistakes:
  • Omitting AS keyword
  • Using TO or RENAME instead of AS
  • Misplacing parentheses
3. Given a table orders with columns price and quantity, what is the output of this query?
SELECT price, quantity, price * quantity AS total FROM orders WHERE price = 10;
medium
A. All rows with total calculated, ignoring price filter.
B. Syntax error due to missing GROUP BY.
C. Rows where price is 10, showing price, quantity, and total (price*quantity).
D. Rows where quantity equals 10.

Solution

  1. Step 1: Understand the WHERE clause effect

    The query filters rows to only those where price equals 10.
  2. Step 2: Calculate total for filtered rows

    For each filtered row, it shows price, quantity, and total which is price multiplied by quantity.
  3. Final Answer:

    Rows where price is 10, showing price, quantity, and total (price*quantity). -> Option C
  4. Quick Check:

    WHERE price=10 filters rows; total = price*quantity [OK]
Hint: Check WHERE filters before calculations [OK]
Common Mistakes:
  • Ignoring WHERE clause filtering
  • Expecting all rows instead of filtered
  • Thinking GROUP BY is needed here
4. Identify the error in this SQL query:
SELECT price + quantity AS total FROM sales WHERE total > 100;
medium
A. Missing FROM clause in the query.
B. You cannot use alias 'total' in WHERE clause directly.
C. The + operator is invalid for numeric addition.
D. AS keyword cannot be used for aliasing.

Solution

  1. Step 1: Understand alias scope in SQL

    Aliases defined in SELECT cannot be used in WHERE clause because WHERE is evaluated before SELECT.
  2. Step 2: Identify correct fix

    Use the full expression (price + quantity) in WHERE or use a subquery/CTE to filter by total.
  3. Final Answer:

    You cannot use alias 'total' in WHERE clause directly. -> Option B
  4. Quick Check:

    Alias not usable in WHERE [OK]
Hint: Aliases appear after WHERE; can't use them there [OK]
Common Mistakes:
  • Using alias in WHERE clause
  • Thinking + is invalid operator
  • Ignoring query evaluation order
5. You want to calculate the discounted price for products with a 10% discount and show it as discounted_price. Which query correctly applies the discount and rounds the result to 2 decimals?
hard
A. SELECT price - (price * 0.10) AS discounted_price FROM products;
B. SELECT price * 0.90 AS discounted_price FROM products;
C. SELECT ROUND(price * 0.90) AS discounted_price FROM products;
D. SELECT ROUND(price - (price * 0.10), 2) AS discounted_price FROM products;

Solution

  1. Step 1: Calculate 10% discount correctly

    Discounted price is price minus 10% of price, which is price - (price * 0.10) or price * 0.90.
  2. Step 2: Round the result to 2 decimals

    Use ROUND(expression, 2) to round to two decimal places for currency formatting.
  3. Final Answer:

    SELECT ROUND(price - (price * 0.10), 2) AS discounted_price FROM products; -> Option D
  4. Quick Check:

    Use ROUND(..., 2) for 2 decimals [OK]
Hint: Use ROUND(expression, 2) to round decimals [OK]
Common Mistakes:
  • Not rounding the result
  • Rounding without specifying decimals
  • Using incorrect discount calculation
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard