How can I highlight two columns to count by group?
When I do this
SELECT id, gender, count(1)
FROM class_student
WHERE id = 1
GROUP BY gender
I get
id gender count
--------------------
1 female 10
1 male 5
How can I get this?
id female male
---------------
1 10 5
Is the following query the correct way to do this?
SELECT
id,
(select count(1) from class_student
where id = 1 AND gender='female') AS female,
(select count(1) from class_student
where id = 1 AND gender='male') AS male
FROM class_student
GROUP BY id
+3
source to share
2 answers