以下为某公司的数据库笔试题及个人整理的答案,小伙伴们有更好的答案欢迎指教!
Student(S#,Sname) 10000条记录
Cource(C#,Cname,T#) 9条记录
SC(S#,C#,Score) 89991条记录
Teacher(T#,Tname) 3条记录
题目并没有指定表中有多少数据,记录是我自己模拟的。
1、选择课程001的成绩大于课程002的成绩的学生ID;
SELECT sc1.`S#` from sc sc1 WHERE sc1.`C#`=1 AND EXISTS (SELECT sc1.`S#` from sc sc2 WHERE sc2.`C#`=2 and sc1.`S#`=sc2.`S#` AND sc1.Score > sc2.Score ) 上面的用法性能太差了。 [SQL] SELECT sc1.`S#` from sc sc1, sc sc2 WHERE sc1.`S#`=sc2.`S#` and sc1.`C#`=1 AND sc2.`C#`=2 AND sc1.Score > sc2.Score 时间: 14.913ms2、查询所有平均成绩大于60分的学生ID,学生姓名;
select s.`S#`,Sname,avg(sc.Score) avgc from Student s,SC sc where s.`S#`=sc.`S#` GROUP BY sc.`S#` HAVING(avgc > 60) [SQL] select s.id,Sname,avg(sc.Score) avgc from Student1 s,SC sc where s.`id`=sc.`S#` GROUP BY sc.`S#` HAVING(avgc > 60) 时间: 0.227ms3、查询所有学生的学生ID,学生姓名,总科目数,总成绩;
select s.`S#`, s.Sname, count(*),sum(sc.Score) from Student s ,SC sc where s.`S#`=sc.`S#` GROUP BY sc.`S#` [SQL] select s.`id`, s.Sname, count(*),sum(sc.Score) from Student1 s ,SC sc where s.`id`=sc.`S#` GROUP BY sc.`S#` 时间: 0.264ms4、查询没有上过“志明”老师课的学生ID,学生姓名;
select * from Student where `S#` NOT in (select `S#` from SC where `C#` in (select `C#` from Cource where `T#` in ( select `T#` from Teacher where Tname LIKE '%志明%' ))) [SQL] select * from Student1 where `id` NOT in (select `S#` from SC where `C#` in (select `C#` from Cource where `T#` in ( select `T#` from Teacher where Tname LIKE '%志明%' ))) 时间: 0.156ms5、母表A中有3000万条数据(分库分表了),子表B中有5万条数据,现在要更新A表中的某个字段值为B表的某个字段值,条件是A表的某字段值与B表的某字段值匹配;如何实现,如何优化效率?
update A set A.a=B.a from A ,B where A.id=B.id具体怎么优化,能想到的就是,where 字段加索引,
操作系统:Windows 7 64位
CPU:I3 2.4G
内存:6G
MySQL:5.6.19 InnoDB
===========文档信息============
版权声明:非商用自由转载-保持署名-注明出处
署名(BY) :testcs_dn(微wx笑)
文章出处:[无知人生,记录点滴](http://blog.csdn.net/testcs_dn)