sql经典面试题50题

 

总结不甚清楚的有:10,19,20,22,24,25,28,41,42 

 

--1、查询"01"课程比"02"课程成绩高的学生的信息及课程分数

--1.1、查询同时存在"01"课程和"02"课程的情况

select a.* , b.score 课程01的分数,c.score 课程02的分数 from Student a , SC b , SC c

where a.SID = b.SID and a.SID = c.SID and b.CID = '01' and c.CID = '02' and b.score > c.score;

--1.2、利用了行转列的知识

select m.*,sc.score 
from (select student.* from student 
where sid in (
select sid from 
(select 
    sid,
    sum(case when cid='01'  then score  else null end) c1,
    sum(case when cid='02'  then score  else null end) c2
    from sc 
    group by sid
    order by sid) c
where c.c1>c.c2 )) m left join sc on m.sid=sc.sid
;

select s.* ,b.score from student s  left join sc b on s.sid=b.sid where s.sid in 
(select a.sid from (select * from sc  where cid='01' ) a left join (select * from sc  where cid='02' ) b on a.sid=b.sid where a.score>b.score) ;

 

--2、查询"01"课程比"02"课程成绩低的学生的信息及课程分数--2.1、查询同时存在"01"课程和"02"课程的情况

--3、查询平均成绩大于等于60分的同学的学生编号和学生姓名和平均成绩

分析:成绩表SC ,学生表

            涉及字段:学生编号和学生姓名和平均成绩

            条件:平均成绩大于等于60分

select a.SID,Sname,cast(avg(b.score) as decimal(18,2)) as 平均分
from Student a ,SC b
where a.SID =b.SID 
GROUP BY SID,Sname
HAVING cast(avg(b.score) as decimal(18,2))>=60
ORDER BY a.SID;  

--4、查询平均成绩小于60分的同学的学生编号和学生姓名和平均成绩

--4.1、查询在sc表存在成绩的学生信息的SQL语句。

select a.SID , a.Sname , cast(avg(b.score) as decimal(18,2)) avg_score

from Student a , sc b

where a.SID = b.SID

group by a.SID , a.Sname

having cast(avg(b.score) as decimal(18,2)) < 60

order by a.SID

--4.2、查询在sc表中不存在成绩的学生信息的SQL语句。

 

select a.SID , a.Sname , ifnull(cast(avg(b.score) as decimal(18,2)),0) avg_score

from Student a left join sc b

on a.SID = b.SID

group by a.SID , a.Sname

having ifnull(cast(avg(b.score) as decimal(18,2)),0) < 60

order by a.SID;

--5、查询所有同学的学生编号、学生姓名、选课总数、所有课程的总成绩

  --5.1、查询所有有成绩的SQL。

分析:有Student,SC表 字段有:学生编号、学生姓名、选课总数、所有课程的总成绩

select a.SID 学生编号 , a.Sname 学生姓名 , count(b.CID) 选课总数, sum(score) 所有课程的总成绩

from Student a , SC b

where a.SID = b.SID

group by a.SID,a.Sname

order by a.SID;

--5.2、查询所有(包括有成绩和无成绩)的SQL。

select a.SID,a.Sname,count(b.Cid),sum(b.score)  from student a 
left join sc b on a.SID =b.SID 
GROUP BY a.SID,a.Sname
ORDER BY a.SID;

--6、查询"李"姓老师的数量

SELECT count(tname) from teacher where Tname  like "李%";

--7、查询学过"张三"老师授课的同学的信息

select distinct Student.* from Student , SC , Course , Teacher
where Student.SID = SC.SID and SC.CID = Course.CID and Course.TID = Teacher.TID and Teacher.Tname = '张三'
order by Student.SID;

select s.*,t.tname,sc.score
 from 
 student s 
 left join sc on s.sid=sc.sid
 left join course c on sc.cid =c.cid
 left join teacher t on t.tid=c.tid
 where t.tname='张三';

 

--8、查询没学过"张三"老师授课的同学的信息

