Skip to content
Advertisement

how to split gender into two columns as male and female

I have simple table student which contains two column names and gender as m or f.

I have to count number of male and female in table and display it into tow different columns .

Result should be like:

|Male |female |
| 5   |   4   |

Advertisement

Answer

I don’t know m but standard SQL for this would be :

SELECT 
    SUM(CASE gender WHEN 'm' THEN 1 ELSE 0 END) AS Male,
    SUM(CASE gender WHEN 'f' THEN 1 ELSE 0 END) AS Female
FROM student
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement