SELECT COUNT(*), SUM(price), AVG(price) FROM products WHERE price > 'ten';
medium
A. Comparing price (numeric) with string 'ten' causes error.
B. Using COUNT(*) with WHERE clause is invalid.
C. SUM(price) cannot be used with AVG(price) in same query.
D. Missing GROUP BY clause for aggregate functions.
Solution
Step 1: Analyze WHERE clause condition
The condition price > 'ten' compares a numeric column to a string, which causes a type error in PostgreSQL.
Step 2: Check other parts of the query
COUNT(*) with WHERE is valid; SUM and AVG can be used together; GROUP BY not needed without grouping.
Final Answer:
Comparing price (numeric) with string 'ten' causes error. -> Option A
Quick Check:
Numeric compared to string causes error [OK]
Hint: Compare numeric columns only with numbers, not strings [OK]
Common Mistakes:
Assuming COUNT(*) cannot be used with WHERE
Thinking GROUP BY is always required with aggregates
Mixing data types in WHERE conditions
5. You have a table orders with columns customer_id and total_amount. How do you write a query to find the average order amount for each customer, but only include customers with at least 5 orders?
hard
A. SELECT customer_id, AVG(total_amount) FROM orders GROUP BY customer_id HAVING COUNT(*) >= 5;
B. SELECT customer_id, AVG(total_amount) FROM orders WHERE COUNT(*) >= 5 GROUP BY customer_id;
C. SELECT customer_id, AVG(total_amount) FROM orders GROUP BY customer_id WHERE COUNT(*) >= 5;
D. SELECT customer_id, AVG(total_amount) FROM orders HAVING COUNT(*) >= 5 GROUP BY customer_id;
Solution
Step 1: Group orders by customer_id
Use GROUP BY customer_id to aggregate orders per customer.
Step 2: Filter groups with HAVING clause
HAVING COUNT(*) >= 5 filters customers with at least 5 orders.
Step 3: Calculate average order amount per customer
Use AVG(total_amount) to find average order value per customer.
Final Answer:
SELECT customer_id, AVG(total_amount) FROM orders GROUP BY customer_id HAVING COUNT(*) >= 5; -> Option A
Quick Check:
GROUP BY + HAVING filters groups [OK]
Hint: Use HAVING to filter groups after GROUP BY [OK]
Common Mistakes:
Using WHERE instead of HAVING for group filters
Placing HAVING before GROUP BY
Not grouping by customer_id
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.