SQL 多表查询例题

准备工作

1、 创建数据表

CREATE TABLE student( 
   sno VARCHAR(10)  PRIMARY KEY,  
   sname VARCHAR(20),
   sage VARCHAR(2),
   ssex VARCHAR(5) 
);  
CREATE TABLE teacher(  
   tno VARCHAR(10) PRIMARY KEY,  
   tname VARCHAR(20)  
);  
CREATE TABLE course(  
   cno VARCHAR(10),  
   cname VARCHAR(20),  
   tno VARCHAR(20),  
   CONSTRAINT pk_course PRIMARY KEY (cno,tno)  
);  
CREATE TABLE sc(  
   sno VARCHAR(10),  
   cno VARCHAR(10),  
   score NUMERIC(4,2),  
   CONSTRAINT pk_sc PRIMARY KEY (sno,cno)  
);

2、 插入数据

/*******初始化学生表的数据******/  
insert into student values ('s001','张三',23,'男');  
insert into student values ('s002','李四',23,'男');  
insert into student values ('s003','吴鹏',25,'男');  
insert into student values ('s004','琴沁',20,'女');  
insert into student values ('s005','王丽',20,'女');  
insert into student values ('s006','李波',21,'男');  
insert into student values ('s007','刘玉',21,'男');  
insert into student values ('s008','萧蓉',21,'女');  
insert into student values ('s009','陈萧晓',23,'女');  
insert into student values ('s010','陈美',22,'女');  
commit;  
/******************初始化教师表***********************/  
insert into teacher values ('t001', '刘阳');  
insert into teacher values ('t002', '谌燕');  
insert into teacher values ('t003', '胡明星');  
commit;  
/***************初始化课程表****************************/  
insert into course values ('c001','J2SE','t002');  
insert into course values ('c002','Java Web','t002');  
insert into course values ('c003','SSH','t001');  
insert into course values ('c004','Oracle','t001');  
insert into course values ('c005','SQL SERVER 2005','t003');  
insert into course values ('c006','C#','t003');  
insert into course values ('c007','JavaScript','t002');  
insert into course values ('c008','DIV+CSS','t001');  
insert into course values ('c009','PHP','t003');  
insert into course values ('c010','EJB3.0','t002');  
commit;  
/***************初始化成绩表***********************/  
insert into sc values ('s001','c001',78.9);  
insert into sc values ('s002','c001',80.9);  
insert into sc values ('s003','c001',81.9);  
insert into sc values ('s004','c001',60.9);  
insert into sc values ('s001','c002',82.9);  
insert into sc values ('s002','c002',72.9);  
insert into sc values ('s003','c002',81.9);  
insert into sc values ('s001','c003','59');  
commit;

结果显示:

  • 学生表
    SQL 多表查询例题

  • 教师表
    SQL 多表查询例题

  • 课程表
    SQL 多表查询例题

  • 分数表
    SQL 多表查询例题

案例测试

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

SELECT a.* ,b.score FROM  
		(SELECT * FROM sc a  WHERE a.`cno`='c001') a,
		(SELECT * FROM sc  b WHERE b.`cno`='c002') b
		WHERE a.sno = b.sno AND a.score>b.score	

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

SELECT sc.`sno`,AVG(sc.`score`) 
	FROM sc 
	GROUP BY sno HAVING AVG(sc.`score`)>60

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

SELECT b.*,a.`sname`
	FROM student a,
	(SELECT sc.`sno`,COUNT(sc.`cno`),SUM(sc.`score`) FROM sc GROUP BY sc.`sno`) b
	WHERE a.`sno`= b.sno

或者利用内联

SELECT a.*,COUNT(b.`cno`),SUM(b.`score`) FROM student a
INNER JOIN sc b ON a.`sno`=b.`sno`
GROUP BY b.`sno` 

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

SELECT COUNT(*) FROM teacher t WHERE t.`tname` LIKE '刘%' 

5、查询没学过“谌燕”老师课的同学的学号、姓名;

SELECT st.`sno`,st.`sname` FROM student st
	WHERE st.`sno` NOT IN
	(
	SELECT  DISTINCT a.sno 
		FROM sc a,
		(SELECT cs.`cno`,cs.`cname` FROM course cs WHERE cs.`tno`=(SELECT b.`tno` FROM teacher  b WHERE b.`tname`='谌燕')) b
		WHERE a.`cno`=b.cno
	)

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

  • 先找课程id与sid,然后再内联student名单,找到具体名字。
  • 代码如下:
    SELECT * FROM sc a 
    JOIN sc b ON a.`sno` =b.`sno`
    JOIN student stu ON a.`sno`=stu.`sno`
    WHERE a.cno='c001' AND b.cno='c002' 
    

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

SELECT d.`sno`,d.`sname`,b.`cname`,c.`tname` FROM sc a
LEFT JOIN course b ON a.`cno`=b.`cno`
LEFT JOIN teacher c ON b.`tno`=c.`tno`
LEFT JOIN student d ON a.`sno` = d.`sno`
WHERE c.`tname`='谌燕'

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

SELECT c.`sno`,c.`sname`,a.`score`,b.`score` FROM sc a
LEFT JOIN sc b ON a.`sno`=b.`sno`
LEFT JOIN student c ON a.`sno`=c.`sno`
WHERE a.`cno`='c001' AND b.`cno`='c002' AND a.`score`>b.`score`

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

SELECT a.`sname`,c.`cno`,b.`sno` FROM student a
INNER JOIN sc b ON b.`sno` = a.`sno`
LEFT JOIN course c ON c.`cno`=b.`cno`
WHERE b.`score`<60

(未完成)10、查询没有学全所有课的同学的学号、姓名;

SELECT a.`sno`,a.`sname`,COUNT(sc.`cno`) FROM student a
INNER JOIN  sc ON a.`sno`=sc.`sno`
 GROUP BY a.`sno` HAVING  COUNT(sc.`cno`)<(SELECT COUNT(cno) FROM course)

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

SELECT DISTINCT c.`sname`,c.`sno` FROM sc a
LEFT JOIN sc b ON a.`cno` = b.`cno`
LEFT JOIN student c ON a.`sno` = c.`sno`
WHERE b.`sno`='s001' AND NOT a.`sno`=b.`sno`  

12、查询至少学过学号为“s001”同学所有一门课的其他同学学号和姓名;

SELECT * FROM sc
 LEFT JOIN student st ON st.sno=sc.sno
 WHERE sc.sno<>'s001' 
 AND sc.cno IN (SELECT cno FROM sc WHERE sno='s001')

13、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;

update sc c set score=
 (
  select avg(c.score)  from course a,teacher b 
  where a.tno=b.tno and b.tname='谌燕' and a.cno=c.cno 
  group by c.cno
 )
where cno in( select cno from course a,teacher b where a.tno=b.tno and b.tname='谌燕')

14、查询学生id,姓名,每门课程的分数,

上一篇:SQL Server2019学习笔记--数据表的查询操作


下一篇:MySQL实验一:数据库的定义实验