select distinct Student.SID from Student , SC , Course , Teacher
where Student.SID = SC.SID and SC.CID = Course.CID and Course.TID = Teacher.TID and Teacher.Tname = '张三'
order by Student.SID;

select m.* from Student m where SID not in (select distinct Student.SID from Student , SC , Course , Teacher
where Student.SID = SC.SID and SC.CID = Course.CID and Course.TID = Teacher.TID and Teacher.Tname = '张三') order by m.SID;

--9、查询学过编号为"01"并且也学过编号为"02"的课程的同学的信息

方法1:

select student.*from student,sc where student.sid=sc.sid 
and sc.cid='01' and exists(SELECT 1 FROM sc sc_2 where sc_2.sid=sc.sid and sc_2.cid='02')
order by student.SID;

方法2:

select m.* from student m where sid in (
select sid from (
select distinct SID from SC where CID = '01'
union all
select distinct SID from SC where CID = '02'
)t GROUP BY sid HAVING count(1)=2
)
ORDER BY m.SID;

方法3:

select * from student s where s.sid in (
select s1.sid
from 
    (select  sid,cid,score from sc where cid='01') s1
    left join 
    (select  sid,cid,score from sc where cid='02') s2
    on s1.sid=s2.sid);
    

--10、查询学过编号为"01"但是没有学过编号为"02"的课程的同学的信息

select Student.* from Student , SC where Student.SID = SC.SID and SC.CID = '01' and not exists (Select 1 from SC SC_2 where SC_2.SID = SC.SID and SC_2.CID = '02') order by Student.SID;

select student.* from student,sc where student.sid=sc.sid and sc.cid='01' and student.sid not in (select sc_2.sid from sc sc_2 where sc_2.sid=sc.sid and sc_2.cid='02');

仍然还是嵌套,三层嵌套, 或者多表联合查询

select * from student
    where student.sid not in(
        select sc.sid from sc where sc.cid in(
            select course.cid from course where course.tid in(
                select teacher.tid from teacher where tname = "张三"
            )
        )
    );

 

select * from student
where student.sid not in(
    select sc.sid from sc,course,teacher 
    where
        sc.cid = course.cid
        and course.tid = teacher.tid
        and teacher.tname= "张三"
);

 

select s.* from student s inner join (select * from sc a where a.cid='01' and a.sid not in (select b.sid from sc b where cid='02')) c on s.sid=c.sid;

select  distinct s.* from student s inner join sc on s.sid=sc.sid and s.sid in (select a.sid from sc a where a.cid='01') and s.sid not in (select b.sid from sc b where b.cid='02');

select  s.sid,s.sname,s.sage,s.ssex from student s inner join sc on s.sid=sc.sid where sc.cid  in (select cid from sc where sc.sid="01") and s.sid <> '01' group by s.sid,s.sname,s.sage,s.ssex;

--11、查询没有学全所有课程的同学的信息

select a.sid,a.sname,a.sage,a.ssex
from student a left join sc b
on a.sid=b.sid
GROUP BY a.sid,a.sname,a.sage,a.ssex
HAVING count(b.cid)<(select count(cid) from course)
ORDER BY a.sid;

select a.sid,a.sname,a.sage,a.ssex
from student a ,sc b 
where a.sid=b.sid
GROUP BY a.sid,a.sname,a.sage,a.ssex
HAVING count(b.cid)<(select count(cid) from course)
ORDER BY a.sid;

 

--12、查询至少有一门课与学号为"01"的同学所学课程相同的同学的信息

select distinct student.* from student,sc where student.sid =sc.sid and sc.cid in (select cid from sc where sid='01') and student.sid <>'01';

select distinct s.* from student s inner join sc on s.sid=sc.sid where sc.cid  in (select cid from sc where sc.sid="01") and s.sid <> '01';

select * from student s where s.sid in (select stu.sid from (
select other.s sid,other.c cid
from
(
select cid from sc where sid=01
) s01
inner join
(
select sid s,cid c from sc where sid!=01
) other
on other.c=s01.cid) stu
group by sid 
having count(cid)>=1);

