【SQL】牛客题霸SQL入门篇1(基础查询、条件查询、高级查询)

链接

基础查询

SQL1查询多列

select device_id,gender,age,university from user_profile;

SQL2 查询所有列

select * from user_profile;

SQL3 查询结果去重

select distinct university from user_profile;

SQL4 查询结果限制返回行数

select device_id from user_profile where id<=2;
select device_id from user_profile where id in(1,2);
select device_id from user_profile limit 0,2;
select device_id from user_profile limit 2;

SQL5 将查询后的列重新命名

as后面跟要新的名字

select device_id as user_infos_example 
from user_profile
where id<=2 ;

条件查询

SQL36 查找后排序

默认为升序 asc可省略

select device_id,age from user_profile order by age asc;

SQL37 查找后多列排序

select device_id,gpa,age from user_profile 
order by gpa asc,age asc;

SQL38 查找后降序排列

select device_id,gpa,age from user_profile 
order by gpa desc,age desc;

SQL6 查找学校是北大的学生信息

like表示模糊搜素

select device_id,university from user_profile where university like '北京大学';

SQL7 查找年龄大于24岁的用户信息

select device_id,gender,age,university from user_profile where age > 24 ;

SQL8 查找某个年龄段的用户信息

select device_id,gender,age from user_profile where age>=20 and age<=23;

SQL9 查找除复旦大学的用户信息

select device_id,gender,age,university from user_profile
where university not like '复旦大学';

SQL10 用where过滤空值练习

select device_id,gender,age,university from user_profile
where age is not NULL;

SQL11 高级操作符练习(1)

男性且GPA在3.5以上(不包括3.5)的用户

select device_id,gender,age,university,gpa
from user_profile
where gender like 'male' and gpa > 3.5 ;

SQL12 高级操作符练习(2)

学校为北大或GPA在3.7以上(不包括3.7)的用户

select device_id,gender,age,university,gpa
from user_profile 
where university like '北京大学' or gpa > 3.7;

SQL13 Where in 和Not in

学校为北大、复旦和山大的同学

select device_id,gender,age,university,gpa
from user_profile 
where university in ('北京大学','复旦大学','山东大学');

SQL14 操作符混合运用

gpa在3.5以上(不包括3.5)的山东大学用户 或 gpa在3.8以上(不包括3.8)的复旦大学

select device_id,gender,age,university,gpa
from user_profile 
where (university='山东大学' and gpa > 3.5) or (university='复旦大学' and gpa > 3.7);

SQL15 查看学校名称中含北京的用户

like表示模糊搜索,%表示任意字符

regexp表示部分匹配

select device_id,age,university
from user_profile 
where university like '%北京%';
select device_id,age,university
from user_profile 
where university regexp '北京';

高级查询

SQL16 查找GPA最高值

复旦大学学生gpa最高值

聚合函数max取最大值或是先降序排序然后取第一个数据

select max(gpa) from user_profile where university = '复旦大学';
select gpa from user_profile 
where university = '复旦大学'
order by gpa desc
limit 1;

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

select 
count(gender) as male_num,
avg(gpa) as avg_gpa
from user_profile
where gender = 'male';
# round(avg(gpa),1)

SQL18 分组计算练习题

对每个学校不同性别的用户活跃情况和发帖数量进行分析,请分别计算出每个学校每种性别的用户数、30天内平均活跃天数和平均发帖数量。

select gender,university,
count(gender) as user_num,
avg(active_days_within_30) as avg_active_day,
avg(question_cnt) as avg_question_cnt
from user_profile
group by university,gender;

SQL19 分组过滤练习题

请取出平均发贴数低于5的学校或平均回帖数小于20的学校

group by的条件判断要用having

select university,
avg(question_cnt) as avg_question_cnt,
avg(answer_cnt) as avg_answer_cnt
from user_profile
group by university
having avg(question_cnt) < 5 or avg(answer_cnt) < 20;

SQL20 分组排序练习题

不同大学的用户平均发帖情况,并期望结果按照平均发帖情况进行升序排列

select university,avg(question_cnt) as 	avg_question_cnt
from user_profile 
group by university
order by avg_question_cnt asc;
  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豆沙睡不醒

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值