欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

总结一些面试时常考的sql语句

程序员文章站 2022-06-17 21:21:55
数据准备 --创建测试数据 create table student(s varchar(10),sname nvarchar(10),sage datetime,ssex nvarchar(...

数据准备

--创建测试数据
create table student(s varchar(10),sname nvarchar(10),sage datetime,ssex nvarchar(10));
insert into student values('01','赵英','1990-01-01','男');
insert into student values('02','钱雄','1990-12-21','男');
insert into student values('03','孙豪','1990-05-20','男');
insert into student values('04','李杰','1990-08-06','男');
insert into student values('05','周梅','1991-12-01','女');
insert into student values('06','吴兰','1992-03-01','女');
insert into student values('07','郑竹','1989-07-01','女');
insert into student values('08','王菊','1990-01-20','女');
create table course(c varchar(10),cname nvarchar(10),t varchar(10));
insert into course values('01','语文','02');
insert into course values('02','数学','01');
insert into course values('03','英语','03');
insert into course values('04','物理','01');
create table teacher(t varchar(10),tname nvarchar(10));
insert into teacher values('01','张三');
insert into teacher values('02','李四');
insert into teacher values('03','王五');
create table sc(s varchar(10),c varchar(10),score decimal(18,1));
insert into sc values('01','01',80);
insert into sc values('01','02',90);
insert into sc values('01','03',99);
insert into sc values('02','01',70);
insert into sc values('02','02',60);
insert into sc values('02','03',80);
insert into sc values('03','01',80);
insert into sc values('03','02',80);
insert into sc values('03','03',80);
insert into sc values('04','01',50);
insert into sc values('04','02' 30);
insert into sc values('04','03',20);
insert into sc values('05','01',76);
insert into sc values('05','02',87);
insert into sc values('06','01',31);
insert into sc values('06','03',34);
insert into sc values('07','02',89);
insert into sc values('07','03',98);

--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数
自己一开始想的:
select * from (select m.s, m.score "01",n.score "02" from (select s,score from sc where sc.c="01") m, (select s,score from sc where sc.c="02")n where m.s=n.s and m.score>n.score) o , student where o.s=student.s;


select * from student s ,sc s1,sc s2 where s.s = s1.s and s1.c='01' and s.s = s2.s and s2.c='02' and s1.score>s2.score;
--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩
select s.s,s.sname,avg(sc.score) avgscore from student s,sc where s.s = sc.s group by sc.s having avg(sc.score)>=60;
--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩
select s.s,s.sname,count(sc.c) ctotal,ifnull(sum(sc.score),0) stotal from student s left join sc on s.s = sc.s group by s.s;
--6、查询"李"姓老师的数量 
select count(*) from teacher t where t.tname like '李%';
--7、查询学过"张三"老师授课的同学的信息 
select s.s,s.sname,s.sage,s.ssex from teacher t ,course c ,sc,student s where t.tname="张三" and t.t=c.t and c.c=sc.c and sc.s = s.s; 
--8、查询没学过"张三"老师授课的同学的信息 
select * from student s where s.s not in (select s.s from teacher t ,course c ,sc,student s where t.tname="张三" and t.t=c.t and c.c=sc.c and sc.s = s.s);
--11、查询没有学全所有课程的同学的信息 
select s.s,s.sname,s.sage,s.ssex from student s,sc,(select count(sc.c) m from sc) n, (select count(*) m from course) c where c.m !=n.m and sc.s=s.s group by s.s;
--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩 
select * from student s,(select sc.s,avg(sc.score) from sc where sc.score<60 group by sc.s having (count(*)>=2)) m where s.s=m.s;

--16、检索"01"课程分数小于60,按分数降序排列的学生信息
select * from  student s right join (select * from sc where sc.c='01' and sc.score<60) m on m.s=s.s ;
--26、查询每门课程被选修的学生数 
 select c.c,count(sc.s) from course c left join sc on c.c=sc.c group by c.c;
--32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列 
select c.c, ifnull(avg(sc.score),0) from course c left join sc on c.c=sc.c group by c.c order by avg(sc.score) desc;
--40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩
select s.*,c.cname,max(sc.score) from student s,sc,course c ,teacher t where s.s=sc.s and c.c=sc.c and c.t=t.t and t.tname="张三";