﻿ SQL Challenges-1: Department wise number of male and female employees - w3resource

# SQL Challenges-1: Department wise number of male and female employees and their total salaries

## SQL Challenges-1: Exercise-56 with Solution

From the following table write a SQL query to find the number of male and female employees in each department and along with their total salaries. Return department ID, number of female employees, their total salaries, number of male employees and their total salaries.

Input:

Table: employees

Structure:

FieldTypeNullKeyDefaultExtra
emp_idintNOPRI
emp_namevarchar(30)YES
emp_sexvarchar(1)YES
emp_salaryintYES
emp_departmentintYES

Data:

emp_idemp_nameemp_sexemp_salary emp_department
100StevenM2400090
101NeenaF1700090
102LexM1700080
103AlexanderM900060
104BruceM600060
105DavidM480080
106ValliF480060
107DianaF420060
108NancyM12000100
109DanielF9000100
110JohnM8200100
111IsmaelM7700100
112Jose ManuelM7800100
113LuisF6900100
114DenM1100030
115AlexanderM310030
116ShelliF290030
117SigalF280030
133JasonM330050
134MichaelF290050
135KiF240050

Sample Solution:

SQL Code(MySQL):

``````create table employees (
emp_id integer(4) not null unique,
emp_name varchar(30),
emp_sex varchar(1),
emp_salary int(6),
emp_department int(3));

insert into employees values(100,'Steven     ','M',24000,  90);
insert into employees values(101,'Neena      ','F',17000,  90);
insert into employees values(102,'Lex        ','M',17000,  80);
insert into employees values(103,'Alexander  ','M', 9000,  60);
insert into employees values(104,'Bruce      ','M', 6000,  60);
insert into employees values(105,'David      ','M', 4800,  80);
insert into employees values(106,'Valli      ','F', 4800,  60);
insert into employees values(107,'Diana      ','F', 4200,  60);
insert into employees values(114,'Den        ','M',11000,  30);
insert into employees values(115,'Alexander  ','M', 3100,  30);
insert into employees values(116,'Shelli     ','F', 2900,  30);
insert into employees values(117,'Sigal      ','F', 2800,  30);
insert into employees values(108,'Nancy      ','M',12000, 100);
insert into employees values(109,'Daniel     ','F', 9000, 100);
insert into employees values(110,'John       ','M', 8200, 100);
insert into employees values(111,'Ismael     ','M', 7700, 100);
insert into employees values(112,'Jose Manuel','M', 7800, 100);
insert into employees values(113,'Luis       ','F', 6900, 100);
insert into employees values(133,'Jason      ','M', 3300,  50);
insert into employees values(134,'Michael    ','F', 2900,  50);
insert into employees values(135,'Ki         ','F', 2400,  50);

SELECT  emp_department AS department,
SUM(CASE WHEN emp_sex='F' THEN 1 END) AS female_employees,
SUM(CASE WHEN emp_sex='F' THEN emp_salary END) AS female_total_salary,
SUM(CASE WHEN emp_sex='M' THEN 1 END) AS male_employees,
SUM(CASE WHEN emp_sex='M' THEN emp_salary END) AS male_total_salary
FROM employees
group BY 1;
```
```

Sample Output:

```department|female_employees|female_total_salary|male_employees|male_total_salary|
----------+----------------+-------------------+--------------+-----------------+
90|               1|              17000|             1|            24000|
80|                |                   |             2|            21800|
60|               2|               9000|             2|            15000|
100|               2|              15900|             4|            35700|
30|               2|               5700|             2|            14100|
50|               2|               5300|             1|             3300|
```

SQL Code Editor: