MySQL综合练习(50道)

这篇具有很好参考价值的文章主要介绍了MySQL综合练习(50道)。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

目录

一、准备工作(建表、插入数据):

二、SQL练习(50道)

1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

6、查询"李"姓老师的数量

7、询学过"张三"老师授课的同学的信息

8、查询没学过"张三"老师授课的同学的信息

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

11、查询没有学全所有课程的同学的信息

12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

14、查询没学过"张三"老师讲授的任一门课程的学生姓名

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

16、检索"01"课程分数小于60,按分数降序排列的学生信息

17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

18、查询各科成绩最高分、最低分、平均分、及格率(60分以上)、中等率(70-80)、优良率(80-90)、优秀率(90以上)

19、按各科成绩进行排序,并显示排名

20、查询学生的总成绩并进行排名

21、查询不同老师所教不同课程平均分从高到低显示

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比

24、查询学生平均成绩及其名次

25、查询各科成绩前三名的记录

26、查询每门课程被选修的学生数

27、查询出只有两门课程的全部学生的学号和姓名

28、查询男生、女生人数

29、查询名字中含有"风"字的学生信息

30、查询同名同性学生名单,并统计同名人数

31、查询1990年出生的学生名单

32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

34、查询课程名称为"数学",且分数低于60的学生姓名和分数

35、查询所有学生的课程及分数情况

36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数

37、查询课程不及格的学生

38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名

39、求每门课程的学生人数

40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩

42、查询每门课程成绩最好的前三名

43、统计每门课程的学生选修人数(超过5人的课程才统计)

44、检索至少选修两门课程的学生学号

45、查询选修了全部课程的学生信息

46、查询各学生的年龄(周岁)

47、查询本周过生日的学生

48、查询下周过生日的学生

49、查询本月过生日的学生

50、查询12月份过生日的学生


一、准备工作(建表、插入数据):

drop table if exists student;
create table student(
s_id int,
s_name varchar(8),
s_birth date,
s_sex char(4)
);

insert into student values
(1,'赵雷','1990-01-01','男'),
(2,'钱电','1990-12-21','男'),
(3,'孙风','1990-05-20','男'),
(4,'李云','1990-08-06','男'),
(5,'周梅','1991-12-01','女'),
(6,'吴兰','1992-03-01','女'),
(7,'郑竹','1989-07-01','女'),
(8,'王菊','1990-01-20','女');


drop table if exists score;
create table score(
s_id int,
c_id int,
s_score int
);

insert into score values
(1,1,80),
(1,2,90),
(1,3,99),
(2,1,70),
(2,2,60),
(2,3,65),
(3,1,80),
(3,2,80),
(3,3,80),
(4,1,50),
(4,2,30),
(4,3,40),
(5,1,76),
(5,2,87),
(6,1,31),
(6,3,34),
(7,2,89),
(7,3,98);


drop table if exists course;
create table course(
c_id int,
c_name varchar(8),
t_id int
);

insert into course values
(1,'语文',2),
(2,'数学',1),
(3,'英语',3);

drop table if exists teacher;
create table teacher(
t_id int,
t_name varchar(8)
);

insert into teacher values
(1,'张三'),
(2,'李四'),
(3,'王五');

二、SQL练习(50道)

1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

-- 方法1
select s.*, sc1.s_score as score_01, sc2.s_score as score_02 
from student s, 
     (select s_id, s_score from score where c_id = 1) sc1,
		 (select s_id, s_score from score where c_id = 2) sc2
 where sc1.s_score > sc2.s_score
 and sc1.s_id = sc2.s_id
 and sc1.s_id = s.s_id
 
-- 方法2
select s.*, sc1.s_score score_01, sc2.s_score score_02
from score sc1, score sc2, student s
where sc1.s_id = sc2.s_id 
and sc1.c_id = 1 
and sc2.c_id = 2
and sc1.s_score > sc2.s_score 
and s.s_id = sc1.s_id;

-- 方法3
select s.*,sc1.s_score score_01,sc2.s_score score02 
from student s
inner join (select * from score where c_id = 1) sc1
on s.s_id = sc1.s_id
inner join (select * from score where c_id = 2) sc2
on s.s_id = sc2.s_id
where sc1.s_score > sc2.s_score;

