You are on page 1of 35

ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Functions and Group by-


Exercises, Practice, Solution
Page | 1 1. Write a query to list the number of jobs available in the employees table

Sample table: employees

2. Write a query to get the total salaries payable to employees.

Sample table: employees

3. Write a query to get the minimum salary from employees table

Sample table: employees

4. Write a query to get the maximum salary of an employee working as a


Programmer.

Sample table: employees

5. Write a query to get the average salary and number of employees working the
department 90.

Sample table: employees

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.

Sample table: employees

8. Write a query to get the difference between the highest and lowest salaries.

Sample table: employees

9. Write a query to find the manager ID and the salary of the lowest-paid
employee for that manager.

Sample table: employees


ACCENTURE BATCH 2 LABORATORY

10. Write a query to get the department ID and the total salary payable in each
department.

Sample table: employees


Page | 2
11. Write a query to get the average salary for each job ID excluding
programmer.

Sample table: employees

12. Write a query to get the total salary, maximum, minimum, average salary of
employees (job ID wise), for department ID 90 only.

Sample table: employees

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.

Sample table: employees

14. Write a query to get the average salary for all departments employing more
than 10 employees.

Sample table: employees


ACCENTURE BATCH 2 LABORATORY

Structure of 'hr' database:

Page | 3
ACCENTURE BATCH 2 LABORATORY

SOLUTION

MySQL Aggregate Function: Exercise-1 with Solution


Page | 4
Write a query to list the number of jobs available in the employees table.

Sample table: employees

Code:
SELECT COUNT(DISTINCT job_id)

FROM employees;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 5
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-2 with Solution

Write a query to get the total salaries payable to employees.


Page | 6
Sample table: employees

Code:
SELECT SUM(salary)

FROM employees;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 7
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-3 with Solution

Write a query to get the minimum salary from employees table.


Page | 8
Sample table: employees

Code:
SELECT MIN(salary)

FROM employees;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 9
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-4 with Solution

Write a query to get the maximum salary of an employee working as a


Page | 10 Programmer.

Sample table: employees

Code:
SELECT MAX(salary)

FROM employees

WHERE job_id = 'IT_PROG';

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Page | 11
ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 12
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-5 with Solution

Write a query to get the average salary and number of employees working the
Page | 13 department 90.

Sample table: employees

Code:
SELECT AVG(salary),count(*)

FROM employees

WHERE department_id = 90;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Page | 14
ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 15
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-6 with Solution

Write a query to get the highest, lowest, sum, and average salary of all
Page | 16 employees.

Sample table: 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

Pictorial Presentation of the above query

Page | 17
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-7 with Solution

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

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 19
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-8 with Solution


Page | 20
Write a query to get the difference between the highest and lowest salaries.

Sample table: employees

Code:
SELECT MAX(salary) - MIN(salary) DIFFERENCE

FROM employees;

Copy
ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 21

<
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-9 with Solution

Write a query to find the manager ID and the salary of the lowest-paid employee
Page | 22 for that manager.

Sample table: employees

Code:
SELECT manager_id, MIN(salary)

FROM employees

WHERE manager_id IS NOT NULL

GROUP BY manager_id

ORDER BY MIN(salary) DESC;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Page | 23
ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 24
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-10 with Solution

Write a query to get the department ID and the total salary payable in each
Page | 25 department.

Sample table: employees

Code:
SELECT department_id, SUM(salary)

FROM employees

GROUP BY department_id;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 26
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-11 with Solution

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

WHERE job_id <> 'IT_PROG'

GROUP BY job_id;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 28
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-12 with Solution

Write a query to get the total salary, maximum, minimum, average salary of
Page | 29 employees (job ID wise), for department ID 90 only.

Sample table: employees

Code:
SELECT job_id, SUM(salary), AVG(salary), MAX(salary), MIN(salary)

FROM employees

WHERE department_id = '90'

GROUP BY job_id;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 30
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-13 with Solution

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.

Sample table: employees

Code:
SELECT job_id, MAX(salary)

FROM employees

GROUP BY job_id

HAVING MAX(salary) >=4000;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 32
ACCENTURE BATCH 2 LABORATORY

MySQL Aggregate Function: Exercise-14 with Solution

Write a query to get the average salary for all departments employing more than
Page | 33 10 employees.

Sample table: employees

Code:
SELECT department_id, AVG(salary), COUNT(*)

FROM employees

GROUP BY department_id

HAVING COUNT(*) > 10;

Copy

Relational Algebra Expression:

Relational Algebra Tree:


ACCENTURE BATCH 2 LABORATORY

Page | 34
ACCENTURE BATCH 2 LABORATORY

Pictorial Presentation of the above query

Page | 35

You might also like