0
0
PostgreSQLquery~10 mins

HAVING for filtering groups in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to filter groups having more than 3 orders.

PostgreSQL
SELECT customer_id, COUNT(*) FROM orders GROUP BY customer_id HAVING COUNT(*) [1] 3;
Drag options to blanks, or click blank then click option'
A>
B<
C=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using WHERE instead of HAVING for aggregated conditions.
Using '<' instead of '>' causing wrong filtering.
2fill in blank
medium

Complete the code to select product categories with total sales equal to 1000.

PostgreSQL
SELECT category, SUM(sales) FROM products GROUP BY category HAVING SUM(sales) [1] 1000;
Drag options to blanks, or click blank then click option'
A<
B=
C>
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using WHERE instead of HAVING for aggregate conditions.
Using '>' or '<' when exact match is needed.
3fill in blank
hard

Fix the error in the code to filter groups with average rating at least 4.

PostgreSQL
SELECT product_id, AVG(rating) FROM reviews GROUP BY product_id HAVING AVG(rating) [1] 4;
Drag options to blanks, or click blank then click option'
A<
B=
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' excludes ratings exactly 4.
Using WHERE instead of HAVING for aggregate filtering.
4fill in blank
hard

Fill both blanks to select departments with more than 5 employees and total salary less than 50000.

PostgreSQL
SELECT department, COUNT(*) AS emp_count, SUM(salary) AS total_salary FROM employees GROUP BY department HAVING COUNT(*) [1] 5 AND SUM(salary) [2] 50000;
Drag options to blanks, or click blank then click option'
A>
B<
C>=
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using WHERE instead of HAVING for aggregate conditions.
Mixing up comparison operators causing wrong filtering.
5fill in blank
hard

Fill all three blanks to select cities with average temperature above 20, count of days above 10, and max temperature below 35.

PostgreSQL
SELECT city, AVG(temperature) AS avg_temp, COUNT(*) AS hot_days, MAX(temperature) AS max_temp FROM weather GROUP BY city HAVING AVG(temperature) [1] 20 AND COUNT(*) [2] 10 AND MAX(temperature) [3] 35;
Drag options to blanks, or click blank then click option'
A>
B>=
C<
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using WHERE instead of HAVING for aggregate filters.
Using wrong comparison operators causing incorrect results.