2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数

       同(1)

3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

-- 方法1
select s.s_id, s_name, round(avg(s_score),2) avg_score
from student s, score
where s.s_id = score.s_id
group by s.s_id,s_name
having avg_score >= 60;

-- 方法2
select s.s_id, s_name, avg_score
from student s, 
     (select s_id, round(avg(s_score),2) avg_score 
		  from score
			group by s_id
			having avg_score >= 60) as temp
where s.s_id = temp.s_id;

4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩(包括有成绩的和无成绩的)

-- 方法1 (先找到平均成绩小于60的学生信息,在用union连接,加上没有选课的学生信息)
select s.s_id, s_name, avg_score
from student s, 
    (select s_id, round(avg(s_score),2) avg_score
		 from score
		 group by s_id
		 having avg_score < 60) temp
where s.s_id = temp.s_id
union
select s_id, s_name, 0
from student 
where s_id not in (select s_id from score)


-- 方法2  没有成绩即该学生没有选课, 查询结果需要包含成绩为空的学生信息, 使用外连接
select s.s_id, s_name,
      (case when avg_score is null then 0 else avg_score end) avg_score 
from student s
left join 
    (select s_id ,round(avg(s_score),2) avg_score
		 from score
		 group by s_id) temp 
on temp.s_id = s.s_id
where avg_score < 60 or avg_score is null

5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

-- 方法1
select s.s_id, s_name, count(c_id) cnt_course,
       (case when sum(s_score) is null then 0 else sum(s_score) end) sum_score
from student s 
left join score 
on score.s_id = s.s_id
group by s.s_id, s_name	

-- 方法2
select s.s_id, s_name,
       (case when cnt_course is null then 0 else cnt_course end) cnt_course,
			 (case when sum_score is null then 0 else sum_score end) sum_score
from student s 
left join 
     (select s_id, count(c_id) cnt_course, sum(s_score) sum_score
		  from score
			group by s_id) temp
on temp.s_id = s.s_id

6、查询"李"姓老师的数量

select count(t_name)  from teacher where t_name like '%李%';

7、询学过"张三"老师授课的同学的信息

-- 方法1(嵌套查询)
select * 
from student
where s_id IN(
     select s_id 
		 from score 
		 where c_id IN(
		     select c_id
				 from course
				 where t_id IN(
				     select t_id
						 from teacher
						 where t_name = '张三'))); 

-- 方法2(自然连接)
select student.* 
from student natural join score
						 natural join course
						 natural join teacher
where t_name = '张三';

8、查询没学过"张三"老师授课的同学的信息

select * from student
where s_id NOT IN (
    select s_id from score
		where c_id IN(
		    select c_id from course
				where t_id IN(
				    select t_id from teacher
						where t_name='张三')));		

9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

-- 方法1
select * from student
where s_id IN (select s_id from score where c_id = 1)
and s_id IN (select s_id from score where c_id = 2)


-- 方法2
select s.* 
from student s,
		 (select s_id from score where c_id = 1) sc1,
		 (select s_id from score where c_id = 2) sc2
where s.s_id = sc1.s_id
and s.s_id = sc2.s_id

10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

-- 方法1
select * from student
where s_id IN (select s_id from score where c_id = 1)
and s_id NOT IN (select s_id from score where c_id = 2)

-- 方法2(使用except,做减法, 包含条件1但不包含条件2的学生)
select s.* 
from student s natural join score where c_id = 1
except 
select s.* 
from student s natural join score where c_id = 2

11、查询没有学全所有课程的同学的信息

-- 方法1 (用union连接 没选修完课程的学生id 和 没有选修课程的学生id)
select s.* from student s
where s_id IN (
               select s_id 
							 from score
							 group by s_id
							 having count(*) < (select count(*) from course)
							 )
union 
select s.* from student s
where s_id NOT IN (
							 select s_id
							 from score
              )		

-- 方法2  查询选修全部课程的学生—————即不存在这样一个学生,该生没有
select * from student
where s_id IN (
               select s_id from student s1
							 where NOT EXISTS (
							       select * from course
										 where NOT EXISTS (
										      select * from score
													where score.s_id = s1.s_id
													and score.c_id = course.c_id)))												 

