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 Employees table with columns
id, name, and salary, what is the output of this query?SELECT AVG(salary) AS avg_salary FROM Employees;
MySQL
CREATE TABLE Employees (id INT, name VARCHAR(50), salary INT); INSERT INTO Employees VALUES (1, 'Alice', 5000), (2, 'Bob', 7000), (3, 'Charlie', 6000);
Attempts:
2 left
💡 Hint
AVG calculates the average of numeric values in a column.
✗ Incorrect
The average salary is (5000 + 7000 + 6000) / 3 = 6000.
❓ query_result
intermediate2:00remaining
Average with NULL values
Consider the Scores table with columns
What is the result of this query?
student_id and score. Some scores are NULL.What is the result of this query?
SELECT AVG(score) AS average_score FROM Scores;
MySQL
CREATE TABLE Scores (student_id INT, score INT); INSERT INTO Scores VALUES (1, 80), (2, NULL), (3, 90);
Attempts:
2 left
💡 Hint
AVG ignores NULL values when calculating the average.
✗ Incorrect
Only scores 80 and 90 are counted, so average is (80 + 90) / 2 = 85.
📝 Syntax
advanced2:00remaining
Identify the syntax error in AVG usage
Which option contains a syntax error when using the AVG function in MySQL?
Attempts:
2 left
💡 Hint
AVG requires parentheses around the column or expression.
✗ Incorrect
Option A misses parentheses around salary, causing syntax error.
❓ optimization
advanced2:00remaining
Optimizing AVG with WHERE clause
You want to calculate the average salary of employees who joined after 2020. Which query is the most efficient?
MySQL
CREATE TABLE Employees (id INT, name VARCHAR(50), salary INT, join_year INT); INSERT INTO Employees VALUES (1, 'Alice', 5000, 2019), (2, 'Bob', 7000, 2021), (3, 'Charlie', 6000, 2022);
Attempts:
2 left
💡 Hint
Filtering rows before aggregation is more efficient than subqueries.
✗ Incorrect
Option D filters rows directly in WHERE clause before AVG calculation.
🧠 Conceptual
expert3:00remaining
AVG function with GROUP BY and NULLs
Given the Sales table with columns
Table data:
region and amount, some amounts are NULL. What is the output of this query?SELECT region, AVG(amount) AS avg_amount FROM Sales GROUP BY region ORDER BY region;
Table data:
region | amount North | 100 North | NULL South | 200 South | 300 East | NULL East | NULL
Attempts:
2 left
💡 Hint
AVG ignores NULLs; if all values are NULL, AVG returns NULL for that group.
✗ Incorrect
East has only NULLs so avg_amount is NULL; North average is 100; South average is (200+300)/2=250.