1、将所有的课程的名称以及对应的任课老师姓名打印出来,如下:
SELECT cname,tname FROM course LEFT JOIN teacher ON teacher.tid=course.cid;
2、查询学生表中男女生各有多少人? 如下:
SELECT gender,COUNT(sid) 人数 from student GROUP BY gender;
3、查询物理成绩等于100的学生的姓名?如下:
SELECT sid,sname FROM student WHERE sid in (SELECT student_id from score WHERE course_id=2 and num=100);
4、查询平均成绩大于八十分的同学的姓名和平均成绩,如下:
SELECT sname 姓名,平均成绩 FROM student INNER JOIN (SELECT student_id,AVG(num) 平均成绩 FROM score GROUP BY student_id HAVING AVG(num)>80)AS s ON student.sid=s.student_id
SELECT sname,AVG(num) FROM student INNER JOIN score on student.sid=score.student_id
GROUP BY student_id having AVG(num)>80;
5、查询所有学生的学号,姓名,选课数,总成绩
SELECT sid,sname,选课数,总成绩 FROM student LEFT JOIN (SELECT student_id,COUNT(sid) 选课数,SUM(num) 总成绩 FROM score GROUP BY student_id)AS s ON student.sid=s.student_id
SELECT student.sid,sname,COUNT(student.sid),SUM(num) FROM student INNER JOIN score on score.student_id=student.sid
GROUP BY student_id;
6、查询姓李老师的个数
SELECT COUNT(tname) from teacher WHERE tname like '李%';
7、查询没有报李平老师课的学生姓名
SELECT sid,sname FROM student WHERE sid NOT in (SELECT DISTINCT student_id FROM score WHERE course_id in (SELECT cid FROM course WHERE teacher_id=(SELECT tid FROM teacher WHERE tname="李平老师")))
SELECT sname FROM student WHERE sid not in
(SELECT DISTINCT student_id FROM score WHERE course_id in
(SELECT cid FROM teacher INNER JOIN course on teacher.tid=course.teacher_id
WHERE tname LIKE '李平%'));
8、查询物理课程比生物课程高的学生的学号
SELECT s1.student_id FROM (SELECT * FROM score WHERE student_id in (SELECT student_id FROM score WHERE course_id=2 OR course_id=1 GROUP BY student_id HAVING COUNT(sid)=2) AND course_id=1)AS s1
LEFT JOIN
(SELECT * FROM score WHERE student_id in (SELECT student_id FROM score WHERE course_id=2 OR course_id=1 GROUP BY student_id HAVING COUNT(sid)=2) AND course_id=2)AS s2
ON s1.student_id=s2.student_id
WHERE s1.num<s2.num
9、查询没有同时选修物理课程和体育课程的学生姓名
SELECT sid,sname FROM student WHERE sid NOT in (SELECT student_id FROM score WHERE course_id=2 or course_id=3
GROUP BY student_id HAVING COUNT(course_id)=2)
10、查询挂科超过两门(包括两门)的学生姓名和班级
SELECT sname FROM student WHERE sid in (SELECT student_id FROM score WHERE num<60 GROUP BY student_id HAVING COUNT(course_id)>=2)
11 、查询选修了所有课程的学生姓名
SELECT sname FROM student WHERE sid in (SELECT student_id FROM score GROUP BY student_id HAVING COUNT(course_id)=(SELECT COUNT(cid) FROM course))
12、查询李平老师教的课程的所有成绩记录
SELECT course_id 课程,num 分数 FROM score WHERE course_id in(SELECT cid FROM course WHERE teacher_id=(SELECT tid FROM teacher WHERE tname="李平老师"))
13、查询全部学生都选修了的课程号和课程名
SELECT course_id,COUNT(course_id) FROM score GROUP BY course_id HAVING COUNT(course_id)=(SELECT COUNT(sid) FROM score)