Mysql 练习题

course表
Mysql 练习题_第1张图片
student表
Mysql 练习题_第2张图片
sc表
Mysql 练习题_第3张图片
teacher表
Mysql 练习题_第4张图片

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

SELECT
	* 
FROM
	student s
	JOIN sc a ON s.SId = a.SId
	join  sc b on s.sid=b.sid and a.cid=01 and b.cid=02
	where a.score>b.score;

2、查询同时存在01课程和02课程的情况

SELECT
	* 
FROM
	sc a
	JOIN sc b ON a.sid = b.sid
	where a.cid='01' and b.cid='02'

3、查询存在01 课程但可能不存在02课程的情况(不存在时显示null)

SELECT
	* 
FROM
	sc a
	LEFT JOIN sc b ON a.sid = b.sid 
	AND b.cid = '02' 
WHERE
	a.cid = '01'

4、查询不存在01课程但存在02课程的情况

SELECT
	* 
FROM
	sc a 
WHERE
	sid NOT IN ( SELECT sid FROM sc b WHERE cid = '01' ) 
	AND cid = '02'

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

SELECT
	s.sid,
	s.sname,
	AVG( sc.score ) 
FROM
	student s
	JOIN sc ON s.sid = sc.sid 
GROUP BY
	sc.sid,
	s.sname
	having AVG(sc.score)>60

6、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩。(没成绩的显示null)

SELECT
	s.sid,
	s.sname,
	count( sc.cid ) as '选课总数',
	sum( score ) as '所有课程总成绩'
FROM
	student s
	LEFT JOIN sc ON s.sid = sc.sid group BY s.sid,
	s.sname;

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

SELECT
	s.* 
FROM
	student s
	JOIN sc ON s.sid = sc.sid
	JOIN course c ON sc.cid = c.CId
	JOIN teacher t ON c.tid = t.tid 
WHERE
	t.Tname = '张三';

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

select * from student where SId in 
(SELECT
	s.sid
FROM
	student s
left JOIN sc ON s.sid = sc.sid
	group by s.sid
	having count(sc.cid)<(select count(cid) from course))

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

SELECT DISTINCT
	s.* 
FROM
	student s
	JOIN sc ON s.sid = sc.sid 
WHERE
	cid IN ( SELECT cid FROM sc WHERE sid = 01 );

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

思路:1、没有01号同学课程以外的课程记录,
(01号同学学习了哪些课程?,找到学习01号课程外的同学,取个反)

2、与01号同学学习的课程数量相同


select sc.sid from sc 
join student s on sc.sid=s.sid
where sc.sid not in (select sid from sc where cid not in(select cid from sc where sid='01'))
group by s.sid
having count(cid)=(select count(cid) from sc where sid='01')

11、查询没学过‘张三’ 老师任一门课的学生姓名。

思路:找出学习过的,再not in

SELECT
	* 
FROM
	student 
