CodeBlanks Logo Icon CodeBlanks

Multiple conditions

Use AND and OR inside HAVING.

Multiple conditions

You can combine multiple conditions in HAVING with AND and OR.

SELECT category, COUNT(*), SUM(price)
FROM products
GROUP BY category
HAVING COUNT(*) > 3 AND SUM(price) > 200;

This query returns categories with many products and high totals.

category, COUNT(*), SUM(price) is selected from products. Rows are grouped by category; aggregate functions are computed per group. HAVING COUNT(*) > 3 AND SUM(price) > 200 runs after grouping and drops groups that fail the condition.

Multiple conditions — details

AND requires both conditions, OR at least one. Parentheses set precedence.

For example, HAVING COUNT(*) > 3 AND SUM(price) > 200 keeps categories that are both large and high in total value.

Practice Multiple conditions

Combine COUNT(*) > 3 AND SUM(price) > 200 in HAVING.

Starter code

query.sql
-- two conditions
SELECT category, COUNT(*), SUM(price)
FROM products
GROUP BY category

Sample solution

query.sql
SELECT category, COUNT(*), SUM(price)
FROM products
GROUP BY category
HAVING COUNT(*) > 3 AND SUM(price) > 200;

Expected output

category  count  sum
Elektronik 5      350

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 Add AND

Question
HAVING COUNT(*) > 3 ____ SUM(price) > 200;
Answer
HAVING COUNT(*) > 3 AND SUM(price) > 200;

Explanation: Two HAVING conditions are combined with AND; a group must meet both.

2 What does AND require?

  • Only one of them
  • None
  • Some of them
  • Both conditions Correct answer

Explanation: AND requires both conditions to be true.

3 Multiple conditions: match the terms

Term Meaning
AND both
OR at least one
NOT not
HAVING group filter

All lessons in Intermediate