今天碰到一个面试题,记录一下:
现有数据表如下,使用SQL,统计出10~20岁,20~30岁
两个分组中男:女人数?请写出对应SQL语句
思路
考察的是sql-case和group by
语句的应用
select
case
when age BETWEEN 10 and 20 then "range(10~20)"
when age BETWEEN 21 and 30 then "range(20~30)"
end as age_range,gender,count(*) as num
from student
GROUP BY
case
when age BETWEEN 10 and 20 then "range(10~20)"
when age BETWEEN 21 and 30 then "range(20~30)"
end,gender;