牛客SQL编程练习(持续更新)

浅记录一下SQL学习过程

1.现在运营想要查看用户信息表中所有的数据,请你取出相应结果
重点:查询表中所有数据

select * from user_profile;

2.现在运营同学想要用户的设备id对应的性别、年龄和学校的数据,请你取出相应数据
重点:取出表中某几列数据

select device_id,gender, age, university
from user_profile;

3.现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。
重点:找出某一列中的唯一值,用distinct关键字(延伸:求唯一值有多少个/重复值有多少个)

select **distinct** university
from user_profile;

4.现在运营只需要查看前2个用户明细设备ID数据,请你从用户信息表 user_profile 中取出相应结果。
重点:查看前X个用户信息,查看m-n条用户信息,用limit关键字,可以结合offset一起使用

select device_id
from user_profile
limit 0,2;

5.现在你需要查看前2个用户明细设备ID数据,并将列名改为 ‘user_infos_example’,,请你从用户信息表取出相应结果。
重点:将提取出的某列信息重命名,用as关键字

select device_id as user_infos_example
from user_profile
limit 0,2;

6.现在运营想要筛选出所有北京大学的学生进行用户调研,请你从用户信息表中取出满足条件的数据,结果返回设备id和学校。
重点:where条件筛选

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

7.现在运营想要针对24岁以上的用户开展分析,请你取出满足条件的设备ID、性别、年龄、学校。

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

8.现在运营想要针对20岁及以上且23岁及以下的用户开展分析,请你取出满足条件的设备ID、性别、年龄。
重点:between…and包括边界值

select device_id,gender,age
from user_profile
where age between 20 and 23;

9.现在运营想要查看除复旦大学以外的所有用户明细,请你取出相应数据

select device_id,gender,age,university
from user_profile
where university != '复旦大学';

// where语句中不等于关系的另外两种写法
where university <> '复旦大学'
where university not in ('复旦大学')

// 其中!=和<>只能排除一个值,而not in可以排除多个值: not in('v1','v2','v3',...)

10.现在运营想要对用户的年龄分布开展分析,在分析时想要剔除没有获取到年龄的用户,请你取出所有年龄值不为空的用户的设备ID,性别,年龄,学校的信息。

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

// 也可以写age不等于空--where age != null;

11.现在运营想要找到男性且GPA在3.5以上(不包括3.5)的用户进行调研,请你取出相关数据。
重点:多条件筛选用and操作符

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

12.现在运营想要找到学校为北大或GPA在3.7以上(不包括3.7)的用户进行调研,请你取出相关数据
重点:or操作符

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

13.现在运营想要找到学校为北大、复旦和山大的同学进行调研,请你取出相关数据。
重点:where…in…操作符

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

14.现在运营想要找到gpa在3.5以上(不包括3.5)的山东大学用户gpa在3.8以上(不包括3.8)的复旦大学同学进行用户调研,请你取出相应数据。
重点:操作符混合使用

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

15.现在运营想查看所有大学中带有北京的用户的信息,请你取出相应数据。
重点:字符串匹配

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

// 匹配串中可包含如下四种通配符:
// _:匹配任意一个字符;
// %:匹配0个或多个字符;
// [ ]:匹配[ ]中的任意一个字符(若要比较的字符是连续的,则可以用连字符“-”表 达 );
// [^ ]:不匹配[ ]中的任意一个字符。

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

// 两种思路
// 思路1:使用max()获取最大值
select max(gpa)
from user_profile
where university='复旦大学';

// 思路2:对表按照gpa排序,取第一行数据
select gpa
from user_profile
where university='复旦大学'
order by gpa desc
limit 0,1;

17.现在运营想要看一下男性用户有多少人以及他们的平均gpa是多少,用以辅助设计相关活动,请你取出相应数据。
重点:SQL语句的运行顺序
from>where>group>having>order>select
题拆解:
男性用户—>where筛选
有多少人—>count()函数
平均gpa—>avg()函数
先用where筛选出所有男性用户,再计算总人数、平均gpa。

select count(gender) as male_num,round(avg(gpa),1) as avg_gpa
from user_profile
where gender='male';
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值