SQL题目及其解答(学生、课程、成绩、教师)

–原题目 https://blog.csdn.net/tu451953337/article/details/45147757

1、第一种外键关联的方式

-- 创建学生表

drop table if exists t_student;
create table t_student(
	stuid int primary key,
	name varchar(20), 
	age int, 
	sex char(1)
);


INSERT INTO `t_student` VALUES ('1', 'bili', '25', '1');
INSERT INTO `t_student` VALUES ('2', 'mary', '22', '1');
INSERT INTO `t_student` VALUES ('3', 'lilei', '18', '0');
INSERT INTO `t_student` VALUES ('4', 'huaye', '23', '1');
INSERT INTO `t_student` VALUES ('5', 'zhangye', '23', '0');

-- 创建老师表
drop table if exists t_teacher;
create table t_teacher(
	tid int primary key, 
	tname varchar(20)
);

INSERT INTO `t_teacher` VALUES ('1', 'Mr.mao');
INSERT INTO `t_teacher` VALUES ('2', 'Mr.li');
INSERT INTO `t_teacher` VALUES ('3', 'Mr.kay');
INSERT INTO `t_teacher` VALUES ('4', 'Mr.zhu');


-- 创建课程表
drop table if exists t_course;
create table t_course(
	cid int not null primary key,
	name varchar(100),
	teacherid int
);

INSERT INTO `t_course` VALUES ('1', 'chinese', '1');
INSERT INTO `t_course` VALUES ('2', 'math', '3');
INSERT INTO `t_course` VALUES ('3', 'english', '1');
INSERT INTO `t_course` VALUES ('4', 'music', '2');
INSERT INTO `t_course` VALUES ('5', 'computer', '4');


-- 创建学生与课程成绩表
drop table if exists t_sc;
create table t_sc(
	stuid int,
	cid int, 
	score int
);

INSERT INTO `t_sc` VALUES ('1', '1', '80.00');
INSERT INTO `t_sc` VALUES ('1', '1', '80.00');
INSERT INTO `t_sc` VALUES ('1', '2', '75.00');
INSERT INTO `t_sc` VALUES ('1', '3', '68.00');
INSERT INTO `t_sc` VALUES ('1', '4', '70.00');
INSERT INTO `t_sc` VALUES ('1', '5', '90.00');
INSERT INTO `t_sc` VALUES ('2', '1', '65.00');
INSERT INTO `t_sc` VALUES ('2', '2', '40.00');
INSERT INTO `t_sc` VALUES ('2', '3', '90.00');
INSERT INTO `t_sc` VALUES ('2', '4', '74.00');
INSERT INTO `t_sc` VALUES ('2', '5', '72.00');
INSERT INTO `t_sc` VALUES ('3', '1', '64.00');
INSERT INTO `t_sc` VALUES ('3', '2', '94.00');
INSERT INTO `t_sc` VALUES ('3', '3', '63.00');
INSERT INTO `t_sc` VALUES ('3', '4', '72.00');
INSERT INTO `t_sc` VALUES ('3', '5', '64.00');
INSERT INTO `t_sc` VALUES ('4', '1', '64.00');
INSERT INTO `t_sc` VALUES ('4', '2', '28.00');
INSERT INTO `t_sc` VALUES ('4', '3', '97.00');
INSERT INTO `t_sc` VALUES ('4', '4', '68.00');
INSERT INTO `t_sc` VALUES ('4', '5', '71.00');
INSERT INTO `t_sc` VALUES ('5', '1', '36.00');
INSERT INTO `t_sc` VALUES ('5', '2', '68.50');
INSERT INTO `t_sc` VALUES ('5', '3', '84.00');
INSERT INTO `t_sc` VALUES ('5', '4', '79.00');
INSERT INTO `t_sc` VALUES ('5', '5', '80.00');
INSERT INTO `t_sc` VALUES ('6', '1', '81.00');
INSERT INTO `t_sc` VALUES ('6', '2', '78.00');
INSERT INTO `t_sc` VALUES ('6', '3', '79.00');
INSERT INTO `t_sc` VALUES ('6', '4', '84.00');
INSERT INTO `t_sc` VALUES ('6', '5', '82.60');

2、第二种有外键关联的创建表的方式

--create table t_student(stuid int primary key,name varchar(20), age int, sex char(1));

