CodeEraFree roadmap

SQL Practice / HAVING clause

SQL HAVING Clause: practice questions with answers

9 questions with solved queries and real output. Outputs checked on MariaDB 10.11 (MySQL-compatible) on 23 November 2026.

HAVING filters groups after GROUP BY has built them. WHERE filters rows before grouping, so it cannot use aggregate functions like COUNT(*) or AVG(salary). If you want to filter on an aggregate, you need HAVING.

The order of a query is: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.

SELECT column, COUNT(*)
FROM table_name
WHERE row_condition
GROUP BY column
HAVING COUNT(*) > 1;

These questions use the employees table (30 rows). Create it first with the copy-and-paste setup script. Here is the data, so you can check your answers by eye.

See the employees table

Table: employees

emp_nofirst_namelast_namesalarybirth_dategenderjoining_datedepartment
1001JamesBond453001998-01-19M2020-11-29Marketing
1002RobertLee694101998-09-02M2019-12-26IT
1003MaryWood395181997-11-07F2019-09-12Business Development
1004JohnCole362641999-02-01M2021-04-14HR
1005JenniferGill489411996-06-05F2019-06-28Marketing
1006MichaelWatts238001999-04-16M2022-06-09HR
1007DavidMills394001998-04-26M2021-10-17IT
1008WilliamHill263921999-10-21M2019-09-30Business Development
1009LindaPotter552311998-01-24F2021-08-07Finance
1010RichardMartin256001996-08-07M2021-03-23IT
1011SusanHall541521999-12-27F2020-08-07Finance
1012JosephTurner605561997-05-13M2019-09-02Marketing
1013MaryGill582461999-01-21F2021-04-30Business Development
1014ThomasRyan236761996-10-12M2022-03-02IT
1015CharlesWilliams572262000-01-29M2019-11-05NULL
1016JessicaClarkNULL2000-01-25F2020-01-24IT
1017NancyParker669001996-10-24F2022-01-09Finance
1018BettyBarker352881998-11-13F2019-12-26HR
1019ChristopherGibson593411998-04-23M2019-05-02Marketing
1020DanielElliott440121999-01-21M2023-01-08IT
1021DavidMillsNULL1998-01-24M2019-12-26IT
1022SarahHarrison446921996-03-20F2020-05-03Finance
1023MatthewHill413651998-05-16M2019-05-23IT
1024AnthonyMiller567941996-06-30M2019-02-28Business Development
1025MarkWood651201998-10-29M2020-07-30Business Development
1026ElizabethWalker470301998-08-11F2021-06-18NULL
1027BarbaraAndersonNULL1999-12-27F2021-03-14HR
1028JessicaClark638361998-01-24F2021-05-23IT
1029NancyCole636692000-01-26F2019-12-26Finance
1030JoeBrooks440001999-08-05M2021-03-09Business Development

How to use this page: read a question, write your own query, then open "Show the answer" and compare. Trying first is what makes it stick.

Question 1Count the number of duplicate first names as 'duplicate_count'.

Show the answer and the output
SELECT first_name, COUNT(*) AS duplicate_count
FROM employees
GROUP BY first_name
HAVING COUNT(*) > 1;

Output

first_nameduplicate_count
David2
Jessica2
Mary2
Nancy2

Question 2Find the departments with more than 5 employees as 'total_employees'.

Show the answer and the output
SELECT department, COUNT(*) AS total_employees
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

Output

departmenttotal_employees
Business Development6
IT9

Question 3Find the departments with an average salary greater than 50000 as 'average_salary'.

Show the answer and the output
SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department
HAVING AVG(salary) > 50000;

Output

departmentaverage_salary
NULL52128.0000
Finance56928.8000
Marketing53534.5000

Question 4Find the departments with at least 3 employees earning 30000 or more, as 'total_employees'.

Show the answer and the output
SELECT department, COUNT(*) AS total_employees
FROM employees
WHERE salary >= 30000
GROUP BY department
HAVING COUNT(*) >= 3;

Output

departmenttotal_employees
Business Development5
Finance5
IT5
Marketing4

WHERE removes the low earners first, and then HAVING counts only who is left in each department.

Question 5Find the departments with a maximum salary greater than 60000 as 'max_salary'.

Show the answer and the output
SELECT department, MAX(salary) AS max_salary
FROM employees
GROUP BY department
HAVING MAX(salary) > 60000;

Output

departmentmax_salary
Business Development65120
Finance66900
IT69410
Marketing60556

Question 6Find the departments where the highest salary ('highest_salary') is at least twice the lowest salary ('lowest_salary').

Show the answer and the output
SELECT department, MAX(salary) AS highest_salary, MIN(salary) AS lowest_salary
FROM employees
GROUP BY department
HAVING MAX(salary) >= 2 * MIN(salary);

Output

departmenthighest_salarylowest_salary
Business Development6512026392
IT6941023676

Question 7Find the departments with at least 5 employees ('total_employees') where the total salary expense ('total_salary_expense') is greater than 100000.

Show the answer and the output
SELECT department, COUNT(*) AS total_employees, SUM(salary) AS total_salary_expense
FROM employees
GROUP BY department
HAVING COUNT(*) >= 5 AND SUM(salary) > 100000;

Output

departmenttotal_employeestotal_salary_expense
Business Development6290070
Finance5284644
IT9307299

Question 8Find the departments where the average salary ('average_salary') is between 20000 and 50000, inclusive.

Show the answer and the output
SELECT department, AVG(salary) AS average_salary
FROM employees
GROUP BY department
HAVING AVG(salary) BETWEEN 20000 AND 50000;

Output

departmentaverage_salary
Business Development48345.0000
HR31784.0000
IT43899.8571

Question 9Find the departments with more than 4 employees ('total_employees') where the difference between the maximum and minimum salary ('salary_difference') is greater than 25000.

Show the answer and the output
SELECT department,
       COUNT(*) AS total_employees,
       MAX(salary) - MIN(salary) AS salary_difference
FROM employees
GROUP BY department
HAVING COUNT(*) > 4 AND MAX(salary) - MIN(salary) > 25000;

Output

departmenttotal_employeessalary_difference
Business Development638728
IT945734

Want these as PDFs? Download the question PDF or the answer PDF to practise offline.