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

💖个人主页:@与自己作战
💯作者简介: 大数据领域优质创作者CSDN@内容合伙人阿里云专家博主
💞牛客刷题系列篇: 【SQL篇】【Python篇】 【Java篇】
📌推荐刷题网站注册地址:【牛客网–SQL篇】
💘推荐理由:从0-1起步,循序渐进
🆘希望大佬们多多支持,携手共进
📝 如果文章对你有帮助的话,欢迎评论💬点赞👍收藏📂加关注
如需要支持请私信我,💯 必支持
👩‍👩‍👦‍👦网址注册地址:【牛客网–注册地址】👩‍👩‍👦‍👦

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

文章目录

; 一、高级查询

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) gpa
from
  user_profile
where
  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
  gpa
from
  user_profile
where
  university = '复旦大学'
order by
  gpa desc
limit
  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_gpa
from
  user_profile
where
  gender = 'male'

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

2、常用函数

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

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

Original: https://blog.csdn.net/walykyy/article/details/127242450
Author: 与自己作战
Title: 【牛客刷题–SQL篇】高级查询之SQL16查找GPA最高值(多种写法)&&SQL17计算男生人数以及平均GPA

原创文章受到原创版权保护。转载请注明出处:https://www.johngo689.com/813734/

转载文章受原作者版权保护。转载请注明原作者出处!

(0)

大家都在看

亲爱的 Coder【最近整理,可免费获取】👉 最新必读书单  | 👏 面试题下载  | 🌎 免费的AI知识星球