--create table.
create table Emp
(
Dept varchar(2),
Name varchar(50),
Gender Varchar(1)
)
--Insert data into table.
insert into Emp
values('A','Test','M')
insert into Emp
values('A','Test','F')
insert into Emp
values('B','Test','M')
insert into Emp
values('B','Test','M')
insert into Emp
values('A','Test','M')
insert into Emp
values('B','Test','F')
insert into Emp
values('A','Test','M')
insert into Emp
values('C','Test','F')
insert into Emp
values('C','Test','M')
insert into Emp
values('A','Test','M')
insert into Emp
values('A','Test','F')
--query--
select Dept, SUM(Case when gender = 'M' then 1 else 0 End) as Male,SUM(Case when gender = 'F' then 1 else 0 End) as Female from Emp Group by Dept
--Result-----
Dept Male
Female
A 4
2
B 2
1
C 1
1
Happy Coding...