基础查询
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;
SQL13Where 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;