12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息

-- 方法1
select * from student s
where s_id <> 1
and s_id IN (
             select s_id from score
						 where c_id IN(
						              select c_id from score
													where s_id = 1
													)
						 )

-- 方法2
select distinct s.*
from student s, score sc1, score sc2
where s.s_id = sc1.s_id
and sc1.c_id = sc2.c_id
and sc2.s_id = 1
and sc1.s_id <> 1

13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

-- (即 没有一门课程是01同学选了,而其他同学没有选的)
select s.* from student s
where s.s_id <> 1
and NOT EXISTS (
                select * from score sc1
								where sc1.s_id = 1
								and NOT EXISTS (
								                select * from score sc2
																where sc2.s_id = s.s_id
																and sc2.c_id = sc1.c_id
																)
								)

14、查询没学过"张三"老师讲授的任一门课程的学生姓名

select * from student
where s_id NOT IN (
    select s_id from score
		where c_id IN(
		    select c_id from course
				where t_id IN(
				    select t_id from teacher
						where t_name='张三')));		

15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

-- 方法1
select s.s_id, s_name, avg_score
from student s join(
								 select s_id , round(avg(s_score),2) avg_score
								 from score
								 where s_score < 60
								 group by s_id
								 having count(*) >= 2
								 )temp
on temp.s_id = s.s_id
)


-- 方法2
select s.s_id, s_name, round(avg(s_score),2) avg_score
from student s, score sc
where s.s_id = sc.s_id
and s_score < 60
group by s.s_id, s_name
having count(*) >= 2;

16、检索"01"课程分数小于60,按分数降序排列的学生信息

-- 方法1
select s.* 
from student s join (
                    select s_id, s_score
										from score
										where c_id = 1
										and s_score < 60
)temp on temp.s_id = s.s_id
and s_score < 60
order by s_score desc


-- 方法2
select s.*
from student s
join score sc on sc.s_id = s.s_id
where c_id = 1
and s_score < 60
order by s_score desc

17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

select s.s_id, s_name,
			 sum(case c_id when 1 then s_score else 0 end) 语文,
			 sum(case c_id when 2 then s_score else 0 end) 数学,
			 sum(case c_id when 3 then s_score else 0 end) 英语,
			 ifnull(round(avg(s_score),2),0) 平均成绩 
 from student s 
 left join score sc 
 on sc.s_id = s.s_id
 group by s.s_id,s_name
 order by 平均成绩 desc;

18、查询各科成绩最高分、最低分、平均分、及格率(60分以上)、中等率(70-80)、优良率(80-90)、优秀率(90以上)

select c.c_id 课程ID, c_name 课程名称, 
       max(s_score) 最高分, min(s_score) 最低分, round(avg(s_score),2) 平均分,
			 concat(round(sum(case when s_score >=60 then 1 else 0 end)/count(*) * 100, 2), '%') 及格率,
			 concat(round(sum(case when s_score between 70 and 80 then 1 else 0 end)/count(*) * 100, 2), '%') 中等率,
			 concat(round(sum(case when s_score between 80 and 90 then 1 else 0 end)/count(*) * 100, 2), '%') 优良率,
			 concat(round(sum(case when s_score >= 90 then 1 else 0 end)/count(*) * 100, 2), '%') 优秀率
from course c join score sc on sc.c_id = c.c_id
group by c.c_id, c_name

19、按各科成绩进行排序,并显示排名

-- 先查询各科的排名,再将查询结果命名为temp, 和student,course连接
select c.c_id, c_name, s.s_id, s_name, s_score, srank
from student s, course c,
		(select s_id, c_id, s_score,
       1+(select count(*)
			    from score sc1
					where sc1.c_id = sc2.c_id
					and sc1.s_score > sc2.s_score
					) srank
     from score sc2
		 )temp
where temp.s_id = s.s_id
and temp.c_id = c.c_id
order by temp.c_id, s_score desc

20、查询学生的总成绩并进行排名

select s.s_id, s_name, sum_score,
			1+(select count(*) 
			   from
				   	(select s_id, sum(s_score) sum_score from score group by s_id) t1
				 where t1.sum_score > t2.sum_score	
				 ) srank	
