0% found this document useful (0 votes)
0 views

MySQL_GroupBy_Having_Practice

This document contains a collection of MySQL queries focused on the GROUP BY and HAVING clauses, organized into Basic, Intermediate, and Advanced levels. Each section includes specific questions along with corresponding SQL solutions related to the 'employees' table. The queries cover various aspects such as counting employees, averaging employee numbers, and filtering results based on conditions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

MySQL_GroupBy_Having_Practice

This document contains a collection of MySQL queries focused on the GROUP BY and HAVING clauses, organized into Basic, Intermediate, and Advanced levels. Each section includes specific questions along with corresponding SQL solutions related to the 'employees' table. The queries cover various aspects such as counting employees, averaging employee numbers, and filtering results based on conditions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

MySQL GROUP BY & HAVING - Practice Questions & Solutions

Introduction
This document provides a structured set of MySQL queries based on the 'employees' table, focusing
on GROUP BY and HAVING clauses. The questions are categorized into Basic, Intermediate, and
Advanced levels, each with detailed SQL solutions.

Basic Level Questions & Answers

Q1. Count the number of employees in each office.


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

Q2. Find the number of employees per job title.


SQL Query:
SELECT jobTitle, COUNT(*) AS employee_count FROM employees GROUP BY jobTitle ORDER
BY employee_count DESC;

Q3. Count how many employees report to each manager.


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

Q4. Find the total number of employees in each office, but only include offices with more
than 5 employees.
SQL Query:
SELECT officeCode, COUNT(*) AS employee_count FROM employees GROUP BY officeCode
HAVING COUNT(*) > 5;

Q5. Find job titles that have more than 3 employees.


SQL Query:
SELECT jobTitle, COUNT(*) FROM employees GROUP BY jobTitle HAVING COUNT(*) > 3;

Intermediate Level Questions & Answers


Q6. Find the average employeeNumber per job title.
SQL Query:
SELECT jobTitle, AVG(employeeNumber) AS avg_emp_num FROM employees GROUP BY
jobTitle;

Q7. List offices where the number of employees is above the average number of employees
per office.
SQL Query:
SELECT officeCode, COUNT(*) AS employee_count FROM employees GROUP BY officeCode
HAVING COUNT(*) > (SELECT AVG(emp_count) FROM (SELECT COUNT(*) AS emp_count
FROM employees GROUP BY officeCode) AS avg_table);

Q8. Find the number of employees for each unique combination of job title and office code.
SQL Query:
SELECT jobTitle, officeCode, COUNT(*) FROM employees GROUP BY jobTitle, officeCode
ORDER BY officeCode;

Q9. Find the officeCode with the maximum number of employees.


SQL Query:
SELECT officeCode FROM employees GROUP BY officeCode ORDER BY COUNT(*) DESC LIMIT
1;

Q10. Retrieve employees who have an extension starting with 'x1' and ending with '5'.
SQL Query:
SELECT * FROM employees WHERE extension LIKE 'x1%5';

Advanced Level Questions & Answers

You might also like