SQL Exercises

SQL HAVING Clause Exercises (With Answers)

8 Exercises
~40 min
8 Intermediate

Exercise 1

intermediate

Question

Find departments with more than 5 employees.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT department, COUNT(*) AS employee_count
2FROM employees
3GROUP BY department
4HAVING COUNT(*) > 5;

Expected Output

| department | employee_count |
|---|---|
| Engineering | 8 |

Explanation

HAVING filters groups after aggregation. WHERE filters rows before grouping. Use HAVING with aggregate functions.

Exercise 1

intermediate

Question

Find departments with more than 5 employees.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT department, COUNT(*) AS employee_count
2FROM employees
3GROUP BY department
4HAVING COUNT(*) > 5;

Expected Output

| department | employee_count |
|---|---|
| Engineering | 8 |

Explanation

HAVING filters groups after aggregation. WHERE filters rows before grouping. Use HAVING with aggregate functions.

Exercise 1

intermediate

Question

Find departments with more than 5 employees.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT department, COUNT(*) AS employee_count
2FROM employees
3GROUP BY department
4HAVING COUNT(*) > 5;

Expected Output

| department | employee_count |
|---|---|
| Engineering | 8 |

Explanation

HAVING filters groups after aggregation. WHERE filters rows before grouping. Use HAVING with aggregate functions.

Exercise 1

intermediate

Question

Find departments with more than 5 employees.

Table Schema

SQL
CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department VARCHAR(50)
);
Show Solution

Solution

SQL
1SELECT department, COUNT(*) AS employee_count
2FROM employees
3GROUP BY department
4HAVING COUNT(*) > 5;

Expected Output

| department | employee_count |
|---|---|
| Engineering | 8 |

Explanation

HAVING filters groups after aggregation. WHERE filters rows before grouping. Use HAVING with aggregate functions.

Exercise 2

intermediate

Question

Find products with average rating above 4.0 and at least 10 reviews.

Table Schema

SQL
CREATE TABLE reviews (
  id INT,
  product_id INT,
  rating DECIMAL(2,1)
);
Show Solution

Solution

SQL
1SELECT product_id, AVG(rating) AS avg_rating, COUNT(*) AS review_count
2FROM reviews
3GROUP BY product_id
4HAVING AVG(rating) > 4.0 AND COUNT(*) >= 10;

Expected Output

| product_id | avg_rating | review_count |
|---|---|---|
| 101 | 4.5 | 25 |

Explanation

Multiple conditions in HAVING are combined with AND/OR just like in WHERE clause.

Exercise 2

intermediate

Question

Find products with average rating above 4.0 and at least 10 reviews.

Table Schema

SQL
CREATE TABLE reviews (
  id INT,
  product_id INT,
  rating DECIMAL(2,1)
);
Show Solution

Solution

SQL
1SELECT product_id, AVG(rating) AS avg_rating, COUNT(*) AS review_count
2FROM reviews
3GROUP BY product_id
4HAVING AVG(rating) > 4.0 AND COUNT(*) >= 10;

Expected Output

| product_id | avg_rating | review_count |
|---|---|---|
| 101 | 4.5 | 25 |

Explanation

Multiple conditions in HAVING are combined with AND/OR just like in WHERE clause.

Exercise 2

intermediate

Question

Find products with average rating above 4.0 and at least 10 reviews.

Table Schema

SQL
CREATE TABLE reviews (
  id INT,
  product_id INT,
  rating DECIMAL(2,1)
);
Show Solution

Solution

SQL
1SELECT product_id, AVG(rating) AS avg_rating, COUNT(*) AS review_count
2FROM reviews
3GROUP BY product_id
4HAVING AVG(rating) > 4.0 AND COUNT(*) >= 10;

Expected Output

| product_id | avg_rating | review_count |
|---|---|---|
| 101 | 4.5 | 25 |

Explanation

Multiple conditions in HAVING are combined with AND/OR just like in WHERE clause.

Exercise 2

intermediate

Question

Find products with average rating above 4.0 and at least 10 reviews.

Table Schema

SQL
CREATE TABLE reviews (
  id INT,
  product_id INT,
  rating DECIMAL(2,1)
);
Show Solution

Solution

SQL
1SELECT product_id, AVG(rating) AS avg_rating, COUNT(*) AS review_count
2FROM reviews
3GROUP BY product_id
4HAVING AVG(rating) > 4.0 AND COUNT(*) >= 10;

Expected Output

| product_id | avg_rating | review_count |
|---|---|---|
| 101 | 4.5 | 25 |

Explanation

Multiple conditions in HAVING are combined with AND/OR just like in WHERE clause.

Related Content

From Our Blog

Ready for more practice?

Join SQL Mastery and get access to interactive exercises, quizzes, and more.