子沫
面试中遇到的sql练习题
面试中面试官问你啥问题,小编都给你整理在这里了。每天都会整理一份最接地气的面试题,希望能帮助到你!
1.用一条SQL 语句查询出每门课都大于80 分的学生姓名?
// 第一种方式:
select name from table
where name
not in (
select name from table
where score< 80
);
// 第二种方式:
select name from table
group by name
having min(score) >= 80;
2.删除除了自动编号id不同, 其他都相同的学生冗余信息?
字段分别是:id,stunum,name,course,score
delete table
where id not in (
select min(id) from table
group by stunum,name,course,score
);
3.一个叫 team 的表,里面只有一个字段name, 一共有4 条纪录,分别是a,b,c,d, 对应四个球对,现在四个球对进行比赛,
用一条sql 语句显示所有可能的比赛组合?
// 所有组合:ab,ac,ad, bc,bd, cd 所有左侧字母都小于右侧字母
select a.name,b.name
from team a,team b
where a.name < b.name
order by a.name,b.name ;
4.从TestDB 数据表中查询出所有月份的发生额都比101 科目相应月份的发生额高的科目?
AccID :科目代码,Occmonth :发生额月份,DebitOccur :发生额
select a.AccID
from TestDB a,( select Occmonth,max(DebitOccur) from TestDB where AccID = '101' group by Occmonth )b
where a.Occmonth = b.Occmonth
and a.DebitOccur > b.DebitOccur;
5.怎么把这样一个表儿
查成这样一个结果
select year,
(select amount from table m where month=1 and m.year=aaa.year) as m1,
(select amount from table m where month=2 and m.year=aaa.year) as m2,
(select amount from table m where month=3 and m.year=aaa.year) as m3,
(select amount from table m where month=4 and m.year=aaa.year) as m4
from table
group by year
6.复制表( 只复制结构, 源表名:a新表名:b)
select * into b from a where 1<>1; // where1=1,拷贝表结构和数据内容
7.拷贝表( 拷贝数据, 源表名:a目标表名:b)
insert into b(a, b, c) select d,e,f from a;
8.显示文章、提交人和最后回复时间
select a.title,a.username,b.adddate
from table a,
(select max(adddate) adddate
from table
where table.title=a.title) b;
9.日程安排提前五分钟提醒
select * from 日程安排
where datediff('minute',f 开始时间,getdate())>5;
10.两张关联表,删除主表中已经在副表中没有的信息
delete from info
where not exists (
select * from infobz
where info.infid=infobz.infid
);
11.有两个表A 和B ,均有key 和value 两个字段,如果B 的key 在A 中也有,就把B 的value 换为A 中对应的value?
update b set b.value=(select a.value from a where a.key=b.key)
where b.id in(
select b.id from b,a
where b.key=a.key
);
12.已知有如下4张表:
学生表:STUDENT(S#,SNAME,SAGE,SSEX)
课程表:COURSE(C#,CNAME,T#)
成绩表:SC(S#,C#,SCORE)
教师表:TEACHER(T#,TNAME)
(1)查询课程编号为“001”的课程比“002”的课程成绩高的所有学生的学号?
select x.sno,x.score,y.score from sc x,sc y
where x.cno=1001
and y.cno=1002
and x.sno=y.sno
and x.score> y.score;
(2)查询平均成绩大于60分的学生的学号和平均成绩?
select sno,avg(score) from sc
group by sno
having avg(score)>60;
(3)查询所有学生的学号、姓名、选课数、总成绩?
select sc.sno,sname,count(cno),sum(score)
from student
join sc
on student.sno=sc.sno
group by sc.sno,sname;
(4)查询姓“悟”的老师的个数?
select count(Tname) from teacher
where Tname like'悟%';
(5)查询没学过“悟空”老师课的学生的学号、姓名?
select sno,sname from student
where sno not in(
select sno from SC
where cno in(
select cno from course
where tno in(
select tno from teacher
where tname='悟空'
)
)
);
Strjson博客-专注于各种精品源码、精品软件、技术教程分享、黑客技术、破解教程(爱你在心口难开、没事写一写)
https://jpgke.com/lg/377.html(转载时请注明本文出处及文章链接)