--13、查询和"01"号的同学学习的课程完全相同的其他同学的信息

分析:student sc 

select Student.* from Student where SID in
(select distinct SC.SID from SC where SID <> '01' and SC.CID in (select distinct CID from SC where SID = '01')
group by SC.SID having count(1) = (select count(1) from SC where SID='01'));

select  Student.* from Student , SC where Student.SID = SC.SID and sc.cid in (
select cid from sc where sid='01') and  student.sid <>'01'
GROUP BY student.sid,student.sname,student.sage,student.ssex 
HAVING count(student.sid)=(select count(1) from sc where sid='01');

--14、查询没学过"张三"老师讲授的任一门课程的学生姓名

select student.* from student where student.sid not in (select DISTINCT(sc.sid) from sc,course,teacher where sc.cid=course.cid and course.tid=teacher.tid and teacher.tname='张三');

 

select * from student s where s.sid not in (select sid from course c left join teacher t on c.tid =t.tid left join sc on sc.cid=c.cid where t.tname='张三');

 

--15、查询两门及其以上不及格课程的同学的学号,姓名及其平均成绩

分析:student score 

           字段:sid,sname,avg(score)

select student.sid,student.sname,avg(score) from student,sc
where student.sid=sc.sid and student.sid in (select sid from sc where score<60
GROUP BY sid
HAVING count(1)>=2)
GROUP BY student.sid,student.sname;

select s.sid,s.sname,avg(sc.score) avgscore from student s left join sc on s.sid=sc.sid where sc.score<60 group by s.sid,s.sname having count(1)>=2

 

--16、检索"01"课程分数小于60,按分数降序排列的学生信息

分析:学生表 student  sc

           字段:student.*

select a.*,sc.* from student a,sc 
where sc.sid=a.sid and sc.cid='01'
and sc.score<=60
order by sc.score desc;

  --17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩--17.1 SQL 2000 静态

分析:student,course,sc

字段:student.*,course.*,sc.*,avg(score)

第一步:

select a.SID 学生编号, a.Sname,b.*,c.*,
case c.Cname when '语文' then b.score else null end '语文',
case c.Cname when '数学' then b.score else null end '数学',
case c.Cname when '英语' then b.score else null end '英语'
from Student a 
left join SC b on a.SID = b.SID
left join Course c on b.CID = c.CID;

第二步:

select a.SID 学生编号, a.Sname,
max(case c.Cname when '语文' then b.score else null end) '语文',
max(case c.Cname when '数学' then b.score else null end) '数学',
max(case c.Cname when '英语' then b.score else null end) '英语',
cast(avg(b.score) as decimal(18,2)) '平均分'
from Student a 
left join SC b on a.SID = b.SID
left join Course c on b.CID = c.CID
group by a.SID , a.Sname
order by '平均分' desc;

select a.SID 学生编号, a.Sname,
max(case c.Cname when '语文' then b.score else null end) 语文,
max(case c.Cname when '数学' then b.score else null end) 数学,
max(case c.Cname when '英语' then b.score else null end) 英语,
cast(avg(b.score) as decimal(18,2)) 平均分
from Student a 
left join SC b on a.SID = b.SID
left join Course c on b.CID = c.CID
group by a.SID , a.Sname
order by 平均分 desc;

两者的区别是,上面sql的结果报错,下面这个sql正确,不知道什么原因,就因为引号吗?真的恶心

--18、查询各科成绩最高分、最低分和平均分:以如下形式显示:课程ID,课程name,最高分,最低分,平均分,及格率,中等率,优良率,优秀率

-- 感觉是错的
select b.cid,b.cname,max(c.score) 最高分,min(c.score) 最低分,cast(avg(c.score) as decimal(18,2)) 平均分,
cast((select count(1) from sc where score>=60)*100/(select count(1) from sc) as decimal(18,2)) 及格率
from course b,sc c where b.cid=c.cid group by b.cid,b.cname
order by b.cid;

