50道sql练习题

2 篇文章 0 订阅

题目见:https://blog.csdn.net/fashion2014/article/details/78826299/
以下为我的解法:

– 1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
SELECT 
s1.s_id,s1.s_birth,s1.s_name,s1.s_sex,s2.s_score as 01_score,s3.s_score as 02_score
FROM
	student s1
	JOIN score s2
	join score s3
	on s2.s_id=s3.s_id
	and s2.c_id='01'
	and s3.c_id='02'
	and s1.s_id=s2.s_id
	and s1.s_id=s3.s_id
	and s2.s_score>s3.s_score
– 2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数
SELECT 
s1.s_id,s1.s_birth,s1.s_name,s1.s_sex,s2.s_score as 01_score,s3.s_score as 02_score
FROM
	student s1
	JOIN score s2
	join score s3
	on s2.s_id=s3.s_id
	and s2.c_id='01'
	and s3.c_id='02'
	and s1.s_id=s2.s_id
	and s2.s_score<s3.s_score
– 3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
SELECT
	s.s_id,
	s.s_name,
	s1.avg_score 
FROM
	student s
	JOIN ( SELECT s_id, avg( s_score ) AS avg_score FROM score GROUP BY s_id HAVING avg( s_score ) > 60 ) s1 ON s.s_id = s1.s_id
– 4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩-- (包括有成绩的和无成绩的)
SELECT
	student.s_id,
	student.s_name,
	score.s_score as s_score
FROM
	student
	LEFT JOIN score ON student.s_id = score.s_id 
GROUP BY
	student.s_id 
HAVING
	 avg( score.s_score ) < 60 
union 
SELECT
	student.s_id,
	student.s_name,
	0  as s_score
FROM
	student
	where student.s_id not  in (select s_id from   score)
– 5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
SELECT
	student.s_id,
	student.s_name,
IF
	( s.sum_course IS NOT NULL, S.sum_course, 0 ) AS sum_course,
IF
	( s.sum_score IS NOT NULL, S.sum_score, 0 ) AS sum_score 
FROM
	student
	LEFT JOIN ( SELECT s_id, count( * ) AS sum_course, sum( s_score ) AS sum_score FROM score GROUP BY s_id ) s ON student.s_id = s.s_id

– 6、查询"李"姓老师的数量
select  count(0)  from teacher where t_name  like '李%'
– 7、查询学过"张三"老师授课的同学的信息
select student.*  from student join score  join course join teacher on 
student.s_id=score.s_id 
and score .c_id=course.c_id
and course.t_id=teacher.t_id
and teacher.t_name='张三'
– 8、查询没学过"张三"老师授课的同学的信息
select  student.*    from  student where s_id
 not in (
select student.s_id from student join score  join course join teacher on 
student.s_id=score.s_id 
and score .c_id=course.c_id
and course.t_id=teacher.t_id
and teacher.t_name='张三')
– 9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息
SELECT DISTINCT
	( student.s_id ),
	student.s_birth,
	student.s_name,
	student.s_sex 
FROM
	student
	JOIN score score1
	JOIN score score2 ON ( score1.c_id = '01' AND score2.c_id = '02' ) 
	AND student.s_id = score1.s_id 
	AND student.s_id = score2.s_id
– 10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息
SELECT DISTINCT
	student.*
FROM
	student 
WHERE
	student.s_id IN ( SELECT s_id FROM score WHERE score.c_id = '01' ) 
	AND student.s_id NOT IN ( SELECT s_id FROM score WHERE score.c_id = '02' )
– 11、查询没有学全所有课程的同学的信息
select from student  join score JOIN   
	( select s_id,count(*) sum_course from score GROUP BY s_id ) sumcourse 
	join  (select count(*) as sum_course_s  from course) sum_courses
	on student.s_id =sumcourse.s_id
	and sumcourse.sum_course!=sum_courses.sum_course_s
– 12、查询至少有一门课与学号为"01"的同学所学相同的同学的信息
SELECT
	distinct (student.s_id),student.s_birth,student.s_name,student.s_sex
FROM
	student , score s1 
WHERE
	student.s_id = s1.s_id 
	AND s1.c_id IN ( SELECT c_id FROM score WHERE score.s_id = '01' )	
– 13、查询和"01"号的同学学习的课程完全相同的其他同学的信息
SELECT student.* FROM student join 
#找到和01号同学学的课程总数相同的同学 
( SELECT student.s_id AS sid FROM student JOIN score ON student.s_id = score.s_id GROUP BY score.s_id HAVING count(c_id) = ( SELECT count(c_id) FROM score WHERE s_id = '01' ) ) s1
JOIN 
#找到没学01号同学没学的课程的同学 
( SELECT student.s_id AS sid FROM student,
         score WHERE student.s_id = score.s_id AND score.c_id NOT IN 
				( SELECT c_id FROM course WHERE c_id NOT IN ( SELECT c_id FROM score WHERE s_id = '01' ) ) 
					GROUP BY score.s_id )  s2

on student.s_id=s1.sid
and s1.sid=s2.sid
– 14、查询没学过"张三"老师讲授的任一门课程的学生姓名
select s_name  from  student where s_id  not in (select s_id  from  score where c_id = (select c_id from course,teacher where course.t_id=teacher.t_id and teacher.t_name='张三'))
– 15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩
select student.s_id,student.s_name,avg(score.s_score)  from student left join score on student.s_id=score.s_id  WHERE student.s_id in (
select s_id from  score where s_score<60 GROUP BY s_id HAVING(count(1)>1)
)
group by student.s_id
– 16、检索"01"课程分数小于60,按分数降序排列的学生信息
select student.* ,score.s_score from  student,score where student.s_id=score.s_id and score.c_id='01' and score.s_score<60 ORDER BY score.s_score desc```

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值