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:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
emp_id | int | NO | PRI | ||
emp_name | varchar(30) | YES | |||
emp_sex | varchar(1) | YES | |||
emp_salary | int | YES | |||
emp_department | int | YES |
Data:
emp_id | emp_name | emp_sex | emp_salary | emp_department |
---|---|---|---|---|
100 | Steven | M | 24000 | 90 |
101 | Neena | F | 17000 | 90 |
102 | Lex | M | 17000 | 80 |
103 | Alexander | M | 9000 | 60 |
104 | Bruce | M | 6000 | 60 |
105 | David | M | 4800 | 80 |
106 | Valli | F | 4800 | 60 |
107 | Diana | F | 4200 | 60 |
108 | Nancy | M | 12000 | 100 |
109 | Daniel | F | 9000 | 100 |
110 | John | M | 8200 | 100 |
111 | Ismael | M | 7700 | 100 |
112 | Jose Manuel | M | 7800 | 100 |
113 | Luis | F | 6900 | 100 |
114 | Den | M | 11000 | 30 |
115 | Alexander | M | 3100 | 30 |
116 | Shelli | F | 2900 | 30 |
117 | Sigal | F | 2800 | 30 |
133 | Jason | M | 3300 | 50 |
134 | Michael | F | 2900 | 50 |
135 | Ki | F | 2400 | 50 |
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:
Contribute your code and comments through Disqus.
Previous: Find the minimum order amount sold by each salesperson.
Next: Find the employees in department Administration who solved the cases for all quarters are more than 1200.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://www.w3resource.com/sql-exercises/challenges-1/sql-challenges-1-exercise-56.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics