数据库复习训练

这篇文章基本涵盖了数据库中的基本查询语法:
where,inner join,right join,left join,order by [asc,desc],group by,limit,between and,max,min,avg,count(*)…

1.数据库,表的结构创建

#学生表
create table `student`(
	`studentNo` int auto_increment primary key,
	`name` varchar(100),
	`sex`	char(10),
	`hometown` varchar(50),
	`age` int,
	`class` varchar(50),
	`card` varchar(100)
);

#课程表
create table `course`(
	`courseNo` int auto_increment primary key,
	`courseName` varchar(50)
);

#成绩表
create table `score`(
	`id` int auto_increment primary key,
	`courseNo` int,
	`studentNo` int,
	`score` int
);

2.添加数据

#添加学生表数据
insert into `student` values(null,'王昭君','女','北京',22,'1班','340233199909287653');
insert into `student` values(null,'诸葛亮','男','广州',24,'2班','340233199703242413');
insert into `student` values(null,'赵云','男','深圳',23,'3班','340233198211225324');
insert into `student` values(null,'曜','男','杭州',25,'4班','340233198309074487');
insert into `student` values(null,'大乔','女','乌鲁木齐',20,'1班','340233199209217553');
insert into `student` values(null,'孙尚香','女','北京',24,'2班','340233199905279966');
insert into `student` values(null,'百里守约','男','深圳',26,'3班','');
insert into `student` values(null,'妲己','女','长沙',25,'4班','');
insert into `student` values(null,'李白','男','天津',27,'1班','');
insert into `student` values(null,'百里玄策','男','北京',24,'2班','340233197809291199');
insert into `student` values(null,'公孙离','女','贵阳',23,'3班','340233197606262103');
insert into `student` values(null,'李信','男','长沙',27,'4班','340233199201313846');
insert into `student` values(null,'西施','女','杭州',23,'1班','340233199505258957');
insert into `student` values(null,'貂蝉','女','杭州',23,'2班','340233199408280121');
insert into `student` values(null,'武则天','女','北京',22,'3班','340233198903180223');
insert into `student` values(null,'韩信','男','北京',26,'4班','340233198301304382');
insert into `student` values(null,'马超','男','上海',27,'1班',null);

#添加课程表数据
insert into `course` values(null,'数据库');
insert into `course` values(null,'Linux');
insert into `course` values(null,'单元测试');
insert into `course` values(null,'JAVA的基本语法');
insert into `course` values(null,'系统安装');
insert into `course` values(null,'软件技术应用');
insert into `course` values(null,'小吃小喝睡百年');

#添加成绩表数据
insert into `score` values(null,1,1,90);
insert into `score` values(null,1,2,45);
insert into `score` values(null,1,3,98);
insert into `score` values(null,1,4,34);
insert into `score` values(null,1,5,66);
insert into `score` values(null,1,6,12);
insert into `score` values(null,1,7,88);
insert into `score` values(null,1,8,89);
insert into `score` values(null,1,9,100);
insert into `score` values(null,1,10,92);
insert into `score` values(null,1,11,46);
insert into `score` values(null,1,12,59);
insert into `score` values(null,1,13,83);
insert into `score` values(null,1,14,60);
insert into `score` values(null,1,15,96);
insert into `score` values(null,1,16,33);

insert into `score` values(null,2,1,23);
insert into `score` values(null,2,2,77);
insert into `score` values(null,2,3,87);
insert into `score` values(null,2,4,58);
insert into `score` values(null,2,5,95);
insert into `score` values(null,2,6,46);
insert into `score` values(null,2,7,74);
insert into `score` values(null,2,8,55);
insert into `score` values(null,2,9,100);
insert into `score` values(null,2,10,93);
insert into `score` values(null,2,11,90);
insert into `score` values(null,2,12,46);
insert into `score` values(null,2,13,78);
insert into `score` values(null,2,14,94);
insert into `score` values(null,2,15,63);
insert into `score` values(null,2,16,57);

insert into `score` values(null,3,1,80);
insert into `score` values(null,3,2,87);
insert into `score` values(null,3,3,93);
insert into `score` values(null,3,4,86);
insert into `score` values(null,3,5,81);
insert into `score` values(null,3,6,100);
insert into `score` values(null,3,7,64);
insert into `score` values(null,3,8,74);
insert into `score` values(null,3,9,100);
insert into `score` values(null,3,10,28);
insert into `score` values(null,3,11,37);
insert into `score` values(null,3,12,84);
insert into `score` values(null,3,13,83);
insert into `score` values(null,3,14,92);
insert into `score` values(null,3,15,85);
insert into `score` values(null,3,16,73);

insert into `score` values(null,4,1,75);
insert into `score` values(null,4,2,45);
insert into `score` values(null,4,3,74);
insert into `score` values(null,4,4,46);
insert into `score` values(null,4,5,83);
insert into `score` values(null,4,6,75);
insert into `score` values(null,4,7,83);
insert into `score` values(null,4,8,37);
insert into `score` values(null,4,9,100);
insert into `score` values(null,4,10,97);
insert into `score` values(null,4,11,48);
insert into `score` values(null,4,12,69);
insert into `score` values(null,4,13,74);
insert into `score` values(null,4,14,96);
insert into `score` values(null,4,15,93);
insert into `score` values(null,4,16,87);

insert into `score` values(null,5,1,14);
insert into `score` values(null,5,2,13);
insert into `score` values(null,5,3,09);
insert into `score` values(null,5,4,23);
insert into `score` values(null,5,5,17);
insert into `score` values(null,5,6,36);
insert into `score` values(null,5,7,60);
insert into `score` values(null,5,8,55);
insert into `score` values(null,5,9,100);
insert into `score` values(null,5,10,60);
insert into `score` values(null,5,11,47);
insert into `score` values(null,5,12,27);
insert into `score` values(null,5,13,18);
insert into `score` values(null,5,14,0);
insert into `score` values(null,5,15,09);
insert into `score` values(null,5,16,82);

insert into `score` values(null,6,1,60);
insert into `score` values(null,6,2,47);
insert into `score` values(null,6,3,68);
insert into `score` values(null,6,4,63);
insert into `score` values(null,6,5,82);
insert into `score` values(null,6,6,94);
insert into `score` values(null,6,7,83);
insert into `score` values(null,6,8,71);
insert into `score` values(null,6,9,100);
insert into `score` values(null,6,10,95);
insert into `score` values(null,6,11,94);
insert into `score` values(null,6,12,95);
insert into `score` values(null,6,13,76);
insert into `score` values(null,6,14,72);
insert into `score` values(null,6,15,87);
insert into `score` values(null,6,16,98);

insert into `score` values(null,7,1,100);
insert into `score` values(null,7,2,100);
insert into `score` values(null,7,3,100);
insert into `score` values(null,7,4,100);
insert into `score` values(null,7,5,100);
insert into `score` values(null,7,6,100);
insert into `score` values(null,7,7,100);
insert into `score` values(null,7,8,100);
insert into `score` values(null,7,9,100);
insert into `score` values(null,7,10,100);
insert into `score` values(null,7,11,100);
insert into `score` values(null,7,12,100);
insert into `score` values(null,7,13,100);
insert into `score` values(null,7,14,100);
insert into `score` values(null,7,15,100);
insert into `score` values(null,7,16,100);

3.SQL训练题

