经典SQL面试题.doc
《经典SQL面试题.doc》由会员分享,可在线阅读,更多相关《经典SQL面试题.doc(14页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、如有侵权,请联系网站删除,仅供学习与交流经典SQL面试题【精品文档】第 14 页表Student(Sid,Sname,Sage,Ssex) 学生表 CREATE TABLE student ( sid varchar(10) NOT NULL, sName varchar(20) DEFAULT NULL, sAge datetime DEFAULT 1980-10-12 23:12:36, sSex varchar(10) DEFAULT NULL, PRIMARY KEY (sid) ENGINE=InnoDB DEFAULT CHARSET=utf8;Course(Cid,Cname,T
2、id) 课程表 CREATE TABLE course ( cid varchar(10) NOT NULL, cName varchar(10) DEFAULT NULL, tid int(20) DEFAULT NULL, PRIMARY KEY (cid) ENGINE=InnoDB DEFAULT CHARSET=utf8;SC(Sid,Cid,score) 成绩表 CREATE TABLE sc ( sid varchar(10) DEFAULT NULL, cid varchar(10) DEFAULT NULL, score int(10) DEFAULT NULL) ENGIN
3、E=InnoDB DEFAULT CHARSET=utf8;Teacher(Tid,Tname) 教师表 CREATE TABLE taacher ( tid int(10) DEFAULT NULL, tName varchar(10) DEFAULT NULL) ENGINE=InnoDB DEFAULT CHARSET=utf8;数据:(MySQL)insert into taacher(tid,tName) values (1,李老师),(2,何以琛),(3,叶平);insert into student(sid,sName,sAge,sSex) values (1001,张三丰,19
4、80-10-12 23:12:36,男),(1002,张无极,1995-10-12 23:12:36,男),(1003,李奎,1992-10-12 23:12:36,女),(1004,李元宝,1980-10-12 23:12:36,女),(1005,李世明,1981-10-12 23:12:36,男),(1006,赵六,1986-10-12 23:12:36,男),(1007,田七,1981-10-12 23:12:36,女);insert into sc(sid,cid,score) values (1,001,80),(1,002,60),(1,003,75),(2,001,85),(2,
5、002,70),(3,004,100),(3,001,90),(3,002,55),(4,002,65),(4,003,60);insert into course(cid,cName,tid) values (001,企业管理,3),(002,马克思,3),(003,UML,2),(004,数据库,1),(005,英语,1);问题: 1、查询“001”课程比“002”课程成绩高的所有学生的学号; select a.Sid from (select sid,score from SC where Cid=001) a,(select sid,score from SC where Cid=00
6、2) b where a.scoreb.score and a.sid=b.sid; 2、查询平均成绩大于60分的同学的学号和平均成绩; select Sid,avg(score) from sc group by Sid having avg(score) 60; 3、查询所有同学的学号、姓名、选课数、总成绩; select Student.Sid,Student.Sname,count(SC.Cid),sum(score) from Student left Outer join SC on Student.Sid=SC.Sid group by Student.Sid,Sname 4、查
7、询姓“李”的老师的个数; select count(distinct(Tname) from Teacher where Tname like 李%; 5、查询没学过“叶平”老师课的同学的学号、姓名; select Student.Sid,Student.Sname from Student where Sid not in (select distinct( SC.Sid) from SC,Course,Teacher where SC.Cid=Course.Cid and Teacher.Tid=Course.Tid and Teacher.Tname=叶平); 6、查询学过“001”并且
8、也学过编号“002”课程的同学的学号、姓名; select Student.Sid,Student.Sname from Student,SC where Student.Sid=SC.Sid and SC.Cid=001and exists( Select * from SC as SC_2 where SC_2.Sid=SC.Sid and SC_2.Cid=002); 7、查询学过“叶平”老师所教的所有课的同学的学号、姓名; select Sid,Sname from Student where Sid in (select Sid from SC ,Course ,Teacher wh
9、ere SC.Cid=Course.Cid and Teacher.Tid=Course.Tid and Teacher.Tname=叶平 group by Sid having count(SC.Cid)=(select count(Cid) from Course,Teacher where Teacher.Tid=Course.Tid and Tname=叶平); 8、查询课程编号“002”的成绩比课程编号“001”课程低的所有同学的学号、姓名; Select Sid,Sname from (select Student.Sid,Student.Sname,score ,(select
10、score from SC SC_2 where SC_2.Sid=Student.Sid and SC_2.Cid=002) score2 from Student,SC where Student.Sid=SC.Sid and Cid=001) S_2 where score2 60); 10、查询没有学全所有课的同学的学号、姓名; select Student.Sid,Student.Sname from Student,SC where Student.Sid=SC.Sid group by Student.Sid,Student.Sname having count(Cid) =60
11、 THEN 1 ELSE 0 END)/COUNT(*) AS 及格百分数 FROM SC T,Course where t.Cid=course.Cid GROUP BY t.Cid 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 SUM(CASE WHEN Cid =001 THEN score ELSE 0 END)/SU
12、M(CASE Cid WHEN 001 THEN 1 ELSE 0 END) AS 企业管理平均分 ,100 * SUM(CASE WHEN Cid = 001 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN Cid = 001 THEN 1 ELSE 0 END) AS 企业管理及格百分数 ,SUM(CASE WHEN Cid = 002 THEN score ELSE 0 END)/SUM(CASE Cid WHEN 002 THEN 1 ELSE 0 END) AS 马克思平均分 ,100 * SUM(CASE WHEN Cid = 002
13、 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN Cid = 002 THEN 1 ELSE 0 END) AS 马克思及格百分数 ,SUM(CASE WHEN Cid = 003 THEN score ELSE 0 END)/SUM(CASE Cid WHEN 003 THEN 1 ELSE 0 END) AS UML平均分 ,100 * SUM(CASE WHEN Cid = 003 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN Cid = 003 THEN 1 ELSE 0 END) AS
14、UML及格百分数 ,SUM(CASE WHEN Cid = 004 THEN score ELSE 0 END)/SUM(CASE Cid WHEN 004 THEN 1 ELSE 0 END) AS 数据库平均分 ,100 * SUM(CASE WHEN Cid = 004 AND score = 60 THEN 1 ELSE 0 END)/SUM(CASE WHEN Cid = 004 THEN 1 ELSE 0 END) AS 数据库及格百分数 FROM SC 21、查询不同老师所教不同课程平均分从高到低显示 SELECT max(Z.Tid) AS 教师ID,MAX(Z.Tname)
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- 经典 SQL 试题
限制150内