> 文档中心 > 【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA

【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA


个人主页:@与自己作战 大数据领域创作者
牛客刷题系列篇:【SQL篇】【Python篇】【Java篇】
推荐刷题网站注册地址:【牛客网–SQL篇】
推荐理由:从0-1起步,循序渐进
网址注册地址:【牛客网–注册地址】
【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA

文章目录

  • 一、高级查询
    • 1、计算函数
    • 1.1、SQL16 查找GPA最高值
      • 1.1.1、SQL语句(第一种写法推荐)
      • 1.1.2、SQL语句(第二种写法)
    • 1.2、SQL17 计算男生人数以及平均GPA
    • 2、常用函数

一、高级查询

1、计算函数

1.1、SQL16 查找GPA最高值

  • 描述

题目:运营想要知道复旦大学学生gpa最高值是多少,请你取出相应数据

【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA

  • 示例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,2234,‘male’,21,‘北京大学’,3.2);
INSERT INTO user_profile VALUES(2,2235,‘male’,null,‘复旦大学’,3.8);
INSERT INTO user_profile VALUES(3,2236,‘female’,20,‘复旦大学’,3.5);
INSERT INTO user_profile VALUES(4,2237,‘female’,23,‘浙江大学’,3.3);
INSERT INTO user_profile VALUES(5,2238,‘male’,25,‘复旦大学’,3.1);
INSERT INTO user_profile VALUES(6,2239,‘male’,25,‘北京大学’,3.6);
INSERT INTO user_profile VALUES(7,2240,‘male’,null,‘清华大学’,3.3);
INSERT INTO user_profile VALUES(8,2241,‘female’,null,‘北京大学’,3.7);

输出
3.8

输入: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,2234,'male',21,'北京大学',3.2);INSERT INTO user_profile VALUES(2,2235,'male',null,'复旦大学',3.8);INSERT INTO user_profile VALUES(3,2236,'female',20,'复旦大学',3.5);INSERT INTO user_profile VALUES(4,2237,'female',23,'浙江大学',3.3);INSERT INTO user_profile VALUES(5,2238,'male',25,'复旦大学',3.1);INSERT INTO user_profile VALUES(6,2239,'male',25,'北京大学',3.6);INSERT INTO user_profile VALUES(7,2240,'male',null,'清华大学',3.3);INSERT INTO user_profile VALUES(8,2241,'female',null,'北京大学',3.7);输出:3.8

【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA

1.1.1、SQL语句(第一种写法推荐)

select
max(gpa) gpa
from
user_profile
where
university=‘复旦大学’

select  max(gpa) gpafrom  user_profilewhere  university='复旦大学'

【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA
【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA

1.1.2、SQL语句(第二种写法)

select
gpa
from
user_profile
where
university = ‘复旦大学’
order by
gpa desc
limit
1

select  gpafrom  user_profilewhere  university = '复旦大学'order by  gpa desclimit  1

【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA
【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA

1.2、SQL17 计算男生人数以及平均GPA

  • 描述

题目:现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动,请你取出相应数据。

【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA

  • 示例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

输入: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
  • SQL语句

select
count(*) male_num,
round(avg(gpa),1) avg_gpa
from
user_profile
where
gender = ‘male’

select  count(*) male_num,  round(avg(gpa),1) avg_gpafrom  user_profilewhere  gender = 'male'

【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA
【牛客刷题--SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA

2、常用函数

最大值max
最小值min
平均值avg
四舍五入(round(xx,yy))xx是字段,yy是保留几位小数

推荐刷题网站:【牛客网–SQL篇】
网址注册地址:【牛客网–注册地址】