MySQL Aggregate Functions and Group by - Exercises, Practice, Solution
MySQL Aggregate Functions and Group by - Exercises, Practice, Solution
5. Write a query to get the average salary and number of employees working the
department 90.
6. Write a query to get the highest, lowest, sum, and average salary of all
employees.
Sample table: employees
7. Write a query to get the number of employees with the same job.
8. Write a query to get the difference between the highest and lowest salaries.
9. Write a query to find the manager ID and the salary of the lowest-paid
employee for that manager.
ACCENTURE BATCH 2 LABORATORY
10. Write a query to get the department ID and the total salary payable in each
department.
Page | 2
Sample table: employees
11. Write a query to get the average salary for each job ID excluding
programmer.
12. Write a query to get the total salary, maximum, minimum, average salary of
employees (job ID wise), for department ID 90 only.
13. Write a query to get the job ID and maximum salary of the employees where
maximum salary is greater than or equal to $4000.
14. Write a query to get the average salary for all departments employing more
than 10 employees.
Page | 3
ACCENTURE BATCH 2 LABORATORY
SOLUTION
Page | 4
MySQL Aggregate Function: Exercise-1 with Solution
Write a query to list the number of jobs available in the employees table.
Code:
SELECT COUNT(DISTINCT job_id)
FROM employees;
Copy
Page | 5
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT SUM(salary)
FROM employees;
Copy
Page | 7
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT MIN(salary)
FROM employees;
Copy
Page | 9
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT MAX(salary)
FROM employees
Copy
Page | 11
ACCENTURE BATCH 2 LABORATORY
Page | 12
ACCENTURE BATCH 2 LABORATORY
Write a query to get the average salary and number of employees working the
Page | 13 department 90.
Code:
SELECT AVG(salary),count(*)
FROM employees
Copy
Page | 14
ACCENTURE BATCH 2 LABORATORY
Page | 15
ACCENTURE BATCH 2 LABORATORY
Write a query to get the highest, lowest, sum, and average salary of all
Page | 16 employees.
Code:
SELECT ROUND(MAX(salary),0) 'Maximum',
ROUND(MIN(salary),0) 'Minimum',
ROUND(SUM(salary),0) 'Sum',
ROUND(AVG(salary),0) 'Average'
FROM employees;
Copy
ACCENTURE BATCH 2 LABORATORY
Page | 17
ACCENTURE BATCH 2 LABORATORY
Write a query to get the number of employees with the same job.
Page | 18
Sample table: employees
Code:
SELECT job_id, COUNT(*)
FROM employees
GROUP BY job_id;
Copy
Page | 19
ACCENTURE BATCH 2 LABORATORY
Code:
SELECT MAX(salary) - MIN(salary) DIFFERENCE
FROM employees;
Copy
ACCENTURE BATCH 2 LABORATORY
Page | 21
<
ACCENTURE BATCH 2 LABORATORY
Write a query to find the manager ID and the salary of the lowest-paid employee
Page | 22 for that manager.
Code:
SELECT manager_id, MIN(salary)
FROM employees
GROUP BY manager_id
Copy
Page | 23
ACCENTURE BATCH 2 LABORATORY
Page | 24
ACCENTURE BATCH 2 LABORATORY
Write a query to get the department ID and the total salary payable in each
Page | 25 department.
Code:
SELECT department_id, SUM(salary)
FROM employees
GROUP BY department_id;
Copy
Page | 26
ACCENTURE BATCH 2 LABORATORY
Write a query to get the average salary for each job ID excluding programmer.
Page | 27
Sample table: employees
Code:
SELECT job_id, AVG(salary)
FROM employees
GROUP BY job_id;
Copy
Page | 28
ACCENTURE BATCH 2 LABORATORY
Write a query to get the total salary, maximum, minimum, average salary of
Page | 29 employees (job ID wise), for department ID 90 only.
Code:
SELECT job_id, SUM(salary), AVG(salary), MAX(salary), MIN(salary)
FROM employees
GROUP BY job_id;
Copy
Page | 30
ACCENTURE BATCH 2 LABORATORY
Write a query to get the job ID and maximum salary of the employees where
Page | 31 maximum salary is greater than or equal to $4000.
Code:
SELECT job_id, MAX(salary)
FROM employees
GROUP BY job_id
Copy
Page | 32
ACCENTURE BATCH 2 LABORATORY
Write a query to get the average salary for all departments employing more than
Page | 33 10 employees.
Code:
SELECT department_id, AVG(salary), COUNT(*)
FROM employees
GROUP BY department_id
Copy
Page | 34
ACCENTURE BATCH 2 LABORATORY
Page | 35