-- ----------------------------
-- Records of t_student
-- ----------------------------
INSERT INTO `t_student` VALUES ('1', 'bili', '25', '1');
INSERT INTO `t_student` VALUES ('2', 'mary', '22', '1');
INSERT INTO `t_student` VALUES ('3', 'lilei', '18', '0');
INSERT INTO `t_student` VALUES ('4', 'huaye', '23', '1');
INSERT INTO `t_student` VALUES ('5', 'zhangye', '23', '0');

--create table t_course(cid int primary key, name varchar(20), teacherid int);
--alter table t_course
--add constraint tcourse_fk foreign key (teacherid) references t_teacher(tid);

-- ----------------------------
-- Records of t_course
-- ----------------------------
INSERT INTO `t_course` VALUES ('1', 'chinese', '1');
INSERT INTO `t_course` VALUES ('2', 'math', '3');
INSERT INTO `t_course` VALUES ('3', 'english', '1');
INSERT INTO `t_course` VALUES ('4', 'music', '2');
INSERT INTO `t_course` VALUES ('5', 'computer', '4');


--create table t_sc(stuid int, cid int, score int);
--alter table t_sc 
--add constraint t_sc_pk primary key (stuid, cid);
--alter table t_sc
--add constraint t_sc_stuid_fk foreign key (stuid) references t_student(stuid);
--alter table t_sc
--add constraint t_sc_cid_fk foreign key (cid) references t_course(cid);

-- ----------------------------
-- Records of t_sc
-- ----------------------------
INSERT INTO `t_sc` VALUES ('1', '1', '80.00');
INSERT INTO `t_sc` VALUES ('1', '1', '80.00');
INSERT INTO `t_sc` VALUES ('1', '2', '75.00');
INSERT INTO `t_sc` VALUES ('1', '3', '68.00');
INSERT INTO `t_sc` VALUES ('1', '4', '70.00');
INSERT INTO `t_sc` VALUES ('1', '5', '90.00');
INSERT INTO `t_sc` VALUES ('2', '1', '65.00');
INSERT INTO `t_sc` VALUES ('2', '2', '40.00');
INSERT INTO `t_sc` VALUES ('2', '3', '90.00');
INSERT INTO `t_sc` VALUES ('2', '4', '74.00');
INSERT INTO `t_sc` VALUES ('2', '5', '72.00');
INSERT INTO `t_sc` VALUES ('3', '1', '64.00');
INSERT INTO `t_sc` VALUES ('3', '2', '94.00');
INSERT INTO `t_sc` VALUES ('3', '3', '63.00');
INSERT INTO `t_sc` VALUES ('3', '4', '72.00');
INSERT INTO `t_sc` VALUES ('3', '5', '64.00');
INSERT INTO `t_sc` VALUES ('4', '1', '64.00');
INSERT INTO `t_sc` VALUES ('4', '2', '28.00');
INSERT INTO `t_sc` VALUES ('4', '3', '97.00');
INSERT INTO `t_sc` VALUES ('4', '4', '68.00');
INSERT INTO `t_sc` VALUES ('4', '5', '71.00');
INSERT INTO `t_sc` VALUES ('5', '1', '36.00');
INSERT INTO `t_sc` VALUES ('5', '2', '68.50');
INSERT INTO `t_sc` VALUES ('5', '3', '84.00');
INSERT INTO `t_sc` VALUES ('5', '4', '79.00');
INSERT INTO `t_sc` VALUES ('5', '5', '80.00');
INSERT INTO `t_sc` VALUES ('6', '1', '81.00');
INSERT INTO `t_sc` VALUES ('6', '2', '78.00');
INSERT INTO `t_sc` VALUES ('6', '3', '79.00');
INSERT INTO `t_sc` VALUES ('6', '4', '84.00');
INSERT INTO `t_sc` VALUES ('6', '5', '82.60');


--create table t_teacher(tid int primary key, tname varchar(20));

-- ----------------------------
-- Records of t_teacher
-- ----------------------------
INSERT INTO `t_teacher` VALUES ('1', 'Mr.mao');
INSERT INTO `t_teacher` VALUES ('2', 'Mr.li');
INSERT INTO `t_teacher` VALUES ('3', 'Mr.kay');
INSERT INTO `t_teacher` VALUES ('4', 'Mr.zhu');