#1.查询‘大乔’的年龄
#2.查询25岁以下(包含25)的学生
#3.查询家乡不在北京的学生
#4.查询‘1班’以外的学生
#5.查询非深圳的学生
#6.查询北京或深圳的学生
#7.查询1班的北京学生
#8.查询非23岁的学生
#9.查询姓‘孙’且名字是一个字的学生
#10.查询叫‘乔’的学生,以‘乔’结尾
#11.查询姓名含‘白’的学生
#12.查询姓名为两个字的学生
#13.查询姓‘百’且年龄大于20的学生
#14.查询学号以6结尾的学生
#15.查询家乡在北京或深圳或长沙的学生
#16.查询年龄为20-25的学生
#17.查询年龄在222325的女生
#18.查询年龄在20-25以外的学生
#19.查询没有填写身份证信息的学生
#20.查询填写了身份证信息的学生
#21.查询所有学生信息,按年龄从小到大排序,再按学号从小到大排序
#22.对中文的姓名数据进行排序【字母顺序】
#23.查询学生总数
#24.查询女生的最大年龄
#25.查询1班的最小年龄
#26.查询北京学生的年龄总和
#27.查询男生的平均年龄
#28.查询所有学生的最大年龄、最小年龄、平均年龄
#29.查询3班年龄小于23岁的学生
#30.查询各种性别的人数
#31.查询各种年龄的人数
#32.查询各个班级的最大年龄、最小年龄和平均年龄
#33.查询1班除外的其他班级学生的平均年龄、最大年龄和最小年龄
#34.查询第4行到第6行的学生信息
#35.查询学生信息及学生的成绩
#36.查询年龄最小的学生的全部信息
#37.查询‘王昭君’的‘数据库’成绩,要求显示姓名,课程名和成绩
#38.查询所有学生的成绩,包括没有成绩的学生
#39.查询大于平均年龄的学生
#40.查询最小年龄的人
#41.查询‘王昭君’的成绩,要求显示成绩
#42.查询‘王昭君’的‘数据库’成绩,要求显示成绩
#43.查询23岁的学生成绩,要求显示成绩
#44.查询‘数据库’和‘小吃小喝睡百年’的课程成绩

4.SQL训练题答案【答案不具有唯一性,欢迎在评论区补充】

