Hint: AVG ignores NULLs and returns float average [OK]
Common Mistakes:
Including NULL in average calculation
Expecting integer instead of float result
Confusing AVG with SUM or COUNT
4. Consider this query: SELECT AVG(price) FROM products WHERE price > 0; It returns NULL even though there are products with positive prices. What is the likely cause?
medium
A. The price column contains only NULL values
B. The WHERE clause filters out all rows
C. AVG() cannot be used with WHERE clause
D. The table products does not exist
Solution
Step 1: Understand AVG() behavior with WHERE clause
The WHERE clause filters rows before AVG() calculates the average.
Step 2: Analyze why AVG() returns NULL
If the WHERE clause excludes all rows (no price > 0), AVG() has no values to average and returns NULL.
Final Answer:
The WHERE clause filters out all rows -> Option B
Quick Check:
Empty filtered rows cause AVG() to return NULL [OK]
Hint: Check WHERE filters; no rows means AVG() returns NULL [OK]
Common Mistakes:
Assuming AVG() ignores WHERE clause
Thinking AVG() returns 0 if no rows
Believing AVG() cannot be combined with WHERE
5. You have a sales table with columns region and amount. How do you write a query to find the average sales amount per region, excluding regions with no sales?
hard
A. SELECT region, AVG(amount) FROM sales GROUP BY region HAVING AVG(amount) > 0;
B. SELECT region, AVG(amount) FROM sales WHERE amount IS NOT NULL GROUP BY region;
C. SELECT region, AVG(amount) FROM sales GROUP BY region WHERE amount > 0;
D. SELECT region, AVG(amount) FROM sales GROUP BY region HAVING amount > 0;
Solution
Step 1: Group sales by region
Use GROUP BY region to calculate averages per region.
Step 2: Filter out regions with no sales
Use HAVING AVG(amount) > 0 to exclude groups where average is NULL or 0 (no sales).
Step 3: Check other options
SELECT region, AVG(amount) FROM sales WHERE amount IS NOT NULL GROUP BY region; (SELECT region, AVG(amount) FROM sales WHERE amount IS NOT NULL GROUP BY region;) filters NULL rows before grouping but includes regions where non-NULL amounts average to 0. Options C and D have syntax errors or misuse WHERE with GROUP BY.
Final Answer:
SELECT region, AVG(amount) FROM sales GROUP BY region HAVING AVG(amount) > 0; -> Option A
Quick Check:
Use HAVING to filter groups by AVG() [OK]
Hint: Use HAVING to filter groups after GROUP BY [OK]
Common Mistakes:
Using WHERE instead of HAVING to filter groups
Not excluding NULL averages
Misplacing WHERE clause after GROUP BY
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.