/*
select * from t_student for update;
select * from t_course for update;
select * from t_sc for update;
select * from t_teacher for update;
*/

3、关于以上表的50道SQL查询题目

1、查询“1”课程比“2”课程成绩高的所有学生的学号

select a.stuid
  from (select * from t_sc s where s.cid = 1) a,
       (select * from t_sc s where s.cid = 2) b
 where a.stuid = b.stuid
   and a.score > b.score;
/*
select a.S#
  from (select s#, score from SC where C# = '001') a,
       (select s#, score from SC where C# = '002')
 where a.score > b.score
   and a.s# = b.s#;
*/

–2、查询平均成绩大于80分的同学的学号和平均成绩;

select stuid, avg(score) from t_sc group by stuid having avg(score)>80;

–3、查询所有同学的学号、姓名、选课数、总成绩;

select s.stuid, s.name, c.xks, c.zcj
  from t_student s left outer join
       (select stuid, count(cid) xks, sum(score) zcj
          from t_sc
         group by stuid) c
 on s.stuid = c.stuid;

select s.stuid, s.name, count(c.cid), sum(c.score)
  from t_student s
  left Outer join t_sc c
    on s.stuid = c.stuid
 group by s.stuid, s.name;

–4、查询姓“李”的老师的个数;

select count(tid) from t_teacher where tname like '李%';

–5、查询没学过“叶平”老师课的同学的学号、姓名;

SELECT
	s.stuid,
	s. NAME
FROM
	t_student s,
	t_course c,
	t_teacher t,
	t_sc sc
WHERE
	s.stuid = sc.stuid
