Skip to content
Advertisement

Count Male and Female in SQL and how many records are there. With male listed with their name

I have a table Company, and I want to group the entries by Male and Female.

And I want their names listed in a column.

The result should like as shown here:

+--------+----------+---------------------+
| Count  |  Gender  | Name                |
+--------+----------+---------------------+
|   5    |    M     | Sandy, Loli , Deays.|
|   3    |    F     | Any , Any.......    |
+--------+----------+---------------------+

I got the count of the male and female, but I also need the names in a single column

Advertisement

Answer

Try This but please keep in mind that STRING_AGG is not available on every DBMS system

select Gender, count(*) Count, String_agg(name, ',')
from t
group by Gender
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement