SQL17. 计算男生人数以及平均GPA
描述
id | device_id | gender | age | university | gpa |
1 | 2138 | male | 21 | 北京大学 | 3.4 |
2 | 3214 | male | | 复旦大学 | 4.0 |
3 | 6543 | female | 20 | 北京大学 | 3.2 |
4 | 2315 | female | 23 | 浙江大学 | 3.6 |
5 | 5432 | male | 25 | 山东大学 | 3.8 |
6 | 2131 | male | 28 | 北京师范大学 | 3.3 |
male_num | avg_gpa |
4 | 3.6 |
示例1
输入:
drop table if exists user_profile; CREATE TABLE `user_profile` ( `id` int NOT NULL, `device_id` int NOT NULL, `gender` varchar(14) NOT NULL, `age` int , `university` varchar(32) NOT NULL, `gpa` float); INSERT INTO user_profile VALUES(1,2138,'male',21,'北京大学',3.4); INSERT INTO user_profile VALUES(2,3214,'male',null,'复旦大学',4.0); INSERT INTO user_profile VALUES(3,6543,'female',20,'北京大学',3.2); INSERT INTO user_profile VALUES(4,2315,'female',23,'浙江大学',3.6); INSERT INTO user_profile VALUES(5,5432,'male',25,'山东大学',3.8); INSERT INTO user_profile VALUES(6,2131,'male',28,'北京师范大学',3.3);
输出:
4|3.6
Mysql 解法, 执行用时: 36ms, 内存消耗: 6368KB, 提交时间: 2021-12-27
select count(1),avg(gpa) from user_profile group by gender having gender='male'
Mysql 解法, 执行用时: 36ms, 内存消耗: 6380KB, 提交时间: 2022-01-22
select count(gender) mail_num,avg(gpa) avg_gpa from user_profile where gender = 'male'
Mysql 解法, 执行用时: 36ms, 内存消耗: 6408KB, 提交时间: 2022-02-09
select count(gender)as male_num,round(avg(gpa),1)as avg_gpa from user_profile where gender='male'
Mysql 解法, 执行用时: 36ms, 内存消耗: 6408KB, 提交时间: 2021-12-18
select count(gender) male_num,avg(gpa) avg_gpa from user_profile where gender='male'
Mysql 解法, 执行用时: 36ms, 内存消耗: 6408KB, 提交时间: 2021-12-12
SELECT COUNT(gender),AVG(gpa) FROM user_profile WHERE gender='male';