牛客网MySQL基础练习

目录

一,基础知识

1,创建表

2,插入

二、牛客网基础练习


一,基础知识

1,创建表

CREATE TABLE table_name (
 field1 datatype,
 field2 datatype,
 field3 datatype
);
create table stu_test(
    id int,
    name varchar(20) comment '姓名',
    password varchar(20) comment '密码',
    age int,
    sex varchar(1),
    birthday timestamp,
    amout decimal(13,2),
    resume text
);

2,插入

INSERT [INTO] table_name
 [(column [, column] ...)] 
 VALUES (value_list) [, (value_list)] ...
 
value_list: value, [, value] ...
create table student(
    id int,
    sn int comment '学号',
    name varchar(20) comment '姓名',
    qq_mail varchar(20) comment 'QQ邮箱'
);
insert into student values(1,1000,'擦洗',NUll);
insert into student values(2,1001,'好还是', '111111');

二、牛客网基础练习

select * from user_profile;

 

select device_id,gender,age,university from  user_profile;

select distinct university from user_profile;

select device_id from user_profile limit 2;

select device_id as user_infos_example from user_profile where id between 1 and 2;

select device_id,age from user_profile order by age;

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;

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

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

select device_id,gender,age from user_profile where age >19 and age < 25;

# select device_id,gender,age,university from user_profile where not age is null;
select device_id,gender,age,university from user_profile where not university = '复旦大学';

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 gpa > 3.5 and gender = 'male';

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

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

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

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

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

 

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

select university,avg(question_cnt) avg_question_cnt from user_profile group by university
 order by avg_question_cnt;

 

select qpd.device_id,qpd.question_id,qpd.result
from question_practice_detail as qpd 
join user_profile as up on
up.device_id = qpd.device_id  and up.university = '浙江大学'
order by question_id;

select university,
count(qpd.question_id) / count(distinct qpd.device_id) avg_answer_cnt
from  question_practice_detail as qpd
join user_profile as up on
qpd.device_id = up.device_id
group by university;

# select university,
# difficult_level,
# count(qpd.question_id) / count(distinct qpd.device_id) avg_answer_cnt
# from  question_practice_detail as qpd
# left join user_profile as up
# on up.device_id = qpd.device_id
# left join question_detail as qd
# on qd.question_id = qpd.question_id
# group by university,difficult_level;
select 
t1.university,
t3.difficult_level,
count(t2.question_id) / count(distinct t2.device_id) avg_answer_cnt
from
user_profile as t1,
question_practice_detail as t2,
question_detail as t3
where
t2.device_id = t1.device_id
and t2.question_id = t3.question_id
group by
t1.university,t3.difficult_level;

select 
device_id,
gender,
age,
gpa
from 
user_profile
where university = '山东大学'
union all
select 
device_id,
gender,
age,
gpa
from 
user_profile
where gender = 'male';

猜你喜欢

转载自blog.csdn.net/qq_50156012/article/details/124172007
今日推荐