1 查询结果去重
题目:现在运营需要查看用户来自于哪些学校,请从用户信息表中取出学校的去重数据。
原题链接:sql题库:sql3 查询结果去重
题解:
select distinct university from user_profile
2. 将查询后的列重新命名
题解:
select device_id as user_infos_example from user_profile limit 2
3 查找某个年龄段的用户信息
题解:
select device_id,gender,age from user_profile where age between 20 and 23
4 查找除复旦大学的用户信息
题解:
select device_id,gender,age,university from user_profile where university != "复旦大学"
5 用where过滤空值练习
select device_id,gender,age,university from user_profile where age is not null
6 Where in 和Not in
select device_id,gender,age,university,gpa from user_profile where university in ("北京大学","复旦大学","山东大学")
原题链接:sql题库:SQL13 Where in 和Not in
7 操作符混合运用
题解1(写法简单,查询效率低)
select device_id,gender,age,university,gpa from user_profile where gpa > 3.5 and university = "山东大学" or gpa > 3.8 and university = "复旦大学"
题解2(写法复杂,查询效率高)
select device_id, gender, age, university, gpa from user_profile where device_id in (select device_id from user_profile where gpa>3.5 and university='山东大学') or device_id in (select device_id from user_profile where gpa>3.8 and university='复旦大学')
原题链接:sql题库:SQL14 操作符混合运用
8 查看学校名称中含北京的用户
📕 知识点
匹配串中可包含如下四种通配符:
_:匹配任意一个字符;
%:匹配0个或多个字符;
[ ]:匹配[ ]中的任意一个字符(若要比较的字符是连续的,则可以用连字符“-”表 达 );
[^ ]:不匹配[ ]中的任意一个字符。
题解:
select device_id, age, university from user_profile where university like "%北京%"
原题链接:sql题库:SQL15 查看学校名称中含北京的用户
9 计算男生人数以及平均GPA
select count(gender) as male_num, avg(gpa) as avg_gpa from user_profile where gender="male"
原题链接:sql题库:SQL17 计算男生人数以及平均GPA
10 分组计算练习题
题解:
select gender, university, count(device_id) as user_num, avg(active_days_within_30) as avg_active_days, avg(question_cnt) as avg_question_cnt from user_profile group by gender, university
原题链接:sql题库:SQL18 分组计算练习题