0% found this document useful (0 votes)
4 views3 pages

MySQL GroupBy Having Full Practice

The document contains a series of SQL practice questions and their corresponding queries focused on the MySQL 'GROUP BY' and 'HAVING' clauses. Each question addresses different scenarios, such as counting employees by office, job title, and manager, as well as filtering results based on specific conditions. The queries are designed to help users practice and understand the application of these SQL features in various contexts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

MySQL GroupBy Having Full Practice

The document contains a series of SQL practice questions and their corresponding queries focused on the MySQL 'GROUP BY' and 'HAVING' clauses. Each question addresses different scenarios, such as counting employees by office, job title, and manager, as well as filtering results based on specific conditions. The queries are designed to help users practice and understand the application of these SQL features in various contexts.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

MySQL Group By & Having - Practice Questions with Answers

-- 1. Count the number of employees in each office.


SELECT officeCode, COUNT(*) AS employee_count FROM employees GROUP BY
officeCode;

-- 2. Find the number of employees per job title.


SELECT jobTitle, COUNT(*) AS job_count FROM employees GROUP BY jobTitle ORDER BY
job_count DESC;

-- 3. Count how many employees report to each manager.


SELECT reportsTo, COUNT(*) AS report_count FROM employees GROUP BY reportsTo
ORDER BY report_count DESC;

-- 4. Find the total number of employees in each department (job title-based).


SELECT jobTitle, COUNT(*) AS department_count FROM employees GROUP BY jobTitle;

-- 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;

-- 6. Find the number of employees per job title in each office.


SELECT officeCode, jobTitle, COUNT(*) FROM employees GROUP BY officeCode,
jobTitle;

-- 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;

-- 8. Retrieve job titles that have exactly 2 employees.


SELECT jobTitle, COUNT(*) FROM employees GROUP BY jobTitle HAVING COUNT(*) = 2;

-- 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;

-- 20. Retrieve managers with at least 2 employees reporting to them.


SELECT reportsTo, COUNT(*) FROM employees GROUP BY reportsTo HAVING COUNT(*) >=
2;
-- 21. Find the most common job title in each office.
SELECT officeCode, jobTitle, COUNT(*) FROM employees GROUP BY officeCode,
jobTitle ORDER BY COUNT(*) DESC;

-- 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;

-- 24. Show the percentage of employees in each job title.


SELECT jobTitle, COUNT(*) * 100 / (SELECT COUNT(*) FROM employees) AS percentage
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';

You might also like