Hi,大家好,我是编程小6,很荣幸遇见你,我把这些年在开发过程中遇到的问题或想法写出来,今天说一说
sql数据库面试必会6题经典_sql笔试题及答案,希望能够帮助你!!!。
Student(Sid,Sname,Sage,Ssex)学生表 Sid:学号 Sname:学生姓名 Sage:学生年龄 Ssex:学生性别 Course(Cid,Cname,T#)课程表 Cid:课程编号 Cname:课程名称 Tid:教师编号 SC(Sid,Cid,score)成绩表 Sid:学号 Cid:课程编号 score:成绩 Teacher(Tid,Tname)教师表 Tid:教师编号: Tname:教师名字
问题:
select a.sid from (select sid,score from sc where cid='001')a, (select sid,score from sc where cid='002')b where a.sid = b.sid and a.score>b.score;
select sid,avg(score) from sc group by sid having avg(score)>60;
select s.sid,s.sname,count_cid as 选课数, sum_score as 总成绩 from student s left join (select sid,count(cid) as count_cid,sum(score) as sum_score from sc group by sid )sc on s.sid = sc.sid;
select count(tname) from teacher where tname like '李%';
select s.sid,s.sname from student as s where s.sid not in ( select DISTINCT sid from sc as sc where sc.cid in ( select cid from course as c left join teacher as t on c.tid = t.tid where t.tname = '叶平') );
select s.sid,s.sname from student as s where s.sid in ( select distinct sc.sid from sc as sc where sc.cid in ( select cid from course as c left join teacher as t on c.tid = t.tid where t.tname = '叶平') group by sc.sid HAVING count(cid)= (select count(cid) from course as c left join teacher as t on c.tid = t.tid where t.tname = '叶平') );
SELECT s.sid,s.sname from student as s left join sc as sc on s.sid = sc.sid where sc.cid = '001' and EXISTS( select * from sc as sc_2 where sc.sid = sc_2.sid and sc_2.cid='002'); select s.sid,s.sname from student as s left join sc as sc on sc.sid = s.sid where sc.cid = '001' and s.sid in ( select sid from sc as sc_2 where sc_2.cid='002' and sc_2.sid = sc.sid);
select sid,sname from (select student.sid,student.sname,score, (select score from sc as sc_2 where sc_2.sid = student.sid and sc_2.cid = '002') as score2 from student,sc where student.sid=sc.sid and cid = '001') s_2 where score2<score;
select sid,sname from student where sid not in (select s.sid from student s,sc where s.sid=sc.sid and score>60 ); select sid,sname from student s where not EXISTS ( select s.sid from sc where sc.sid = s.sid and sc.score>60);
select s.sid,s.sname from student s ,sc sc where s.sid = sc.sid group by s.sid,s.sname having count(sc.cid)<( select count(cid) from course); select s.sid,s.sname from student s right join sc sc on s.sid = sc.sid group by s.sid,s.sname having count(sc.cid)< (select count(cid) from course);
select student.sid,sname from student,sc where student.sid = sc.sid and cid in (select cid from sc where sid='1001'); select s.sid,s.sname from sc sc left join student as s on sc.sid = s.sid where sc.cid in (select cid from sc where sid='1001'); select sc_1.sid,s.sname from sc sc_1 left join student as s on sc_1.sid = s.sid where exists (select sc_2.cid from sc as sc_2 where sc_1.cid = sc_2.cid and sc_2.sid = '1001');
update sc set score = (select avg(sc_2.score) from sc sc_2 where sc_2.cid = sc.cid) where cid in (select c.cid from course c left join teacher t on t.tid = c.tid where t.tname = '叶平');
select sc_1.sid from (select cid from sc where sid='1002')a left join sc sc_1 on a.cid = sc_1.cid where sc_1.sid<>'1002' group by sc_1.sid having count(sc_1.cid) = (select count(cid) from sc where sid='1002'); select a.sid,s.sname from (select sid,GROUP_CONCAT(cid order by cid separator ',') as cid_str from sc where sid='1002')b, (select sid,GROUP_CONCAT(cid order by cid separator ',') as cid_str from sc group by sid)a left join student s on a.sid = s.sid where a.cid_str = b.cid_str and a.sid<>'1002';
delete from sc WHERE cid in ( select c.cid from course c LEFT JOIN teacher t on c.tid=t.tid where t.tname = '叶平');
insert into sc select sid,'002', (select avg(score) from sc where cid='0022') from student where sid not in (select sid from sc where cid='002');
select sid as 学生id, (SELECT score from sc where sc.sid = t.sid and cid='004') as 数据库, (select score from sc where sc.sid = t.sid and cid='001') as 企业管理, (select score from sc where sc.sid = t.sid and cid='015') as 英语, count(cid) as 有效课程数, avg(t.score) as 平均成绩 from sc as t group by sid order by avg(t.score);
select l.cid as 课程id,l.score as 最高分, r.score as 最低分 from sc l,sc r where l.cid = r.cid and l.score = (select max(t.score) from sc t where l.cid = t.cid group by t.cid) and r.score = (select min(t.score) from sc t where r.cid = t.cid group by t.cid) order by l.cid; select cid as 课程id,max(score) as 最高分, min(score) as 最低分 from sc group by cid;
SELECT t.cid as 课程号, c.cname as 课程名, COALESCE(avg(score),0) as 平均成绩, 100*sum(case when COALESCE(score,0)>=60 then 1 else 0 END)/count(*) as 及格百分数 from sc t left join course c on t.cid = c.cid group by t.cid order by 100*sum(case when COALESCE(score,0)>=60 then 1 else 0 END)/count(*);
select t.tid as 教师id, t.tname as 教师姓名, sc.cid as 课程id, avg(score) as 平均成绩 from sc as sc LEFT JOIN course c on sc.cid = c.cid left join teacher t on c.tid = t.tid group by sc.cid order by avg(sc.score) desc;
select sc.cid as 课程id,cname as 课程名称, sum(case when score between 85 and 100 then 1 else 0 end) as '[100-85]', sum(case when score between 70 and 85 then 1 else 0 end) as '[85-70]', sum(case when score between 60 and 70 then 1 else 0 end) as '[70-60]', sum(case when score<60 then 1 else 0 end) as '[60-0]' from sc as sc left join course as c on sc.cid = c.cid group by sc.cid;
select 1+(select count(distinct 平均成绩) from (select sid,avg(score) as 平均成绩 from sc group by sid)t1 where 平均成绩>t2.平均成绩) as 名次, sid as 学生学号,平均成绩 from (select sid,avg(score) 平均成绩 from sc group by sid) as t2 order by 平均成绩 desc;
select sid,cid,score from sc sc_1 where ( select count(3) from sc sc_2 where sc_1.cid = sc_2.cid and sc_2.score>=sc_1.score)<=2 order by sc_1.cid );
select cid, count(sid) from sc group by cid;
select sc.sid,s.sname, count(sc.cid) as 课程数 from sc as sc LEFT JOIN student as s on sc.sid = s.sid group by sc.sid having count(sc.cid)=1;
select count(ssex) as 男生人数 from student group by ssex having ssex = '男'; select count(2) from student where ssex = '女';
select sid,sname from student where sname like '张%';
select sname,count(8) from student group by sname having count(8)>1;
select s.sname,sc.sid,avg(sc.score) as 平均成绩 from sc as sc left join student as s on sc.sid = s.sid group by sc.sid having avg(sc.score)>85;
select cid,avg(score) from sc group by cid order by avg(score),cid desc;
select c.cname,s.sid,s.sname,sc.score from course c left join sc on sc.cid = c.cid LEFT JOIN student s on s.sid = sc.sid where c.cname = '数据库' and sc.score<60;
select sc.sid,sc.cid,s.sname,c.cname from sc LEFT JOIN course c on sc.cid = c.cid left join student s on sc.sid = s.sid;
select distinct s.sid,s.sname,sc.cid,sc.score from sc left join student s on sc.sid = s.sid left join course c on sc.cid = c.cid where sc.score>70;
select cid from sc where score<60 ORDER BY cid;
select sc.sid,s.sname from sc left join student s on sc.sid = s.sid where sc.cid = '003' and sc.score>80;
select count(2) from (select distinct sid from sc)a;
select s.sname,sc.score from sc sc left join student s on sc.sid = s.sid left join course c on sc.cid = c.cid left join teacher t on c.tid = t.tid where t.tname = '叶平' and sc.score = ( select max(score) from sc sc_1 where sc.cid = sc_1.cid);
select cid,count(*) from sc group by cid;
select DISTINCT a.sid,a.cid,a.score from sc as a ,sc as b where a.score = b.score and a.cid <> b.cid;
select cid as 课程号,count(8) as 选修人数 from sc group by cid HAVING count(sid)>10 order by count(8) desc,cid;
select sid from sc group by sid having count(8)>=2;
select cid,cname from course where cid in (select cid from sc group by cid);
select sname from student where sid not in ( select sid from sc,course,teacher where course.tid = teacher.tid and sc.cid = course.cid and teacher.tname='叶平' );
select sid,avg(COALESCE(score,0)) from sc where sid in ( select sid from sc where score<60 group by sid having count(8)>2 ) group by sid;
select sid,score from sc where cid='004' and score<60 order by score desc;
delete from sc where sid = '002' and cid = '001';
今天的分享到此就结束了,感谢您的阅读,如果确实帮到您,您可以动动手指转发给其他人。