Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Recall & Review
beginner
What is the purpose of the HAVING clause in SQL?
The HAVING clause is used to filter groups created by GROUP BY based on a condition, similar to how WHERE filters rows before grouping.
Click to reveal answer
beginner
How does HAVING differ from WHERE in SQL queries?
WHERE filters individual rows before grouping, while HAVING filters groups after aggregation is done.
Click to reveal answer
beginner
Write a simple SQL query using HAVING to find departments with more than 5 employees.
SELECT department, COUNT(*) AS employee_count FROM employees GROUP BY department HAVING COUNT(*) > 5;
Click to reveal answer
intermediate
Can HAVING be used without GROUP BY? Explain.
Yes, HAVING can be used without GROUP BY to filter aggregated results over the entire table, but it is less common.
Click to reveal answer
beginner
Why do aggregate functions like COUNT() or SUM() often appear in HAVING clauses?
Because HAVING filters groups based on aggregated values, aggregate functions are used to specify conditions on those groups.
Click to reveal answer
What does the HAVING clause filter in an SQL query?
AGroups after aggregation
BColumns in the SELECT statement
CIndividual rows before grouping
DDatabase tables
✗ Incorrect
HAVING filters groups after aggregation, unlike WHERE which filters rows before grouping.
Which clause is used to filter rows before grouping in SQL?
AORDER BY
BGROUP BY
CHAVING
DWHERE
✗ Incorrect
WHERE filters rows before grouping, HAVING filters groups after aggregation.
Which aggregate function is commonly used in HAVING clauses?
AROUND()
BNOW()
CCOUNT()
DSUBSTRING()
✗ Incorrect
COUNT() is an aggregate function often used in HAVING to filter groups by their size.
Can HAVING be used without GROUP BY in SQL?
ANo, it always requires GROUP BY
BYes, but it filters aggregated results over the entire table
CYes, it filters individual rows
DNo, HAVING is only for sorting
✗ Incorrect
HAVING can filter aggregated results without GROUP BY, but this is less common.
Which clause would you use to find groups with SUM(sales) greater than 1000?
AHAVING SUM(sales) > 1000
BWHERE SUM(sales) > 1000
CGROUP BY SUM(sales) > 1000
DORDER BY SUM(sales) > 1000
✗ Incorrect
HAVING is used to filter groups based on aggregate conditions like SUM(sales) > 1000.
Explain the difference between WHERE and HAVING clauses in SQL.
Think about when filtering happens in the query process.
You got /4 concepts.
Write an example SQL query using HAVING to show only groups with an average score above 80.
Use GROUP BY and HAVING with AVG() function.
You got /3 concepts.
Practice
(1/5)
1.
What is the main purpose of the HAVING clause in SQL?
easy
A. To filter individual rows before grouping
B. To sort the results of a query
C. To filter groups created by GROUP BY based on aggregate conditions
D. To join two tables together
Solution
Step 1: Understand the role of GROUP BY
The GROUP BY clause groups rows based on column values.
Step 2: Identify the purpose of HAVING
HAVING filters these groups using aggregate functions like SUM or COUNT.
Final Answer:
To filter groups created by GROUP BY based on aggregate conditions -> Option C
Quick Check:
HAVING filters groups, not rows [OK]
Hint: Remember: WHERE filters rows, HAVING filters groups [OK]
Common Mistakes:
Confusing HAVING with WHERE clause
Using HAVING without GROUP BY
Thinking HAVING sorts data
2.
Which of the following is the correct syntax to filter groups with HAVING?
SELECT department, COUNT(*) FROM employees GROUP BY department _______ COUNT(*) > 5;
easy
A. GROUP BY
B. WHERE
C. FILTER
D. HAVING
Solution
Step 1: Identify filtering clause after grouping
After GROUP BY, filtering groups requires HAVING, not WHERE.
Step 2: Confirm correct clause usage
HAVING COUNT(*) > 5 filters groups with more than 5 employees.
Final Answer:
HAVING -> Option D
Quick Check:
Use HAVING after GROUP BY [OK]
Hint: Use HAVING to filter groups, not WHERE [OK]
Common Mistakes:
Using WHERE instead of HAVING after GROUP BY
Placing HAVING before GROUP BY
Using FILTER keyword which is invalid here
3.
Given the table sales with columns region and amount, what will this query return?
SELECT region, SUM(amount) FROM sales GROUP BY region HAVING SUM(amount) > 1000;
medium
A. All regions with total sales greater than 1000
B. All regions with total sales less than or equal to 1000
C. All sales records with amount greater than 1000
D. Syntax error due to HAVING clause
Solution
Step 1: Group sales by region and sum amounts
The query groups rows by region and calculates total amount per region.
Step 2: Filter groups with total sales > 1000
The HAVING clause keeps only regions where the sum is greater than 1000.
Final Answer:
All regions with total sales greater than 1000 -> Option A
Quick Check:
HAVING filters groups by aggregate sum [OK]
Hint: HAVING filters groups by aggregate results [OK]
Common Mistakes:
Thinking HAVING filters individual rows
Confusing SUM(amount) with amount column
Assuming syntax error with HAVING
4.
Identify the error in this query:
SELECT category, COUNT(*) FROM products HAVING COUNT(*) > 10 GROUP BY category;
medium
A. Missing WHERE clause before HAVING
B. HAVING clause used before GROUP BY
C. COUNT(*) cannot be used in HAVING
D. GROUP BY should be replaced with ORDER BY
Solution
Step 1: Check order of clauses in SQL
The correct order is GROUP BY first, then HAVING.
Step 2: Identify incorrect clause order
The query places HAVING before GROUP BY, causing syntax error.
Final Answer:
HAVING clause used before GROUP BY -> Option B
Quick Check:
GROUP BY before HAVING [OK]
Hint: GROUP BY must come before HAVING [OK]
Common Mistakes:
Placing HAVING before GROUP BY
Thinking HAVING needs WHERE before it
Replacing GROUP BY with ORDER BY incorrectly
5.
You have a students table with columns class and score. You want to find classes where the average score is at least 75 and the number of students is more than 10. Which query achieves this?
hard
A. SELECT class, AVG(score), COUNT(*) FROM students GROUP BY class HAVING AVG(score) >= 75 AND COUNT(*) > 10;
B. SELECT class, AVG(score), COUNT(*) FROM students HAVING AVG(score) >= 75 AND COUNT(*) > 10 GROUP BY class;
C. SELECT class, AVG(score), COUNT(*) FROM students WHERE AVG(score) >= 75 AND COUNT(*) > 10 GROUP BY class;
D. SELECT class, AVG(score), COUNT(*) FROM students GROUP BY class WHERE AVG(score) >= 75 AND COUNT(*) > 10;
Solution
Step 1: Group students by class
Use GROUP BY class to group rows by class.
Step 2: Filter groups with HAVING using aggregate conditions
Use HAVING AVG(score) >= 75 AND COUNT(*) > 10 to keep classes meeting both conditions.
Final Answer:
SELECT class, AVG(score), COUNT(*) FROM students GROUP BY class HAVING AVG(score) >= 75 AND COUNT(*) > 10; -> Option A
Quick Check:
HAVING filters groups by multiple aggregates [OK]
Hint: Use HAVING for multiple aggregate filters after GROUP BY [OK]