select * from user_profile;
select device_id,gender,age,university from user_profile;
select distinct university from user_profile; #distinct 关键字
select device_id from user_profile where id<=2;
或者
select device_id from user_profile limit 2; # 关键字limit用于限制查询结果返回的数据的数量
select device_id from user_profile limit 0,2;
select device_id as user_infors_example from user_profile limit 2; # as可以被用作重命名列名或者表名
select device_id,age from user_profile order by age; # order by 关键字
select device_id,gpa,age from user_profile order by gpa,age;
select device_id,gpa,age from user_profile order by gpa desc,age desc; # desc关键件字
select device_id,university from user_profile where university='北京大学';
select device_id,gender,age,university from user_profile where age>24;
select device_id,age,university from user_profile where university like '%北京%';
select device_id,gender,age from user_profile where age>=20 and age<=23;
select device_id,gender,age,university,gpa from user_profile where gender = "male" and gpa>3.5;
# 单等号
select device_id,gender,age,university from user_profile where university != '复旦大学';
select device_id,gender,age,university from user_profile where age !='';
或者
select device_id,gender,age,university from user_profile where age is not null;
select device_id,gender,age,university,gpa from user_profile where university='北京大学' or gpa>3.7;
select max(gpa) from user_profile where university='复旦大学';
select device_id,gender,age,university,gpa from user_profile where (gpa>3.5 and
university='山东大学') or (gpa>3.8 and university='复旦大学');
# and的优先级大于or
select count(gender) as male_num,avg(gpa) as avg_gpa from user_profile where gender = 'male';
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; # group by
select university, avg(question_cnt) as avg_question_cnt
from user_profile group by university order by avg_question_cnt;
select user_profile.device_id,question_id,result
from question_practice_detail,user_profile
where question_practice_detail.device_id = user_profile.device_id
and university='浙江大学';
每个学校答过题的用户平均答题数量情况,请你取出数据
select
university,
count(question_id)/count(distinct user_profile.device_id) as avg_answer_cnt
from
user_profile,
question_practice_detail
where
user_profile.device_id = question_practice_detail.device_id
group by university;
select
case
when age >= 25 then '25岁及以上'
when age < 25 or age is null then '25岁以下'
end as age_cut,count(*) number
from
user_profile
group by age_cut