--这种方法又很复杂
select f.cname,e.* 
from course f 
left join 
(select c.cid,c.max,d.jigelv from 
	(select sc.cid,max(sc.score) max
		from sc group by sc.cid) c 
left join 
	(select a.cid,c1/c2 jigelv from 
		(select cid ,count(1) c1 from sc where sc.score>=60 group by cid) a 
		left join 
		(select cid ,count(1) c2 from sc  group by cid) b on a.cid=b.cid
	) d 
on c.cid=d.cid
) e 
on e.cid=f.cid;

--这种方法最简单
select f.cname,e.* 
from course f 
left join 
(
select sc.cid,max(score) max,min(score) min,
sum(case when score>=60 then 1 else 0 end)/count(sc.cid) ge60,
sum(case when score>=70 and score<80 then 1 else 0 end)/count(sc.cid) ge70,
sum(case when score>=80 and score<90 then 1 else 0 end)/count(sc.cid) ge80,
sum(case when score>=90 then 1 else 0 end)/count(sc.cid) ge90
from sc group by cid

) e 
on e.cid=f.cid;

--19、按各科成绩进行排序,并显示排名

方法1:

select t.* ,((select count(1) from SC where CID = t.CID and score > t.score) + 1) px from sc t order by t.cid , px

select t.* ,((select count(distinct(score)) from SC where CID = t.CID and score >= t.score) + 1) px from sc t order by t.cid , px

方法2:

select t.* , rank() over(partition by cid order by score desc) as ranking  from sc t order by t.CID , ranking

select t.* , dense_rank() over(partition by cid order by score desc) as ranking  from sc t order by t.CID , ranking

 

--20、查询学生的总成绩并进行排名

--20.1 查询学生的总成绩

select m.sid,m.sname,ifnull(sum(n.score),0) 总成绩
from student m left join sc n on m.sid=n.sid
group by m.sid,m.sname
order by 总成绩 desc

--20.2 查询学生的总成绩并进行排名,sql 2000用子查询完成,分总分重复时保留名次空缺和不保留名次空缺两种。

一:
select t1.* , ((select count(1) from
(
select m.SID 学生编号 ,
m.Sname 学生姓名 ,
ifnull(sum(score),0) 总成绩
from Student m left join SC n on m.SID = n.SID
group by m.SID , m.Sname
) t2 where 总成绩 > t1.总成绩) + 1) px from
(
select m.SID 学生编号 ,
m.Sname 学生姓名 ,
ifnull(sum(score),0) 总成绩
from Student m left join SC n on m.SID = n.SID
group by m.SID , m.Sname
) t1
order by px

二:
select t1.* , ((select count(distinct 总成绩) from
(
select m.SID 学生编号 ,
m.Sname 学生姓名 ,
ifnull(sum(score),0) 总成绩
from Student m left join SC n on m.SID = n.SID
group by m.SID , m.Sname
) t2 where 总成绩 >= t1.总成绩) + 1) px from
(
select m.SID 学生编号 ,
m.Sname 学生姓名 ,
ifnull(sum(score),0) 总成绩
from Student m left join SC n on m.SID = n.SID
group by m.SID , m.Sname
) t1
order by px;

select sid,sum(score) sumscore 
,rank() over(order by sum(score) desc) from SC group by sid;

--20.3 查询学生的总成绩并进行排名,sql 2005用rank,DENSE_RANK完成,分总分重复时保留名次空缺和不保留名次空缺两种。

select t.*,(rank() over( order by 总成绩 desc)) px from 
(select m.sid,m.sname,ifnull(sum(n.score),0) 总成绩
from student m left join sc n on m.sid=n.sid
group by m.sid,m.sname)t
order by px


select t.*,(dense_rank() over( order by 总成绩 desc)) px from 
(select m.sid,m.sname,ifnull(sum(n.score),0) 总成绩
from student m left join sc n on m.sid=n.sid
group by m.sid,m.sname)t
order by px

-21、查询不同老师所教不同课程平均分从高到低显示

select m.tid,m.tname,avg(o.score) avg_score
from teacher m,course n,sc o 
where m.tid=n.tid and n.cid=o.cid
group by m.tid,m.tname
order by avg(score) desc;

