0
0
PostgreSQLquery~20 mins

HAVING for filtering groups in PostgreSQL - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
HAVING Clause Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Filtering groups with HAVING clause
Given a table sales with columns region and amount, what is the output of this query?

SELECT region, SUM(amount) AS total_sales
FROM sales
GROUP BY region
HAVING SUM(amount) > 1000;
PostgreSQL
CREATE TABLE sales (region TEXT, amount INT);
INSERT INTO sales VALUES
('North', 500), ('North', 600), ('South', 400), ('South', 300), ('East', 1200), ('West', 800);
A[{"region": "North", "total_sales": 1100}]
B[{"region": "North", "total_sales": 1100}, {"region": "East", "total_sales": 1200}]
C[{"region": "East", "total_sales": 1200}, {"region": "West", "total_sales": 800}]
D[{"region": "North", "total_sales": 1100}, {"region": "South", "total_sales": 700}, {"region": "East", "total_sales": 1200}]
Attempts:
2 left
💡 Hint
Remember HAVING filters groups after aggregation, unlike WHERE which filters rows before grouping.
🧠 Conceptual
intermediate
1:30remaining
Understanding HAVING vs WHERE
Which statement correctly explains the difference between WHERE and HAVING clauses in SQL?
AWHERE filters groups after aggregation; HAVING filters groups before aggregation.
BWHERE filters groups after aggregation; HAVING filters rows before grouping.
CWHERE and HAVING both filter rows before grouping but HAVING is faster.
DWHERE filters rows before grouping; HAVING filters groups after aggregation.
Attempts:
2 left
💡 Hint
Think about when aggregation happens in the query process.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in HAVING clause
Which option contains a syntax error in the HAVING clause?
PostgreSQL
SELECT department, COUNT(*) AS emp_count
FROM employees
GROUP BY department
HAVING emp_count > 5;
AHAVING emp_count > 5
BHAVING COUNT(*) > 5
CHAVING COUNT(emp_count) > 5
DHAVING SUM(emp_count) > 5
Attempts:
2 left
💡 Hint
Can you use column aliases directly in HAVING in PostgreSQL?
optimization
advanced
2:00remaining
Optimizing HAVING with WHERE
Consider this query:
SELECT category, SUM(price) AS total_price
FROM products
WHERE price > 10
GROUP BY category
HAVING SUM(price) > 100;

What is the main benefit of using the WHERE clause here?
AIt reduces the number of rows before grouping, improving query performance.
BIt allows filtering on aggregated values directly.
CIt filters groups after aggregation, so fewer groups are returned.
DIt sorts the results before grouping.
Attempts:
2 left
💡 Hint
Think about when WHERE filters data compared to HAVING.
🔧 Debug
expert
2:30remaining
Debugging unexpected HAVING results
A developer runs this query:
SELECT customer_id, COUNT(order_id) AS order_count
FROM orders
GROUP BY customer_id
HAVING order_count > 3;

But it returns a syntax error. What is the cause?
AGROUP BY must include order_count column.
BCOUNT(order_id) cannot be used in HAVING clause.
CUsing the alias 'order_count' in HAVING is not allowed in PostgreSQL.
DHAVING clause must come before GROUP BY.
Attempts:
2 left
💡 Hint
Check if column aliases can be used in HAVING in PostgreSQL.