0
0
PostgreSQLquery~20 mins

Why filtering behavior matters in PostgreSQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Filtering Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
query_result
intermediate
2:00remaining
Filtering rows with WHERE vs HAVING

Consider a table sales with columns region, product, and amount. Which query correctly returns regions with total sales greater than 1000?

PostgreSQL
SELECT region, SUM(amount) AS total_sales
FROM sales
WHERE amount > 100
GROUP BY region
HAVING SUM(amount) > 1000;
AReturns regions where each individual sale is over 100 and total sales per region exceed 1000.
BReturns regions where total sales per region exceed 1000, ignoring individual sale amounts.
CReturns all regions regardless of sales amounts.
DReturns an error because WHERE cannot be used with GROUP BY.
Attempts:
2 left
💡 Hint

Remember that WHERE filters rows before grouping, and HAVING filters groups after aggregation.

🧠 Conceptual
intermediate
1:30remaining
Effect of filter order on query results

Why does the order of filtering conditions matter in SQL queries involving aggregation?

ABecause <code>WHERE</code> filters rows before aggregation, and <code>HAVING</code> filters after aggregation, changing the order changes results.
BBecause <code>HAVING</code> always runs before <code>WHERE</code>, so order does not matter.
CBecause filters in <code>WHERE</code> and <code>HAVING</code> are combined and run simultaneously.
DBecause <code>WHERE</code> filters groups and <code>HAVING</code> filters individual rows.
Attempts:
2 left
💡 Hint

Think about when rows are filtered versus when groups are filtered.

📝 Syntax
advanced
1:30remaining
Identify the syntax error in filtering query

Which option contains a syntax error in filtering aggregated data?

PostgreSQL
SELECT department, COUNT(*) AS emp_count
FROM employees
GROUP BY department
WHERE emp_count > 10;
AThe query is correct and will run without errors.
BSyntax error because GROUP BY must come after HAVING.
CSyntax error because COUNT(*) cannot be used in HAVING clause.
DSyntax error because WHERE cannot be used after GROUP BY; HAVING should be used instead.
Attempts:
2 left
💡 Hint

Check the order of clauses in SQL and which clause filters aggregated results.

optimization
advanced
2:00remaining
Optimizing filtering for large datasets

Given a large orders table, which filtering approach is more efficient to find customers with more than 5 orders?

PostgreSQL
SELECT customer_id, COUNT(*) AS order_count
FROM orders
GROUP BY customer_id
HAVING COUNT(*) > 5;
AFilter rows with WHERE before grouping using COUNT(*) > 5 condition.
BUse a subquery to filter customers with more than 5 orders before grouping.
CAdd an index on customer_id to speed up grouping and filtering.
DUse HAVING after GROUP BY as shown; it is efficient for large datasets.
Attempts:
2 left
💡 Hint

Think about how indexes affect query performance on large tables.

🔧 Debug
expert
2:30remaining
Debugging unexpected filtering results

A query returns no rows but you expect some. The query is:

SELECT category, AVG(price) AS avg_price
FROM products
WHERE price < 200
GROUP BY category
HAVING AVG(price) > 150;

What is the most likely reason for no results?

AAVG(price) cannot be used in HAVING clause, causing no results.
BThe WHERE clause filters out all rows with price ≥ 200 before grouping, so groups have fewer rows, lowering AVG(price).
CHAVING clause is filtering groups incorrectly; it should be in WHERE.
DGROUP BY category is missing, causing aggregation error.
Attempts:
2 left
💡 Hint

Consider how WHERE affects which rows are included before aggregation.