select t.tid,t.tname,c.cname,avg(sc.score) avg from teacher t left join course c on c.tid=t.tid left join sc on sc.cid=c.cid group by t.tid,t.tname,c.cname order by avg desc

--22、查询所有课程的成绩第2名到第3名的学生信息及该课程成绩

--22.1 sql 2000用子查询完成

--Score重复时保留名次空缺

select * from (select t.* , ( (select count(1) from SC where CID = t.CID and score > t.score) + 1) px from sc t) m where px between 2 and 3 order by m.cid , m.px;

select * from (select t.*,((select count(1) from sc where cid=t.cid and score>t.score)+1) px from sc t) m where px between 2 and 3 order by m.cid,m.px;

--Score重复时合并名次

select * from (select t.* ,  (select count(distinct score) from SC where CID = t.CID and score >= t.score) px from sc t) m where px between 2 and 3 order by m.cid , m.px

--22.2 用窗口函数

select * from (select t.* , RANK() over(partition by Cid order by score desc) px from sc t) m where px between 2 and 3 order by m.cid,m.px
select * from (select t.* , dense_rank() over(partition by cid order by score desc) px from sc t) m where px between 2 and 3 order by m.CID , m.px

 

--23、统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60 及所占百分比

--23.1 统计各科成绩各分数段人数:课程编号,课程名称, 100-85 , 85-70 , 70-60 , 0-60

--横向显示

select b.cid,b.cname,
sum(case when c.score<=100 and c.score>85 then 1 else 0 end) as '100-85',
sum(case when c.score<=85 and c.score>70 then 1 else 0 end) as '85-70',
sum(case when c.score<=70 and c.score>60 then 1 else 0 end ) as '70-60',
sum(case when c.score<=60 and c.score>0 then 1 else 0 end )as '60-0'
from course b,sc c
where b.cid=c.cid
group by b.cid,b.cname

 

 下面这个方法也可以:

select b.cid,b.cname,
count(case when c.score<=100 and c.score>85 then 1  end) as '100-85',
count(case when c.score<=85 and c.score>70 then 1  end) as '85-70',
count(case when c.score<=70 and c.score>60 then 1  end ) as '70-60',
count(case when c.score<=60 and c.score>0 then 1  end )as '60-0'
from course b,sc c
where b.cid=c.cid
group by b.cid,b.cname

下面这种也可以,但我不甚理解

select m.CID 课程编号, m.Cname 课程名称,
(select count(1) from SC where CID = m.CID and score <= 60) '0-60',
(select count(1) from SC where CID = m.CID and score > 60 and score <= 70) '60-70' ,
(select count(1) from SC where CID = m.CID and score >70 and score <= 85) '70-85' ,
(select count(1) from SC where CID = m.CID and score > 85) '85-100' 
from Course m
order by m.CID

--纵向显示1(显示存在的分数段)

select b.cid,b.cname,
(
case when c.score>=85 then '85-100'
when c.score>=70 and c.score<85 then '70-85'
when c.score>=60 and c.score<70 then '60-70'
else '0-60'
end 
) '分数段'
,
count(1) '数量'
from course b,sc c
where b.cid=c.cid
group by b.cid,b.cname,
(
case when c.score>=85 then '85-100'
when c.score>=70 and c.score<85 then '70-85'
when c.score>=60 and c.score<70 then '60-70'
else '0-60'
end 
) 

 

--纵向显示2(显示存在的分数段,不存在的分数段用0显示) ,

求百分比的时候 有点不理解,cast(count(1) * 100.0 / (select count(1) from sc where CID = m.CID) as decimal(18,2)) 百分比

--横向显示

select m.CID 课程编号, m.Cname 课程名称,

(select count(1) from SC where CID = m.CID and score < 60) 0-60 ,