from (select s_id, sum(s_score) sum_score
      from score 
			group by s_id) t2,
			student s
where s.s_id = t2.s_id
order by srank asc			

-- 方法2
WITH t as
  (select s_id, sum(s_score) sum_score
	 from score 
	 group by s_id
	 )
select s_name, t2.s_id, sum_score,
       1+(select count(*) 
			    from t t1
					where t1.sum_score > t2.sum_score
					) srank
from t t2, student s
where t2.s_id = s.s_id
order by srank		

21、查询不同老师所教不同课程平均分从高到低显示

-- 方法1
select t.t_id, t_name, c.c_id, c_name, round(avg(s_score),2) avg_score
from score sc
join course c on c.c_id = sc.c_id
join teacher t on t.t_id = c.t_id
group by t.t_id, t_name, c.c_id, c_name
order by t.t_id, avg_score desc

-- 方法2
select t.t_id, t_name, c.c_id, c_name, avg_score
from (
      select c_id, round(avg(s_score),2) avg_score
			from score
			group by c_id
			) sc
join course c on c.c_id = sc.c_id
join teacher t on t.t_id = c.t_id
order by t.t_id, avg_score desc

22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

-- 22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩
select c.c_id, c_name, s.s_id, s_name, s_score, srank
from student s, course c,
		 (select s_id, c_id, s_score,
			1+(select count(*)
			   from score sc1
				 where sc1.s_score > sc2.s_score
				 )srank
			from score sc2	
			) temp
where s.s_id = temp.s_id
and c.c_id = temp.c_id
and srank in (2,3)
order by temp.c_id, s_score desc

23、统计各科成绩各分数段人数:课程编号,课程名称,[100-85],[85-70],[70-60],[0-60]及所占百分比

select c.c_id 课程ID, c_name 课程名称, 
			 concat(round(sum(case when s_score between 0 and 60 then 1 else 0 end)/count(*) * 100, 2), '%') 及格率,
			 sum(case when s_score between 0 and 60 then 1 else 0 end) 及格人数,
			 concat(round(sum(case when s_score between 60 and 70 then 1 else 0 end)/count(*) * 100, 2), '%') 中等率,
			 sum(case when s_score between 60 and 70 then 1 else 0 end) 中等人数,
			 concat(round(sum(case when s_score between 70 and 85 then 1 else 0 end)/count(*) * 100, 2), '%') 优良率,
			 sum(case when s_score between 70 and 85 then 1 else 0 end) 优良人数,
			 concat(round(sum(case when s_score between 85 and 100 then 1 else 0 end)/count(*) * 100, 2), '%') 优秀率,
			 sum(case when s_score between 85 and 100 then 1 else 0 end) 优秀人数
from course c join score sc on sc.c_id = c.c_id
group by c.c_id, c_name

24、查询学生平均成绩及其名次

-- 先计算每个学生的平均成绩, 再用排名的sql语句
WITH t as(
 select s_id, round(avg(s_score),2) avg_score
 from score
 group by s_id
)
select t2.s_id, s_name, avg_score,
       1+ (
					  select count(*)
					  from t t1
						where t1.avg_score> t2.avg_score
			  ) srank
from t t2 natural join student
order by srank asc

25、查询各科成绩前三名的记录

select c_id, c_name, s_id, s_name, s_score
from student natural join score
						 natural join course
						 natural join (
	                         select s_id, c_id, s_score
													 from score s1
													 where (
																		 select count(*)
																		 from score s2
																		 where s2.c_id = s1.c_id
																		 and s2.s_score > s1.s_score 
													         ) < 3				 
  						              ) temp
order by c_id, s_score desc

26、查询每门课程被选修的学生数

-- 方法1
select c_id, c_name, count(*) cnt_studnet
from student s natural join score sc 
               natural join course c
group by c_id, c_name

-- 方法2
select c.c_id, c_name, cnt_student
from course c join ( 
                    select c_id, count(*) cnt_student
										from score
										group by c_id
										) temp
on temp.c_id = c.c_id

27、查询出只有两门课程的全部学生的学号和姓名

-- 方法1
select s_id, s_name
from student
where s_id IN (
               select s_id
							 from score
							 group by s_id
							 having count(*)=2	
              )