// An highlighted block
#1.查询‘大乔’的年龄
-- select `name`,`age` from student where name='大乔';
#2.查询25岁以下(包含25)的学生
-- select * from student where age <= 25;
#3.查询家乡不在北京的学生
-- select * from student where hometown!='北京';
-- select * from student where hometown<>'北京';
-- select * from student where not hometown='北京';
#4.查询‘1班’以外的学生
-- select * from student where class!='1班';
#5.查询非深圳的学生
-- select * from student where hometown!='深圳';
-- select * from student where hometown<>'深圳';
-- select * from student where not hometown='深圳';
#6.查询北京或深圳的学生
-- select * from student where hometown='北京' or hometown='深圳';
#7.查询1班的北京学生
-- select * from student where class='1班' and hometown='北京';
#8.查询非23岁的学生
-- select * from student where age!='23';
#9.查询姓‘孙’且名字是一个字的学生
-- select * from student where name like '孙_';
#10.查询叫‘乔’的学生,以‘乔’结尾
-- select * from student where name like '%乔';
#11.查询姓名含‘白’的学生
-- select * from student where name like '%白%';
#12.查询姓名为两个字的学生
-- select * from student where name like '__';
#13.查询姓‘百’且年龄大于20的学生
-- select * from student where name like '百%' and age > 20;
#14.查询学号以6结尾的学生
-- select * from student where studentNo like '%6';
#15.查询家乡在北京或深圳或长沙的学生
-- select * from student where hometown='北京' or hometown='深圳' or hometown='长沙';
-- select * from student where hometown in('北京','深圳','长沙');
#16.查询年龄为20-25的学生
-- select * from student where age>=20 and age<=25;
-- select * from student where age between 20 and 25;
#17.查询年龄在222325的女生
-- select * from student where age in(22,23,25) and sex='女';
#18.查询年龄在20-25以外的学生
-- select * from student where age not between 20 and 25;
#19.查询没有填写身份证信息的学生
-- select * from student where card='';
-- select * from student where card is null;
#20.查询填写了身份证信息的学生
-- select * from student where card is not null and not card='';
#21.查询所有学生信息,按年龄从小到大排序,再按学号从小到大排序
-- select * from student order by age,studentNo;
#22.对中文的姓名数据进行排序【字母顺序】
-- select * from student order by convert(name using gbk);
#23.查询学生总数
-- select count(*) from student;
#24.查询女生的最大年龄
-- select MAX(age) as age from student where sex='女';
#25.查询1班的最小年龄
-- select min(age) as age from student where class='1班';
#26.查询北京学生的年龄总和
-- select sum(age) from student where hometown='北京';
#27.查询男生的平均年龄
-- select avg(age) from student where sex='男';
#28.查询所有学生的最大年龄、最小年龄、平均年龄
-- select max(age) as 最大年龄,min(age) as 最小年龄,avg(age) as 平均年龄 from student;
#29.查询3班年龄小于23岁的学生
-- select * from student where class='3班' and age<23; 
#30.查询各种性别的人数
-- select sex,count(*) from student group by sex;
#31.查询各种年龄的人数
-- select age,count(*) from student group by age;
#32.查询各个班级的最大年龄、最小年龄和平均年龄
-- select class as 班级,max(age) as 最大年龄,min(age) as 最小年龄,avg(age) as 平均年龄 from student group by class;
#33.查询1班除外的其他班级学生的平均年龄、最大年龄和最小年龄
-- select class as 班级,max(age) as 最大年龄,min(age) as 最小年龄,avg(age) as 平均年龄 from student where not class='1班' group by class;
#34.查询第4行到第6行的学生信息
-- select * from student limit 3,3;
#35.查询学生信息及学生的成绩
-- select * from student as stu inner join score as sco on stu.studentNo=sco.studentNo;
-- select * from student stu,score sco where stu.studentNo=sco.studentNo;
#36.查询年龄最小的学生的全部信息
-- select * from student order by age limit 1;
#37.查询‘王昭君’的‘数据库’成绩,要求显示姓名,课程名和成绩
-- select stu.`name`,cou.courseName,sco.score from student as stu
-- inner join score as sco
-- on stu.studentNo=sco.studentNo
-- inner join course as cou
-- on cou.courseNo=sco.courseNo
-- where stu.`name`='王昭君'
-- and cou.courseName='数据库';

-- select stu.`name`,cou.courseName,sco.score from student as stu,score as sco,course as cou
-- where stu.studentNo=sco.studentNo and cou.courseNo=sco.courseNo and stu.`name`='王昭君'
-- and cou.courseName='数据库';
#38.查询所有学生的成绩,包括没有成绩的学生
-- select stu.`name`,cou.courseName,sco.score from score as sco 
-- right join course as cou on sco.courseNo=cou.courseNo
-- inner join student as stu on stu.studentNo=sco.studentNo;
#39.查询大于平均年龄的学生
-- select * from student where age>(select avg(age) from student);
#40.查询最小年龄的人
-- select * from student order by age limit 1;
-- select * from student where age=(select min(age) from student);
#41.查询‘王昭君’的成绩,要求显示成绩
-- select score from score where studentNo=(select studentNo from student where `name`='王昭君');
#42.查询‘王昭君’的‘数据库’成绩,要求显示成绩
-- select score from score where 
-- studentNo=(select studentNo from student where `name`='王昭君') and 
-- courseNo=(select courseNo from course where courseName='数据库');
#43.查询23岁的学生成绩,要求显示成绩
-- select score from score where studentNo in (select studentNo from student where age=23);
#44.查询‘数据库’和‘小吃小喝睡百年’的课程成绩
-- select stu.`name`,cou.courseName,sco.score from student as stu 
-- inner join score as sco on stu.studentNo=sco.studentNo
-- inner join course as cou on sco.courseNo=cou.courseNo
-- where cou.courseName='数据库' or cou.courseName='小吃小喝睡百年';

猜你喜欢

转载自blog.csdn.net/qq_43096786/article/details/112795037
今日推荐