cast((select count(1) from SC where CID = m.CID and score < 60)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比 ,

(select count(1) from SC where CID = m.CID and score >= 60 and score < 70) 60-70 ,

cast((select count(1) from SC where CID = m.CID and score >= 60 and score < 70)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比 ,

(select count(1) from SC where CID = m.CID and score >= 70 and score < 85) 70-85 ,

cast((select count(1) from SC where CID = m.CID and score >= 70 and score < 85)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比 ,

(select count(1) from SC where CID = m.CID and score >= 85) 85-100 ,

cast((select count(1) from SC where CID = m.CID and score >= 85)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 百分比

from Course m

order by m.CID

--纵向显示1(显示存在的分数段)

select m.CID 课程编号 , m.Cname 课程名称 ,  (

case when n.score >= 85 then '85-100'

when n.score >= 70 and n.score < 85 then '70-85'

when n.score >= 60 and n.score < 70 then '60-70'

else '0-60'

end)  分数段,

count(1) 数量 ,

cast(count(1) * 100.0 / (select count(1) from sc where CID = m.CID) as decimal(18,2)) 百分比

from Course m , sc n

where m.CID = n.CID

group by m.CID , m.Cname , (

case when n.score >= 85 then '85-100'

when n.score >= 70 and n.score < 85 then '70-85'

when n.score >= 60 and n.score < 70 then '60-70'

else '0-60'

end)

order by m.CID , m.Cname , 分数段

--24、查询学生平均成绩及其名次--24.1 查询学生的平均成绩并进行排名,sql 2000用子查询完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。

一:原始的方法不太懂:

select t1.* , px = (select count(1) from

(

select m.SID 学生编号 ,

m.Sname 学生姓名 ,

isnull(cast(avg(score) as decimal(18,2)),0) 平均成绩

from Student m left join SC n on m.SID = n.SID

group by m.SID , m.Sname

) t2 where 平均成绩 > t1.平均成绩) + 1 from

(

select m.SID 学生编号 ,

m.Sname 学生姓名 ,

isnull(cast(avg(score) as decimal(18,2)),0) 平均成绩

from Student m left join SC n on m.SID = n.SID

group by m.SID , m.Sname

) t1

order by px

select t1.* , px = (select count(distinct 平均成绩) from

(

select m.SID 学生编号 ,

m.Sname 学生姓名 ,

isnull(cast(avg(score) as decimal(18,2)),0) 平均成绩

from Student m left join SC n on m.SID = n.SID

group by m.SID , m.Sname

) t2 where 平均成绩 >= t1.平均成绩) from

(

select m.SID 学生编号 ,

m.Sname 学生姓名 ,

isnull(cast(avg(score) as decimal(18,2)),0) 平均成绩

from Student m left join SC n on m.SID = n.SID

group by m.SID , m.Sname

) t1

order by px

二:sql 2005用rank,DENSE_RANK完成,分平均成绩重复时保留名次空缺和不保留名次空缺两种。

select t.* ,  rank() over(order by 平均成绩 desc) ranking from
(
select m.SID 学生编号 ,
m.Sname 学生姓名 ,
ifnull(cast(avg(score) as decimal(18,2)),0) 平均成绩
from Student m left join SC n on m.SID = n.SID
group by m.SID , m.Sname
) t
order by ranking

--25、查询各科成绩前三名的记录

题目的意思是每个科目前三名。

--25.2 分数重复时不保留名次空缺,合并名次

--sql 2000用子查询实现

select * from (select t.* , (select count(distinct score)  from SC where CID = t.CID and score >= t.score) px from sc t) m where px between 1 and 3 order by m.Cid , m.px

二:用rank() 窗口函数

select * from (select sc.sid,sc.cid,sc.score, rank() over(partition by sc.cid order by sc.score desc) ranking
from sc ) t
where t.ranking <=3

select t.* , DENSE_RANK() over(partition by Cid order by score desc) px from sc t

select * from (select t.* , DENSE_RANK() over(partition by Cid order by score desc) px from sc t) m where px between 1 and 3 order by m.cid,m.px

--26、查询每门课程被选修的学生数

select sc.cid,count(sc.sid) 学生数 from sc 
group by sc.cid;

--27、查询出只选了两门课程的学生信息--学号和姓名

第一种:

select a.sid,a.sname ,d.count from student a
left join (SELECT c.sid,count(c.cid) as count from sc c group by c.sid) d on a.sid=d.sid 
where count=2;

第二种:

select a.sid,a.sname,count(c.cid)  count from student a
left join sc c on a.sid=c.sid 
group by a.sid,a.sname
having count=2;

第三种:

select a.sid,a.sname,count(c.cid) count from student a ,sc c
where a.sid =c.sid
group by a.sid,a.sname
having count=2;

--28、查询男生、女生人数

select ssex ,count(1) from student
group by ssex;

对这case when 不是很清晰,得捋捋

select count(Ssex) as 男生人数 from Student where Ssex = N'男'

select count(Ssex) as 女生人数 from Student where Ssex = N'女'

select sum(case when Ssex = N'男' then 1 else 0 end) 男生人数 ,sum(case when Ssex = N'女' then 1 else 0 end) 女生人数 from student

select case when Ssex = N'男' then N'男生人数' else N'女生人数' end 男女情况 , count(1) 人数 from student group by case when Ssex = N'男' then N'男生人数' else N'女生人数' end

--29、查询名字中含有"风"字的学生信息

SELECT * from student where sname  like "%风%";

--30、查询同名同性学生名单,并统计同名人数

select sname,count(*) from student group by sname
having count(*)>1;

--31、查询1990年出生的学生名单(注:Student表中Sage列的类型是datetime)

select * from student where year(student.sage)=1990;

逻辑是这样但运行不了,估计还得去看函数用法

select * from Student where datediff(yy,sage,'1990-01-01') = 0

select * from Student where datepart(yy,sage) = 1990

select * from Student where convert(varchar(4),sage,120) = '1990'

--32、查询每门课程的平均成绩,结果按平均成绩降序排列,平均成绩相同时,按课程编号升序排列

select m.cid,m.cname,avg(n.score) avg_score 
from course m,sc n
where m.cid =n.cid
group by m.cid,m.cname
order by avg_score desc,m.cid asc;

--33、查询平均成绩大于等于85的所有学生的学号、姓名和平均成绩

select a.sid,a.sname,avg(c.score) avg_score 
from student a,sc c
where a.sid =c.sid
group by a.sid,a.sname
having avg_score>=85
order by a.sid;

--34、查询课程名称为"数学",且分数低于60的学生姓名和分数

select a.sid,a.sname,b.cname,c.score
from student a,sc c,course b
where a.sid =c.sid and c.cid=b.cid
and b.cname='数学' and c.score<60;

--35、查询所有学生的课程及分数情况

select a.sid,a.sname,b.cname,c.score
from student a,sc c,course b
where a.sid =c.sid and c.cid=b.cid

--36、查询任何一门课程成绩在70分以上的姓名、课程名称和分数

select a.sid,a.sname,b.cname,c.cid,c.score
from student a,sc c,course b
where a.sid =c.sid and c.cid=b.cid and c.score >= 70
order by a.sid,c.cid

--37、查询不及格的课程

select a.sid,a.sname,b.cname,c.cid,c.score
from student a,sc c,course b
where a.sid =c.sid and c.cid=b.cid  and c.score < 60
order by a.sid,c.cid

--38、查询课程编号为01且课程成绩在80分以上的学生的学号和姓名

select a.sid,a.sname,c.score,c.cid from student a,sc c 
where a.sid =c.sid and c.cid ='01' and c.score>=80;

--39、求每门课程的学生人数

select b.cid,b.cname,count(1) from course b,sc c 
where b.cid=c.cid 
group by b.cid,b.cname;

--40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩

--40.1 当最高分只有一个时

select  Student.* , Course.Cname , SC.CID , SC.score
from Student, SC , Course , Teacher
where Student.SID = SC.SID and SC.CID = Course.CID and Course.TID = Teacher.TID and Teacher.Tname = N'张三'
order by SC.score desc 
limit 1

--40.2 当最高分出现多个时

select a.sid,a.sname,a.sage,a.ssex,d.score from student a,course b,teacher c,sc d 
where a.sid=d.sid and d.cid=b.cid and b.tid=c.tid and c.tname='张三' and d.score=(select max(e.score) from 
(select a.*,d.score from student a,course b,teacher c,sc d 
where a.sid=d.sid and d.cid=b.cid and b.tid=c.tid 
and c.tname='张三') e)

--41、查询不同课程但成绩相同的学生的学生编号、课程编号、学生成绩

第一种,同一个学生不同课程但成绩相同

select  a.sid,a.cid,a.score,s.sname from student s left join sc a on s.sid=a.sid  left join sc b on a.sid=b.sid where a.cid <> b.cid and a.score=b.score 
group by a.sid,a.cid,a.score,s.sname;

第二种:不同学生不同课程但成绩相同

理解错误,答案也不知道是啥了

 

 

--方法1

select m.* from SC m ,(select CID , score from SC group by CID , score having count(1) > 1) n

where m.CID= n.CID and m.score = n.score order by m.CID , m.score , m.SID

--方法2

select m.* from SC m where exists (select 1 from (select CID , score from SC group by CID , score having count(1) > 1) n

where m.CID= n.CID and m.score = n.score) order by m.CID , m.score , m.SID

--42、查询每门功成绩最好的前两名

照葫芦画瓢的答案,能用

select * from (select t.* , RANK() over(partition by Cid order by score desc) px from sc t) m where px between 1 and 2 order by m.cid,m.px
select * from (select t.* , dense_rank() over(partition by cid order by score desc) px from sc t) m where px between 1 and 2 order by m.CID , m.px

select * from (select t.* , RANK() over(partition by Cid order by score desc) px from sc t ) m where m.px <3

--43、统计每门课程的学生选修人数(超过5人的课程才统计)。要求输出课程号和选修人数,查询结果按人数降序排列,若人数相同,按课程号升序排列

select course.cid,course.cname ,count(*) 学生人数
from course,sc
where course.cid=sc.cid
group by course.cid,course.cname
having count(*) >=5
order by 学生人数 desc,course.cid asc

--44、检索至少选修两门课程的学生学号

select a.sid,a.sname from student a,sc 
where a.sid=sc.sid group by a.sid,a.sname
having count(1)>=2

--45、查询选修了全部课程的学生信息

select a.sid,a.sname from student a,sc 
where a.sid=sc.sid group by a.sid,a.sname
having count(1)>2

select student.* from student where SID in
(select SID from sc group by SID having count(1) = (select count(1) from course))

 

--46、查询各学生的年龄

select student.sid ,student.sname,TIMESTAMPDIFF(year,sage,now()) from student;

--47、查询本周过生日的学生

select *
from student 
where WEEKOFYEAR(student.Sage)=WEEKOFYEAR(CURDATE());

--48、查询下周过生日的学生

select * from student where WEEKOFYEAR(student.Sage)=WEEKOFYEAR(CURDATE())+1;

--49、查询本月过生日的学生

select * from student where MONTH(student.Sage)=MONTH(CURDATE());

--50、查询下月过生日的学生

select * from student where MONTH(student.Sage)=MONTH(CURDATE())+1;

--51 查询选修了全部课程的学生信息

 select s.sid,s.sname from student s inner join 
(select sid from 
(select count(cid) c1 from course) c
left join (select sc.sid,count(cid) c2 from sc group by sc.sid ) d
on c.c1=d.c2) co
on s.sid=co.sid;

参考:

1.数据库MySQL经典面试题之SQL语句

2.mysql 中 isnull 和 ifnull 判断字段是否为null https://blog.csdn.net/cxqiang2013/article/details/35587295

3.SQL:mysql中如何使用isnull函数 https://blog.csdn.net/lch_2016/article/details/81007761

4.【mysql题目】按各科成绩排序,并显示名次(同名次空缺vs合并)

5.SQL练习题-50道SQL练习题及答案与详细分析

 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值