-- 方法2
select s_id, s_name
from student natural join score
group by s_id, s_name
having count(*) = 2

28、查询男生、女生人数

-- 方法1
select s_sex, count(*) cnt_sex
from student
group by s_sex;

-- 方法2
select  sum(case s_sex when '男' then 1 else 0 end) 男生人数,
        sum(case s_sex when '女' then 1 else 0 end) 女生人数
from student

29、查询名字中含有"风"字的学生信息

select * from student
where s_name like '%风%'

30、查询同名同性学生名单,并统计同名人数

select s_name, s_sex, count(*) cnt_student
from student
group by s_name, s_sex
having cnt_student > 1

31、查询1990年出生的学生名单

select * from student
where year(s_birth) = '1990'

32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

-- 方法1
select c_id, c_name, round(avg(s_score) ,2) avg_score
from course natural join score 
group by c_id, c_name
order by avg_score desc, c_id asc

-- 方法2 
select c.c_id, c_name, round(avg(s_score) ,2) avg_score
from course c 
join score sc on sc.c_id = c.c_id
group by c.c_id, c_name 
order by avg_score desc, c_id asc

-- 方法3
select c.c_id, c_name, avg_score
from course c join (
                     select c_id, round(avg(s_score) ,2) avg_score
										 from score
										 group by c_id
										) temp
on temp.c_id = c.c_id
order by avg_score desc, c_id asc

33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

-- 方法1
select s_id, s_name, round(avg(s_score) ,2) avg_score
from student natural join score 
group by s_id, s_name
having avg_score >= 85;

-- 方法2
select s.s_id, s_name, avg_score
from student s join (
										select s_id, round(avg(s_score) ,2) avg_score
										from score
										group by s_id
										) temp
on temp.s_id = s.s_id
where avg_score >= 85	

34、查询课程名称为"数学",且分数低于60的学生姓名和分数

-- 方法1
select s_name, s_score
from student natural join score
             natural join course
where c_name = '数学'
and s_score < 60


-- 方法2
select s_name, s_score
from student s join (
                      select s_id, c_id, s_score
											from score
											where s_score < 60
											) temp
on temp.s_id = s.s_id
join course c on c.c_id = temp.c_id
where c_name = '数学'

35、查询所有学生的课程及分数情况

select s.s_id, s_name,
			sum(case c_id when 1 then s_score else 0 end) 语文,
			sum(case c_id when 2 then s_score else 0 end) 数学,
			sum(case c_id when 3 then s_score else 0 end) 英语
from student s natural join score
group by s.s_id, s_name
							 

36、查询任何一门课程成绩在70分以上的学生姓名、课程名称和分数

-- 任何一门成绩都要大于70, 则表示最低成绩也要大于70
select s_name, c_name, s_score
from student s natural join score
               natural join course
where s_id IN (
                select s_id
								from score
								group by s_id
								having min(s_score) > 70
								)

37、查询课程不及格的学生

-- 方法1
select s.* , c_name, s_score
from student s natural join score
               natural join course
where s_score < 60

-- 方法2
select s_name, c_name, s_score
from score sc 
join student s on s.s_id = sc.s_id
join course c on c.c_id = sc.c_id
where s_score < 60

38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名

-- 方法1
select s_id, s_name, s_score
from student s natural join score sc 
               natural join course c
where c_id = 1
and s_score >= 80

-- 方法2
select s.s_id, s_name
from student s
where s_id IN (
               select s_id 
							 from score
							 where c_id = 1
							 and s_score >= 80
							 )

39、求每门课程的学生人数

-- 方法1
select c.c_id, c_name, count(*) cnt_student
from course c natural join score 
group by c.c_id, c_name

-- 方法2
select c.c_id, c_name, cnt_student
from course c join (
                    select c_id, count(*) cnt_student
										from score
										group by c_id
										) temp
on temp.c_id = c.c_id

40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩


-- 方法1 张三老师所授课程中学生的最高分数,  再求张三老师所授课程中学生的成绩等于最高分数的学生
select s.*, s_score
from student s natural join score sc 
               natural join course c
							 natural join teacher t
where t_name = '张三'
and s_score = (
							  select max(s_score)
								from student s natural join score sc 
								               natural join course c
							                 natural join teacher t
                where t_name = '张三'
              )

