Challenge - 5 Problems
AVG Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ query_result
intermediate2:00remaining
Calculate average salary of employees
Given the table Employees with columns
id, name, and salary, what is the result of this query?SELECT AVG(salary) FROM Employees;
SQL
CREATE TABLE Employees (id INT, name VARCHAR(50), salary INT); INSERT INTO Employees VALUES (1, 'Alice', 50000), (2, 'Bob', 60000), (3, 'Charlie', 70000);
Attempts:
2 left
💡 Hint
AVG calculates the average value of a numeric column.
✗ Incorrect
The average salary is (50000 + 60000 + 70000) / 3 = 60000.
🧠 Conceptual
intermediate2:00remaining
AVG function behavior with NULL values
Consider a table Scores with a column
score containing values: 10, NULL, 20. What does SELECT AVG(score) FROM Scores; return?Attempts:
2 left
💡 Hint
AVG ignores NULL values when calculating the average.
✗ Incorrect
AVG calculates average of non-NULL values only: (10 + 20) / 2 = 15.
📝 Syntax
advanced2:00remaining
Identify the syntax error in AVG usage
Which option contains a syntax error when using AVG in SQL?
SQL
SELECT AVG salary FROM Employees;
Attempts:
2 left
💡 Hint
AVG requires parentheses around the column name.
✗ Incorrect
Option C misses parentheses around salary, causing syntax error.
❓ optimization
advanced2:00remaining
Optimizing AVG calculation with conditions
You want to calculate the average salary of employees who joined after 2020 from table Employees with columns
salary and join_year. Which query is most efficient?Attempts:
2 left
💡 Hint
Filtering rows before aggregation is more efficient.
✗ Incorrect
Option A filters rows first, then calculates AVG, which is efficient.
🔧 Debug
expert2:00remaining
Debugging unexpected AVG result with GROUP BY
Given the table Sales with columns
region, sales_amount, and sales_rep, what is the output of this query?SELECT region, AVG(sales_amount) FROM Sales GROUP BY sales_rep;
Attempts:
2 left
💡 Hint
GROUP BY columns must match selected non-aggregated columns.
✗ Incorrect
Query groups by sales_rep but selects region without aggregation, causing syntax error.