AND c.cid = sc.cid
AND c.teacherid = t.tid
AND t.tname != '叶平';
 
 select Student.S#, Student.Sname
   from Student
  where S# not in
        (select distinct (SC.S#) from SC,
                         Course,
                         Teacher where SC.C# = Course.C#and Teacher.T# = Course.T# andTeacher.Tname = '叶平');

–6、查询学过“001”并且也学过编号“002”课程的同学的学号、姓名;

SELECT
	s.stuid,
	s. NAME
FROM
	t_student s,
	t_sc c
WHERE
	c.cid = 1
AND s.stuid = c.stuid
AND EXISTS (
	SELECT
		*
	FROM
		t_sc sc
	WHERE
		sc.stuid = s.stuid
	AND sc.cid = 2
);

–7、查询学过“叶平”老师所教的所有课的同学的学号、姓名;

SELECT
	s.stuid,
	s. NAME
FROM
	t_student s,
	t_sc sc
WHERE
	s.stuid = sc.stuid
AND sc.cid IN (
	SELECT
		c.cid
	FROM
		t_teacher t,
		t_course c
	WHERE
		t.tid = c.teacherid
	AND t.tname = 'Mr.kay'
);

 select S#, Sname
   from Student
  where S# in
        (select S#
           from SC, Course, Teacher
          where SC.C# = Course.C# andTeacher.T# = Course.T#
            and Teacher.Tname = '叶平'
          group by S#
         having count(SC.C#) = (select count(C#) from Course,
                                      Teacher where Teacher.T# = Course.T# and Tname = '叶平'));

–8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名;

Select S#, Sname
  from (select Student.S#,
               Student.Sname,
               score,
               (select score
                  from SC SC_2
                 where SC_2.S# = Student.S#and SC_2.C# = '002') score2
          from Student, SC
         where Student.S# = SC.S# andC# = '001') S_2
 where score2 < score;

–9、查询所有课程成绩小于60分的同学的学号、姓名;

SELECT
	s.stuid,
	s. NAME
FROM
	t_student s
WHERE
	s.stuid NOT IN (
		SELECT
			sc.stuid
		FROM
			t_student s,
			t_sc sc
		WHERE
			sc.score >= 60
		AND s.stuid = sc.stuid
	);

SELECT
	s.stuid,
	s. NAME
FROM
	t_student s
WHERE
	NOT EXISTS (
		SELECT
			*
		FROM
			t_sc sc
		WHERE
			sc.stuid = s.stuid
		AND sc.score >= 60
	);

–10、查询没有学全所有课的同学的学号、姓名;

SELECT
	s.stuid,
	s. NAME
FROM
	t_student s,
	t_sc sc
WHERE
	s.stuid = sc.stuid
GROUP BY
	s.stuid,
	s. NAME
HAVING
	count(sc.cid) != (
		SELECT
			count(cid)
		FROM
			t_course
	);

–11、查询至少有一门课与学号为“1001”的同学所学相同的同学的学号和姓名;

SELECT DISTINCT
	s.stuid,
	s. NAME
FROM
	t_student s,
	t_sc sc
WHERE
	s.stuid = sc.stuid
AND s.stuid != 1
AND sc.cid IN (
	SELECT
		cid
	FROM
		t_sc
	WHERE
		stuid = 1
)
--select s.stuid,s.name from t_Student s, t_SC sc where s.stuid=SC.Stuid 
and sc.cid in (select Cid from t_SC where stuid='1');

–13、把“t_SC”表中“叶平”老师教的课的成绩都更改为此课程的平均成绩;

UPDATE t_sc sc1
SET sc1.score = (
	SELECT
		avg(score)
	FROM
		t_sc
	GROUP BY
		cid
	HAVING
		cid IN (
			SELECT
				cid
			FROM
				t_teacher t,
				t_course c
			WHERE
				t.tid = c.teacherid
			AND t.tname = 'Mr.mao'
		)
	AND cid = sc1.cid
);

/*
update t_sc sc_1
   set sc_1.score =
       (select avg(sc_2.score) from t_sc sc_2 where sc_2.cid = sc_1.cid)
       from t_course,
       t_teacher
 where t_course.Cid = t_sc.cid and t_course.tid = t_teacher.tid
   and t_teacher.tname = 'Mr.mao');
*/
SELECT
	*
FROM
	t_sc sc,
	t_course c,
	t_teacher t
WHERE
	sc.cid = c.cid
AND c.teacherid = t.tid
AND t.tname = 'Mr.mao'

–14、查询和“1002”号的同学学习的课程完全相同的其他同学学号和姓名;

SELECT
	*
FROM
	t_sc
WHERE
	stuid IN (
		SELECT
			stuid
		FROM
			t_sc
		GROUP BY
			stuid
		HAVING	count(*) = (
				SELECT
					count(*)
				FROM
					t_sc
				WHERE
					stuid = 6
			)
	)
AND cid IN (
	SELECT
		cid
	FROM
		t_sc
	WHERE
		stuid = 6
);
    

–15、删除学习“叶平”老师课的SC表记录

DELETE t_sc
WHERE
	cid IN (
		SELECT
			cid
		FROM
			t_course c,
			t_teacher t
		WHERE
			c.teacherid = t.tid
		AND t.tname = 'Mr.mao'
	);

–16、向SC表中插入一些记录,这些记录要求符合以下条件:没有上过编号“5”课程的同学学号、2号课的平均成绩;

INSERT INTO t_sc SELECT
	stuid,
	'5' cid,
	(
		SELECT
			avg(score)
		FROM
			t_sc sc
		WHERE
			sc.cid = 2
	) score
FROM
	t_student s
WHERE
	NOT EXISTS (
		SELECT
			*
		FROM
			t_sc sc
		WHERE
			sc.cid = 5
		AND sc.stuid = s.stuid
	);

–17、按平均成绩从高到低显示所有学生的“数据库”、“企业管理”、“英语”三门的课程成绩,按
–如下形式显示: 学生ID,数据库,企业管理,英语,有效课程数,有效平均分

select t.stuid,
       max(case t.cname when 'Chinese' then t.score end) Chinese,
       max(case t.cname when 'Math' then t.score end) Math,
       max(case t.cname when 'English' then t.score end) English,
       round(avg(t.score), 2) avgscore,
       count(t.cid) kcs
  from (select s.stuid, s.name stuname, sc.cid, sc.score, c.name cname
          from t_student s, t_sc sc, t_course c
         where s.stuid = sc.stuid
           and sc.cid = c.cid
           and c.name in ('Chinese', 'Math', 'English')) t
 group by t.stuid
 order by avgscore desc;
 /*
select sc.stuid,
       (select score from t_sc where cid=1 and stuid=sc.stuid) Chinese,
       (select score from t_sc where cid=2 and stuid=sc.stuid) Math,
       (select score from t_sc where cid=3 and stuid=sc.stuid) English,
       round(avg(sc.score),2) avgscore,
       count(c.cid) kcs
 from t_sc sc, t_course c where sc.cid=c.cid and c.name in ('Chinese', 'Math', 'English')
 group by sc.stuid
 order by avgscore desc;
*/

–18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

select cid, max(score) maxscore, min(score) minscore from t_sc group by cid order by cid;
 /*
 SELECT L.Cid As 课程ID, L.score AS 最高分, R.score AS 最低分
   FROM t_SC  L, t_SC  R
  WHERE L.Cid = R.Cid
    and L.score = (SELECT MAX(IL.score)
                     FROM t_SC  IL, t_Student  IM
                    WHERE L.Cid = IL.Cid
                      and IM.Stuid = IL.Stuid
                    GROUP BY IL.Cid)
    AND R.Score =
        (SELECT MIN(IR.score) FROM t_SC  IR WHERE R.Cid = IR.Cid GROUP BY IR.Cid);
*/

–19、按各科平均成绩从低到高和及格率的百分数从高到低顺序

select sc.cid, (select name from t_course where cid=sc.cid) cname,
       round(avg(sc.score),2) avgscore,
       round((select count(*) from t_sc where cid = sc.cid and score>=70)/
       (select count(*) from t_sc where cid = sc.cid)*100,2)||'%' jgl
  from t_sc sc
 group by sc.cid
 order by avgscore, jgl desc;
 /*
  SELECT t.C# AS 课程号,max(course.Cname)AS 课程名,isnull(AVG(score),0) AS平均成绩
        ,100 * SUM(CASE WHEN  isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数
    FROM SC T,Course
    where t.C#=course.C#
    GROUP BY t.C#
    ORDER BY 100* SUM(CASE WHEN  isnull(score,0)>=60 THEN 1 ELSE 0 END)/COUNT(*) DESC
    */

–20、查询如下课程平均成绩和及格率的百分数(用"1行"显示):
–企业管理(001),马克思(002),OO&UML (003),数据库(004)

select * from t_sc sc group by sc.cid

select sc.cid, (select name from t_course where cid=sc.cid) cname,
       case sc.cid when 1 then round(avg(sc.score),2) end chinese_avgscore,
       case sc.cid when 1 then
       round((select count(*) from t_sc where cid = sc.cid and score>=70)/
       (select count(*) from t_sc where cid = sc.cid)*100,2)||'%' end chinese_jgl       
  from t_sc sc
 group by sc.cid

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

select sc.cid,
       round(avg(sc.score),2) avg_score,
       c.name,
       t.tname
  from t_sc sc, t_course c, t_teacher t
  where sc.cid=c.cid and t.tid=c.teacherid
 group by sc.cid, c.name, t.tname
 order by avg_score desc;

–22、查询如下语文成绩第 2 名到第 5 名的学生成绩单:
–[学生ID],[学生姓名],语文成绩, 排名

select * from (
select t.*, rownum tn from (
select s.stuid, s.name,
       min(case c.name when 'Chinese' then sc.score end) chinese_score
  from t_student s, t_sc sc, t_course c
 where s.stuid = sc.stuid
   and sc.cid = c.cid
  group by s.stuid, s.name
  order by chinese_score desc) t where rownum <=5) where tn>1

–23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

select sc.cid, c.name,
(select count(*) from t_sc where cid=sc.cid and score>=85 and score<=100) a,
(select count(*) from t_sc where cid=sc.cid and score>=70 and score<85) b,
(select count(*) from t_sc where cid=sc.cid and score>=60 and score<70) c,
(select count(*) from t_sc where cid=sc.cid and score<60) d
  from t_sc sc, t_course c
 where sc.cid = c.cid
 group by sc.cid, c.name;
 /*
 SELECT SC.Cid as 课程ID, c.name as 课程名称 
        ,SUM(CASE WHEN sc.score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS a
        ,SUM(CASE WHEN sc.score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS b
        ,SUM(CASE WHEN sc.score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS c
        ,SUM(CASE WHEN sc.score < 60 THEN 1 ELSE 0 END) AS d
    FROM t_SC sc,t_Course c
    where SC.Cid=C.Cid 
    GROUP BY SC.Cid,c.name; 
*/

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

SELECT
	t.*, rownum rn
FROM
	(
		SELECT
			s.stuid,
			s. NAME,
			avg(sc.score) avg_score
		FROM
			t_student s,
			t_sc sc
		WHERE
			s.stuid = sc.stuid
		GROUP BY
			s.stuid,
			s. NAME
		ORDER BY
			avg_score DESC
	) t;

SELECT
	t.*, row_number () over (ORDER BY avg_score DESC) pm
FROM
	(
		SELECT
			s.stuid,
			s. NAME,
			avg(sc.score) avg_score
		FROM
			t_student s,
			t_sc sc
		WHERE
			s.stuid = sc.stuid
		GROUP BY
			s.stuid,
			s. NAME
	) t;

–25、查询各科成绩前三名的记录:(不考虑成绩并列情况)
SELECT
*
FROM
(
SELECT
sc.cid,
c. NAME,
sc.score,
row_number () over (
PARTITION BY sc.cid,
c. NAME
ORDER BY
sc.score DESC
) pm
FROM
t_sc sc,
t_course c
WHERE
sc.cid = c.cid
)
WHERE
pm <= 3;

/*
SELECT t1.S# as 学生ID,t1.C# as 课程ID,Score as 分数
FROM SC t1
WHERE score IN (SELECT TOP 3 score
FROM SC
WHERE t1.C#= C#
ORDER BY score DESC
)
ORDER BY t1.C#;
*/

–26、查询每门课程被选修的学生数
SELECT
cid,
count(*) rs,
(
SELECT
NAME
FROM
t_course
WHERE
cid = t_sc.cid
) cname
FROM
t_sc
GROUP BY
cid;

–27、查询出只选修了3门课程的全部学生的学号和姓名
SELECT
sc.stuid,
s. NAME
FROM
t_sc sc,
t_student s
WHERE
sc.stuid = s.stuid
GROUP BY
sc.stuid,
s. NAME
HAVING
count(*) = 3;

–28、查询男生、女生人数
SELECT
CASE
WHEN sex = ‘1’ THEN
‘Man’
ELSE
‘Woman’
END sex,
count(*) rs
FROM
t_student
GROUP BY
sex;

–29、查询姓“张”的学生名单
select * from t_student where name like ‘w%’;

–30、查询同名同性学生名单,并统计同名人数
select name, count() rs from t_student group by name having count()>1;

–31、1981年出生的学生名单(注:Student表中Sage列的类型是datetime)
select to_char(sysdate,‘yyyy’) year from dual;

–32、查询每门课程的平均成绩,结果按平均成绩升序排列,平均成绩相同时,按课程号降序排列
select cid, round(avg(score),2) avg_score from t_sc group by cid order by avg_score, cid desc;

–33、查询平均成绩大于85的所有学生的学号、姓名和平均成绩
SELECT
s.stuid,
s. NAME,
round(avg(sc.score), 2) avg_score
FROM
t_student s,
t_sc sc
WHERE
s.stuid = sc.stuid
GROUP BY
s.stuid,
s. NAME
HAVING
avg(sc.score) > 80;

–34、查询课程名称为“数据库”,且分数低于60的学生姓名和分数
SELECT
s.stuid,
s. NAME stuname,
sc.score
FROM
t_student s,
t_sc sc,
t_course c
WHERE
s.stuid = sc.stuid
AND sc.cid = c.cid
AND c. NAME = ‘Computer’
AND sc.score < 70;

–35、查询所有学生的选课情况
SELECT
s.stuid,
s. NAME stuname,
c.cid,
c. NAME coursename
FROM
t_student s,
t_sc sc,
t_course c
WHERE
s.stuid = sc.stuid
AND sc.cid = c.cid;

–36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数
SELECT
s.stuid,
s. NAME stuname,
c.cid,
c. NAME coursename,
sc.score
FROM
t_student s,
t_sc sc,
t_course c
WHERE
s.stuid = sc.stuid
AND sc.cid = c.cid
AND sc.score > 70;

–37、查询不及格的课程,并按课程号从大到小排列
SELECT
c.cid,
c. NAME
FROM
t_sc sc,
t_course c
WHERE
sc.cid = c.cid
GROUP BY
c.cid,
c. NAME,
sc.score
HAVING
sc.score < 70
ORDER BY
c.cid DESC;

–38、查询课程编号为003且课程成绩在80分以上的学生的学号和姓名;
SELECT
s.stuid,
s. NAME,
sc.score
FROM
t_student s,
t_sc sc
WHERE
s.stuid = sc.stuid
AND sc.cid = 3
AND sc.score > 80;

–39、求选了课程的学生人数
select count(*) rs from (select stuid from t_sc group by stuid);

–40、查询选修“叶平”老师所授课程的学生中,成绩最高的学生姓名及其成绩
SELECT
s.stuid,
s. NAME stuname,
t.max_score
FROM
t_student s,
t_sc sc,
(
SELECT
c.cid,
max(sc.score) max_score
FROM
t_sc sc,
t_course c,
t_teacher t
WHERE
sc.cid = c.cid
AND c.teacherid = t.tid
AND t.tname = ‘Mr.mao’
GROUP BY
c.cid
) t
WHERE
s.stuid = sc.stuid
AND sc.cid = t.cid
AND sc.score = t.max_score;
/*
select S.name stuname, sc.score
from t_Student s, t_SC sc, t_Course C, t_Teacher t
where S.Stuid = SC.Stuid
and SC.Cid = C.Cid
and C.Teacherid = T.tid
and T.Tname = ‘Mr.mao’
and SC.score = (select max(score) from t_SC where Cid = C.Cid);*/

–41、查询各个课程及相应的选修人数
select cid, count(*) rs from t_sc group by cid order by cid;

–42、查询不同课程成绩相同的学生的学号、课程号、学生成绩
SELECT
*
FROM
t_sc sc
WHERE
EXISTS (
SELECT
*
FROM
t_sc
WHERE
stuid != sc.stuid
AND cid != sc.cid
AND score = sc.score
);

SELECT DISTINCT
A.Stuid,
B.score
FROM
t_SC A,
t_SC B
WHERE
A.Score = B.Score
AND A.Cid <> B.Cid;

–43、查询每门功成绩最好的前两名
SELECT
*
FROM
(
SELECT
stuid,
cid,
score,
row_number () over (
PARTITION BY cid
ORDER BY
score DESC
) pm
FROM
t_sc
)
WHERE
pm < 3;
/*
SELECT t1.S# as 学生ID, t1.C# as 课程ID, Score as 分数
FROM SC t1
WHERE score IN
(SELECT TOP 2 score FROM SC WHERE t1.C# = C# ORDER BY score DESC)
ORDER BY t1.C#;
*/

–44、统计每门课程的学生选修人数(超过10人的课程才统计)。要求输出课程号和选修人数,
–查询结果按人数降序排列,查询结果按人数降序排列,若人数相同,按课程号升序排列
select cid, count() rs from t_sc group by cid having count() > 5 order by rs desc, cid;

–45、检索至少选修两门课程的学生学号
select stuid ,count() from t_sc group by stuid having count() > 2;

–46、查询全部学生都选修的课程的课程号和课程名
SELECT
c.cid,
c. NAME
FROM
t_course c
WHERE
EXISTS (
SELECT
cid,
count() rs
FROM
t_sc
WHERE
cid = c.cid
GROUP BY
cid
HAVING
count(
) = (
SELECT
count(*)
FROM
t_student
)
);

–47、查询没学过“叶平”老师讲授的任一门课程的学生姓名
SELECT
*
FROM
t_student
WHERE
stuid NOT IN (
SELECT
s.stuid
FROM
t_student s,
t_sc sc
WHERE
s.stuid = sc.stuid
AND sc.cid IN (
SELECT
cid
FROM
t_course c,
t_teacher t
WHERE
c.teacherid = t.tid
AND t.tname = ‘Mr.zhu’
)
);

SELECT
*
FROM
t_Student s
WHERE
s.stuid NOT IN (
SELECT
sc.stuid
FROM
t_Course c,
t_Teacher t,
t_SC sc
WHERE
c.teacherid = T.Tid
AND SC.Cid = c.Cid
AND t.Tname = ‘Mr.zhu’
);

–48、查询1门以上不及格课程的同学的学号及其平均成绩
SELECT
stuid,
avg(score) avg_score
FROM
t_sc
GROUP BY
stuid
HAVING
stuid IN (
SELECT
stuid
FROM
t_sc
WHERE
score < 70
GROUP BY
stuid
HAVING
count(*) > 1
);

–49、检索“5”课程分数小于60,按分数降序排列的同学学号
select stuid from t_sc where cid=5 and score<70 order by score desc;

–50、删除“002”同学的“001”课程的成绩
delete from t_sc where stuid=2 and cid=1;

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值