-- 方法2 
select s.*, s_score
from student s natural join score sc 
							natural join course c
							natural join teacher t
where t_name = '张三'
order by s_score desc limit 1

41、查询不同课程成绩相同的学生的学生编号、课程编号、学生成绩


-- 按成绩分组,选择count(*)>1 ,再根据s_score 选择score表中的元组
select * from score
where s_score IN (
                   select s_score from score
									 group by s_score
									 having count(*) > 1
									 )

42、查询每门课程成绩最好的前三名

select c.c_id, c_name, s.s_id, s_name, s_score
from (
      select s_id, c_id, s_score
			from score s1
			where (
			        select count(*) 
							from score s2
							where s2.s_score > s1.s_score
							and s2.c_id = s1.c_id
							) < 3
			) temp
join student s on s.s_id = temp.s_id
join course c on c.c_id = temp.c_id
order by temp.c_id, temp.s_score desc

43、统计每门课程的学生选修人数(超过5人的课程才统计)

select c_id, c_name , count(*) cnt_student
from score natural join course
group by c_id, c_name
having count(*)>=5

44、检索至少选修两门课程的学生学号

select * from student
where s_id IN (
               select s_id from score
							 group by s_id
							 having count(*) >=2
							 )

45、查询选修了全部课程的学生信息

-- 方法1
select * from student s
where s_id IN (
							 select s_id from score 
							 group by s_id
							 having count(*) = (
							                      select count(*) from course
												          )
						   )

-- 方法2
select * from student 
where NOT EXISTS (
                   select * from course
									 where NOT EXISTS (
									                    select * from score
																			where score.c_id = course.c_id
																			and student.s_id = score.s_id
																			)
							    )

46、查询各学生的年龄(周岁)

select *,
			 (
			      date_format(now(), '%Y') - date_format(s_birth, '%Y') -
			       (case when date_format(now(), '%M%D')>date_format(s_birth, '%M%D') then 0 else 1 end)
			 ) as age
from student 

47、查询本周过生日的学生

# 1、选取原生日里的月日成分(按照字符串进行处理)
#    >>> MID(sage,6,5)
# 2、将选取的月日成分与今年的年份相接
#    >>> CONCAT(YEAR(CURDATE()),"-" ,MID(sage,6,5))
# 3、使拼接后的日期的周数和 now/curdate 的周数相等即可确定本周过生日
select * from student
where week(concat(year(curdate()),"-", mid(s_birth,6,5))) = week(curdate())

48、查询下周过生日的学生

select * from student
where week(concat(year(curdate()),"-", mid(s_birth,6,5))) = week(curdate())+1

49、查询本月过生日的学生

select * from student where month(s_birth) = month(now())

select * from student
where month(concat(year(curdate()), "-", mid(s_birth,6,5))) = month(curdate())

50、查询12月份过生日的学生

select * from student where month(s_birth)=12

select * from student
where month(concat(year(curdate()), "-", mid(s_birth,6,5))) = 12

最后,博主把整合版本的sql练习放在下面,需要的宝子们自取。

student-test.sql资源-CSDN文库文章来源地址https://www.toymoban.com/news/detail-447010.html

