0
0
MySQLquery~20 mins

AVG function in MySQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
AVG Function Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2: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);
A3
B18000
C6000
DNULL
Attempts:
2 left
💡 Hint
AVG calculates the average of numeric values in a column.
query_result
intermediate
2:00remaining
Average with NULL values
Consider the Scores table with columns 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);
A56.6667
B85
CNULL
D170
Attempts:
2 left
💡 Hint
AVG ignores NULL values when calculating the average.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in AVG usage
Which option contains a syntax error when using the AVG function in MySQL?
ASELECT AVG salary FROM Employees;
BSELECT AVG(salary) AS average_salary FROM Employees;
CSELECT AVG(salary) FROM Employees;
DSELECT AVG(salary + bonus) FROM Employees;
Attempts:
2 left
💡 Hint
AVG requires parentheses around the column or expression.
optimization
advanced
2: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);
ASELECT AVG(salary) FROM Employees WHERE join_year >= 2020;
BSELECT AVG(salary) FROM (SELECT * FROM Employees WHERE join_year > 2020) AS filtered;
CSELECT AVG(salary) FROM Employees;
DSELECT AVG(salary) FROM Employees WHERE join_year > 2020;
Attempts:
2 left
💡 Hint
Filtering rows before aggregation is more efficient than subqueries.
🧠 Conceptual
expert
3:00remaining
AVG function with GROUP BY and NULLs
Given the Sales table with columns 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
A[{"region": "East", "avg_amount": null}, {"region": "North", "avg_amount": 100}, {"region": "South", "avg_amount": 250}]
B[{"region": "East", "avg_amount": 0}, {"region": "North", "avg_amount": 50}, {"region": "South", "avg_amount": 250}]
C[{"region": "East", "avg_amount": null}, {"region": "North", "avg_amount": 50}, {"region": "South", "avg_amount": 250}]
D[{"region": "East", "avg_amount": 0}, {"region": "North", "avg_amount": 100}, {"region": "South", "avg_amount": 250}]
Attempts:
2 left
💡 Hint
AVG ignores NULLs; if all values are NULL, AVG returns NULL for that group.