0
0
SQLquery~20 mins

AVG function in SQL - 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 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);
A60000
B180000
CNULL
D3
Attempts:
2 left
💡 Hint
AVG calculates the average value of a numeric column.
🧠 Conceptual
intermediate
2: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?
ANULL
B30
C10
D15
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 AVG in SQL?
SQL
SELECT AVG salary FROM Employees;
ASELECT AVG(salary) FROM Employees;
BSELECT AVG(salary) Employees;
CSELECT AVG salary FROM Employees;
DSELECT AVG(salary) FROM Employees WHERE salary > 50000;
Attempts:
2 left
💡 Hint
AVG requires parentheses around the column name.
optimization
advanced
2: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?
ASELECT AVG(salary) FROM Employees WHERE join_year > 2020;
BSELECT AVG(CASE WHEN join_year > 2020 THEN salary ELSE NULL END) FROM Employees;
CSELECT AVG(salary) FROM Employees;
DSELECT AVG(salary) FROM Employees HAVING join_year > 2020;
Attempts:
2 left
💡 Hint
Filtering rows before aggregation is more efficient.
🔧 Debug
expert
2: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;
AAverage sales_amount per sales_rep with region repeated
BSyntax error due to GROUP BY columns mismatch
CAverage sales_amount per region
DNULL for all rows
Attempts:
2 left
💡 Hint
GROUP BY columns must match selected non-aggregated columns.