到了这里,关于MySQL综合练习(50道)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • MySQL---单表查询综合练习

    创建emp表 CREATE TABLE emp( empno INT(4) NOT NULL COMMENT \\\'员工编号\\\', ename VARCHAR(10) COMMENT \\\'员工名字\\\', job VARCHAR(10) COMMENT \\\'职位\\\', mgr INT(4) COMMENT \\\'上司\\\', hiredate DATE COMMENT \\\'入职时间\\\', sal INT(7) COMMENT \\\'基本工资\\\', comm INT(7) COMMENT \\\'补贴\\\', deptno INT(2) COMMENT \\\'所属部门编号\\\' ); emp表添加主键 mysql alte

    2024年01月21日
    浏览(40)
  • python-Excel数据模型文档转为MySQL数据库建表语句(需要连接数据库)-工作小记

    将指定Excel文档转为create table 建表语句。该脚本适用于单一且简单的建表语句 呈现效果  代码   仅作为笔记记录,如有问题请各位大佬来指导 

    2024年02月14日
    浏览(47)
  • MySQL---多表分组查询综合练习

    CREATE TABLE dept ( deptno INT(2) NOT NULL COMMENT \\\'部门编号\\\', dname VARCHAR (15) COMMENT \\\'部门名称\\\', loc VARCHAR (20) COMMENT \\\'地理位置\\\' ); 添加dept表主键 mysql alter table dept add primary key(deptno);           Query OK, 0 rows affected (0.02 sec)                       Records: 0  Duplicates: 0  Warnings:

    2024年01月23日
    浏览(48)
  • MySQL---多表等级查询综合练习

    CREATE TABLE emp( empno INT(4) NOT NULL COMMENT \\\'员工编号\\\', ename VARCHAR(10) COMMENT \\\'员工名字\\\', job VARCHAR(10) COMMENT \\\'职位\\\', mgr INT(4) COMMENT \\\'上司\\\', hiredate DATE COMMENT \\\'入职时间\\\', sal INT(7) COMMENT \\\'基本工资\\\', comm INT(7) COMMENT \\\'补贴\\\', deptno INT(2) COMMENT \\\'所属部门编号\\\' ); emp表添加主键 mysql alter table emp a

    2024年01月23日
    浏览(41)
  • 六、MySQL---综合练习题(单表、多表、分组函数以及等级查询)

    数据库:dept_emp 1. dept表 :deptno’部门编号’、dname’部门名称’、loc’地理位置’; 2. emp表 :empno ‘员工编号’、ename ‘员工名字’、job ‘职位’、mgr ‘上司’、hiredate ‘入职时间’、sal ‘基本工资’,comm ‘补贴’,deptno ‘所属部门编号’; 3. salgrade :grade ‘工资等级’

    2024年01月21日
    浏览(50)
  • Mysql - 常用插入数据的三种方法详解及练习

    目录 🥙8.1.1 mysql中常用的三种插入数据的语句 1. insert into - 插入数据 2. replace into - 插入替换数据 3. insert ignore - 如果已存在,忽略当前新数据 🥙8.1.2 以上三种方法的练习及区分 🥙8.1.3 说明 🥙8.1.4 牛客练习题 1. insert into - 插入数据 数据库会检查 主键 ,如果出现重复会报错

    2024年03月24日
    浏览(42)
  • hive建表,与插入数据

    思路,hive导入分区表,只能通过临时表导入。 固建立临时表(不分区),导入数据到临时表,创建分区表,通过【insert 分区表 select 临时表】 导入分区表 打开hue或者直接hive ----------------------------- 创建分区表 ----------------------------- 外部表external 以日期进行分区partitioned 数据

    2024年02月10日
    浏览(43)
  • Linux系统下SQLite创建数据库, 建表, 插入数据保姆级教程

    1,创建数据库: sqlite test.db  我这边是sqlite2版本, 直接使用命令sqlite test.db创建一个名称为test的数据库; test是你自定义是数据库名 , 创建好数据库后, 接下来开始创建表格 2.创建表格, 就是常规的sql建表语句 CREATE TABLE  ids_logs ( english_details TEXT, chinese_details TEXT, event_definition TEXT

    2024年02月08日
    浏览(60)
  • 单链表——单链表的定义及基本操作(头插法尾插法建表、查找、插入、删除等)

    上一篇我们已经完成了顺序表的实现和基本操作元素的增加、删除和查找 (链接直达:线性表元素的基本操作(C语言)【数据结构】-CSDN博客) 我们知道顺序表支持随机访问,可以通过下标来直接访问,同时也可以进行排序等优点;但是仍存在局限性,对顺序表的中部进行增加

    2024年04月10日
    浏览(45)
  • 【FusionInsight 迁移】HBase从C50迁移到6.5.1(02)C50上准备FTP Server

    在FusionInsight HD集群的HBase数据迁移过程中,需要通过FTP-Server从老集群FusinInsight C50上获取HBase的数据,因此需要在老集群FusinInsight C50中添加FTP-Server以及具有相应权限的用户ftpUser。 本文主要介绍如何在老集群FusinInsight C50中创建ftpUser用户以及如何添加配置FTP-Server。 使用admin用

    2024年02月09日
    浏览(32)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包