WHERE
	sid NOT IN (
	SELECT
		s.sid 
	FROM
		student s
		LEFT JOIN sc ON s.sid = sc.sid 
WHERE
	sc.cid = ( SELECT cid FROM course c JOIN teacher t ON c.tid = t.tid WHERE t.Tname = '张三' )

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

SELECT
	s.sid,s.sname,avg(score)
FROM
	student s
	JOIN sc ON s.sid = sc.sid 
WHERE
	score < 60
GROUP BY
	s.sid,s.sname
HAVING
 count( score )>=2;

13、查询课程ID、课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率 , 及格为>=60,中等为:[70-80),优良为:[80-90),优秀为:>=90,要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

SELECT
	sc.cid,
	c.cname,
	max( sc.score ) as '平均分',
	min( sc.score ) as '最低分',
	avg( sc.score ) as '平均分',
	count(1) as '选修人数',
	sum(case when score>=60 then 1 else 0 end )/count(1) as '及格率',
	sum(case when score>=70 and score<80 then 1 else 0 end)/count(1) as '中等率',
	sum(case when score >=80 and score <90 then 1 else 0 end )/count(1) as '优良率',
	sum(case when score >=90 then 1 else 0 end )/count(1) as '优秀率'
FROM
	sc
	JOIN course c ON sc.cid = c.cid 
GROUP BY
	sc.cid,
	c.cname;
order by count(1) desc ,cid 

14、按各科成绩进行排序,并显示排名,score重复时继续排序

select cid,score,@rank:=@rank+1 as '排名' from sc,(select @rank:=0) as t order by score desc

15、按各科成绩进行排序,并显示排名,Score重复时合并名次。

SELECT
	cid,
	score,
CASE
		WHEN @score = score 
		THEN @rank 
		when @score:=score
		THEN @rank:=@rank+1
	  END 
		AS '排名'

FROM
	sc,(
	SELECT
	@rank :=0,
	@score :=NULL) as t 
	order by score desc;

16、统计各科成绩各分数段人数:课程编号,课程名称,(85-100】,(70-85】,及所占百分比

百分比可以用cancat函数

SELECT
	sc.cid,
	cname,
	concat(sum(case when score>85 and score <=100 then 1 else 0 end)/count(1)*100,'%') as '(85-100]',
	concat( sum(case when score>70 and score <=85 then 1 else 0 end)/count(1)*100,'%')as '(70-85]',
FROM
	sc
	JOIN course c ON sc.cid = c.cid 
GROUP BY
	sc.cid,
	c.cname

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

思路:前三名转换为:若大于此成绩的的数量少于3即为前三名。

SELECT
	* 
FROM
	sc a 
WHERE
	( SELECT count( 1 ) FROM sc b WHERE b.cid = a.cid AND b.score > a.score )< 3 
ORDER BY
	score DESC

18、查询只选修两门课程的学生学号和姓名

SELECT
	s.sid,
	s.sname,
	count(1) as '选课数'
FROM
	student s
	JOIN sc ON s.sid = sc.sid 
GROUP BY
	s.sid,
	s.sname
HAVING
	count( 1 )=2

19、查询男生女生人数

select s.ssex,count(1) from student s group by s.ssex

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

SELECT
	a.sname,
	a.ssex,
	count( 1 ) as '同名同性别人数'
FROM
	`student` a
	JOIN student b ON a.sname = b.sname 
	AND a.ssex = b.ssex 
	AND a.sid != b.sid
	group by a.sname,a.ssex

21、查询1990年出生的学生名单
用year()函数

SELECT
	* 
FROM
	student s 
WHERE
	YEAR ( sage )= '1990'

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

SELECT
	cid,
	avg( score ) 
FROM
	sc 
GROUP BY
	cid 
ORDER BY
	avg( score ),
	cid

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

SELECT
	s.sid,
	s.sname,
	avg( score ) 
FROM
	sc
	JOIN student s ON sc.sid = s.sid 
GROUP BY
	s.sid,
	s.sname 
HAVING
	avg( score )>= 85 
ORDER BY
	avg(
	score 
	)

25、查询课程名称为‘数学’,且分数低于60的学生姓名和分数。

SELECT
	s.sname,
	score 
FROM
	student s
	JOIN sc ON s.sid = sc.sid
	JOIN course c ON sc.cid = c.cid 
WHERE
	cname = '数学' 
	AND score < 60;

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

SELECT
	s.sid,
	s.sname,
	c.cname,
	sc.score 
FROM
	student s
	JOIN sc ON s.sid = sc.sid
	JOIN course c ON sc.cid = c.cid 
WHERE
	sc.score > 70

27、查询选修‘张三’老师所授课程的学生中成绩最高的学生信息及其成绩。

SELECT
	s.*,
	sc.score 
FROM
	student s
	JOIN sc ON s.sid = sc.sid
	JOIN course c ON sc.cid = c.cid
	JOIN teacher t ON c.tid = t.tid 
WHERE
	t.tname = '张三' 
ORDER BY
	score DESC 
	LIMIT 1

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

SELECT
	s.sid,
	a.cid,
	a.score 
FROM
	student s
	JOIN sc a ON s.sid = a.sid
	join sc b on a.sid=b.sid 
	where a.cid!=b.cid and a.score=b.score
	group by s.sid,a.cid,a.score

29、查询每门课程成绩最好的前两名

SELECT
	a.cid,
	a.score
FROM
	sc a where ( SELECT count(*) FROM sc b WHERE a.CId = b.CId AND b.score > a.score )<2 
ORDER BY
	a.cid

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

SELECT
	s.sid,
	s.sname 
FROM
	student s
	JOIN sc ON s.sid = sc.sid 
GROUP BY
	s.sid,
	s.sname 
HAVING count( cid )=(SELECT
		count( 1 ) 
FROM
	course)

31、查询个学生的年龄,只按月份来算,

SELECT YEAR
	(
	NOW())- YEAR ( sage ) 
FROM
	student

32、按照出生日期来算年龄,当前月日<出生的月日,则年龄减一。

SELECT
	*,
	timestampdiff(
		YEAR,
		sage,
	now()) 
FROM
	student

你可能感兴趣的:(Mysql)