[PS] 本人从网上找到的一些面试sql题目,如有不足之处,敬请谅解,可以先拷贝建表语句sql来进行建表,再尝试做一下题目。
建表sql语句:
create table student(
sno varchar(10) primary key,
sname varchar(20),
sage int(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 double(4,2),
constraint pk_sc primary key (sno,cno)
);
#/*******初始化学生表的数据******/
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);
相关sql代码练习题目及其答案如下:
#1、查询“c001”课程比“c002”课程成绩高的所有学生的学号;
select a.sno from sc a,sc b WHERE a.sno=b.sno
and a.cno='c001' and b.cno='c002'
and a.score>b.score
#2、查询平均成绩大于60 分的同学的学号,姓名和平均成绩;
select student.sno,student.sname,avg from student RIGHT JOIN (select sc.sno,AVG(sc.score) as avg from sc GROUP BY sc.sno HAVING
AVG(sc.score) >60 )as a
on student.sno=a.sno
#3、查询所有同学的学号、姓名、选课数、总成绩;
select st.sno,st.sname,a.countsum,scoresum from student st JOIN (select sc.sno,count(sc.cno) as countsum,SUM(sc.score) as scoresum from sc GROUP BY sc.sno)as a on st.sno=a.sno
#4、查询姓“刘”的老师的个数;
select COUNT(*) from teacher te WHERE te.tname like '刘%'
#5、查询没学过“谌燕”老师课的同学的学号、姓名;
select student.sno,student.sname from sc JOIN student on sc.sno=student.sno
where
sc.cno not in (
select course.cno from teacher te JOIN course
on te.tno=course.tno
WHERE te.tname like '%谌燕%'
)
#6、查询学过“c001”并且也学过编号“c002”课程的同学的学号、姓名;
select student.sno,student.sname from student JOIN (
select sno from sc WHERE sc.cno='c002'and sno in(
select sno from sc WHERE sc.cno='c001'
)
)as a on student.sno=a.sno
#7、查询学过“谌燕”老师所教的所有课的同学的学号、姓名;
select student.sno,student.sname from sc JOIN student on sc.sno=student.sno
where
sc.cno in (
select course.cno from teacher te JOIN course
on te.tno=course.tno
WHERE te.tname like '%谌燕%'
)GROUP BY student.sno
#8、查询所有课程成绩小于60 分的同学的学号、姓名;
SELECT course.cname,student.sno,student.sname from course RIGHT JOIN sc on course.cno=sc.cno
JOIN student on sc.sno=student.sno
WHERE score<60
#9、查询没有学全所有课的同学的学号、姓名;
SELECT sc.sno,student.sname,cnosum from sc RIGHT JOIN student on sc.sno=student.sno
JOIN (select sc.sno,count(sc.cno) cnosum from sc GROUP BY sc.sno)as a
on sc.sno=a.sno
WHERE cnosum<(SELECT count(course.cno) cns from course)
GROUP BY sno
#10、查询至少有一门课与学号为“s001”的同学所学相同的同学的学号和姓名;
select DISTINCT sc.sno,sname from sc LEFT JOIN student on sc.sno=student.sno
WHERE sc.cno in (
select DISTINCT cno from sc WHERE sc.sno='s001'
)and sc.sno <>'s001'
#11、把“SC”表中“谌燕”老师教的课的成绩都更改为此课程的平均成绩;
update sc,(select sc.*,a.avgscore from sc JOIN
(SELECT sc.cno,ROUND(AVG(sc.score),2) avgscore from sc WHERE sc.cno in(
SELECT cno FROM teacher te JOIN course co ON ( te.tno = co.tno ) WHERE te.tname = '谌燕'
)GROUP BY sc.cno )as a
on sc.cno=a.cno)as b
set sc.score=b.avgscore
where sc.cno=b.cno
#14、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分
select sc.cno,MAX(sc.score),MIN(sc.score) from sc GROUP BY sc.cno
#15、按各科 平均成绩 从 低到高 和 及格率的百分数 从高到低顺序
select sc.cno,AVG(sc.score)as avg FROM sc GROUP BY sc.cno ORDER BY avg
select b.bpass/a.count as jigelv from (select sc.cno,count(sc.cno)as count from sc GROUP BY sc.cno)as a LEFT JOIN (select sc.cno,COUNT(sc.cno)as bpass from sc WHERE sc.score>60 GROUP BY sc.cno)as b
on a.cno=b.cno ORDER BY jigelv desc
#16、查询每门课程被选修的学生数
select sc.cno,COUNT(sc.score) from sc GROUP BY sc.cno
#17、查询出只选修了一门课程的全部学生的学号和姓名
select student.sno,student.sname from sc JOIN student on sc.sno=student.sno
WHERE sc.cno=(select sc.cno from sc GROUP BY sc.cno HAVING COUNT(sc.score)=1)
#18、查询男生、女生人数
select student.ssex,COUNT(ssex) from student GROUP BY ssex
#19、查询姓“张”的学生名单
SELECT * from student WHERE student.sname like '张%'
#20、查询平均成绩大于85 的所有学生的学号、姓名和平均成绩
select student.sno,sname,avg from student RIGHT JOIN (select sc.sno,avg(sc.score)as avg from sc GROUP BY sc.sno HAVING avg>85)as a
on a.sno=student.sno
#21、求选了课程的学生人数
select COUNT(*) numcount from (
select DISTINCT sno from sc
)as a
#21、查询选修“谌燕”老师所授课程的学生中,成绩最高的学生姓名及其成绩
select a.cno,student.sname,sc.score from sc LEFT JOIN student on sc.sno=student.sno
LEFT JOIN (SELECT
cno,
MAX(score)as maxscore
FROM
sc
WHERE
cno IN ( SELECT cno FROM course WHERE tno = ( SELECT tno FROM teacher WHERE teacher.tname = '谌燕' ) )
GROUP BY cno )as a on a.cno=sc.cno
WHERE score=maxscore
#22、检索“c004”课程分数小于60,按分数降序排列的同学学号
select sno from sc WHERE sc.score<60 and sc.cno='c004'