MySQL GroupBy Having Full Practice
MySQL GroupBy Having Full Practice
-- 5. List the number of employees per office, but only for offices with more than 5
employees.
SELECT officeCode, COUNT(*) AS emp_count FROM employees GROUP BY officeCode
HAVING COUNT(*) > 5;
-- 7. Find offices where more than 3 employees have the same job title.
SELECT officeCode, jobTitle, COUNT(*) FROM employees GROUP BY officeCode,
jobTitle HAVING COUNT(*) > 3;
-- 9. Count how many employees have each specific extension prefix (first two characters).
SELECT LEFT(extension, 2) AS extension_prefix, COUNT(*) FROM employees GROUP BY
extension_prefix;
-- 10. Count employees whose email domain is the same (after @ symbol).
SELECT SUBSTRING_INDEX(email, '@', -1) AS domain, COUNT(*) FROM employees GROUP
BY domain;
-- 11. Find job titles where the highest employee number is above 1500.
SELECT jobTitle, MAX(employeeNumber) FROM employees GROUP BY jobTitle HAVING
MAX(employeeNumber) > 1500;
-- 12. Find the manager who has the most employees reporting to them.
SELECT reportsTo, COUNT(*) AS report_count FROM employees GROUP BY reportsTo
ORDER BY report_count DESC LIMIT 1;
-- 13. List employees grouped by job title, sorted by the number of employees in descending
order.
SELECT jobTitle, COUNT(*) FROM employees GROUP BY jobTitle ORDER BY COUNT(*)
DESC;
-- 14. Retrieve job titles where the total number of employees is not within the range of 3 to
10.
SELECT jobTitle, COUNT(*) FROM employees GROUP BY jobTitle HAVING COUNT(*) NOT
BETWEEN 3 AND 10;
-- 15. Find the highest and lowest employee number per office.
SELECT officeCode, MAX(employeeNumber) AS max_emp, MIN(employeeNumber) AS
min_emp FROM employees GROUP BY officeCode;
-- 16. List all managers along with the number of direct reports they have.
SELECT reportsTo AS manager_id, COUNT(*) AS num_reports FROM employees GROUP BY
reportsTo;
-- 17. Retrieve job titles where the number of employees is an even number.
SELECT jobTitle, COUNT(*) FROM employees GROUP BY jobTitle HAVING COUNT(*) % 2 =
0;
-- 18. Show offices that have employees with at least 3 different job titles.
SELECT officeCode, COUNT(DISTINCT jobTitle) FROM employees GROUP BY officeCode
HAVING COUNT(DISTINCT jobTitle) >= 3;
-- 19. Find job titles with the maximum and minimum number of employees.
SELECT jobTitle, COUNT(*) FROM employees GROUP BY jobTitle ORDER BY COUNT(*)
DESC LIMIT 1;
-- 22. Retrieve the top 3 offices with the highest number of employees.
SELECT officeCode, COUNT(*) FROM employees GROUP BY officeCode ORDER BY COUNT(*)
DESC LIMIT 3;
-- 23. Find employees grouped by job title, displaying the highest employee number in each
group.
SELECT jobTitle, MAX(employeeNumber) FROM employees GROUP BY jobTitle;
-- 25. Find employees whose job title appears more than twice in their office.
SELECT officeCode, jobTitle, COUNT(*) FROM employees GROUP BY officeCode,
jobTitle HAVING COUNT(*) > 2;
-- 26. Find employees grouped by manager, showing the minimum and maximum employee
numbers under each.
SELECT reportsTo, MIN(employeeNumber), MAX(employeeNumber) FROM employees GROUP
BY reportsTo;
-- 27. Retrieve employees whose job title appears exactly 4 times in the entire company.
SELECT jobTitle, COUNT(*) FROM employees GROUP BY jobTitle HAVING COUNT(*) = 4;
-- 28. Find offices where the job title with the most employees has at least 5 people.
SELECT officeCode, jobTitle, COUNT(*) FROM employees GROUP BY officeCode,
jobTitle HAVING COUNT(*) >= 5;
-- 29. Show managers who manage employees with at least 3 different job titles.
SELECT reportsTo, COUNT(DISTINCT jobTitle) FROM employees GROUP BY reportsTo
HAVING COUNT(DISTINCT jobTitle) >= 3;
-- 30. Retrieve job titles where the highest employee number ends in '2'.
SELECT jobTitle, MAX(employeeNumber) FROM employees GROUP BY jobTitle HAVING
MAX(employeeNumber) LIKE '%2';