mysql查询语句大全讲解及案例 (mysql实例)

mysql实例查询的sql技巧 - 问题记录

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

(查询语文(01)成绩比数学(02)成绩高的学生信息及分数)

select a.*,b.CID,b.score,c.CID,c.score from Student a,SC b,SC c where a.SID=b.SID and b.SID=c.SID and b.CID='01' and c.CID='02' and b.score > c.score;

技巧:关联多一个成绩表 作为比对 01 02 成绩

不存在 02 课程的情况

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

left join SC b on a.SID = b.SID and b.CID = '01'

left join SC c on a.SID = c.SID and c.CID = '02'

where b.score > isnull(c.score)

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

同上

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

select a.SID,a.Sname,cast(avg(b.score) as decimal(5,2)) from Student a,SC b where a.SID=b.SID group by a.SID,a.Sname having avg(b.score) > 60;

按SID,Sname 分组

cast(number as 进制(总位数,小数点))

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

同上

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

有成绩的学生

select a.SID,a.Sname,sum(b.score),count(b.CID) from Student a,SC b where a.SID=b.SID group by a.SID,a.Sname;

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

包括没有成绩的学生:

select a.SID,a.Sname,sum(b.score),count(b.CID) from Student a left join SC b on a.SID=b.SID group by a.SID,a.Sname;

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

select count(Tname) from Teacher where Tname like '李%';

select count(Tname) 李姓老师的数量 from Teacher where left(Tname,1) = '李'

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

select a.SID,a.CID,b.* from SC a,Student b where a.SID=b.SID and a.CID='02';

方法2

select d.*,a.Tid,a.Tname,b.CID,b.Cname from Teacher a , Course b,SC c,Student d where a.Tid='01' and a.Tid=b.Tid and b.CID=c.CID and c.SID=d.SID;

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

select e.* from Student e where e.SID not in (select d.SID from Teacher a , Course b,SC c,Student d where a.Tid='01' and a.Tid=b.Tid and b.CID=c.CID and c.SID=d.SID);

方法2

select * from Student where SID not in(select a.SID from Student a,SC b where a.SID=b.SID and b.CID=02);

not in 先查出学过 老师的 课,再排除

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

select * from Student where SID in (select SID from SC where CID in(01,02) group by SID having count(SID)>1);

--方法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 Student.* from Student , SC where Student.SID = SC.SID and SC.CID = '02' and exists (Select 1 from SC SC_2 where SC_2.SID = SC.SID and SC_2.CID = '01') order by Student.SID

--方法3

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

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

select distinct b.* from SC a,Student b where a.SID not in (select SID from SC where CID=02) and a.SID=b.SID;

查出 02 再排除

--方法1

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

--方法2

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') order by Student.SID

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

select * from Student where SID in (select SID from SC group by SID having count(SID)<3);

select a.SID,a.Sname,a.Sage,a.Ssex,c.Cname from Student a,SC b,Course c where a.SID=b.SID and b.CID=c.CID and a.SID in (select SID from SC group by SID having count(SID)<3);

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

select * from Student where SID in (select distinct SID from SC where CID in (select CID from SC where SID='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';

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

select * from Student where SID in(select distinct SID from SC where SID!=01 and CID in (01,02,03) group by SID having count(SID)>2 );

选出 课程及数量,再排除

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'))

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

select * from Student where SID not in(select SID from SC where CID in (select a.CID from Course a,Teacher b where a.TID=b.TID and a.TID=01));

查询老师对应的课程,再排除

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 = '张三') order by Student.SID;

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

select a.SID,a.Sname,avg(b.score) from Student a,SC b where a.SID=b.SID and a.SID in (select distinct SID from SC where score < 60) group by a.SID,a.Sname;

方法2:

select Student.SID , Student.sname , cast(avg(score) as decimal(18,2)) 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;

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

select a.*,b.score from Student a,SC b where b.CID=01 and a.SID=b.SID and b.score <60 order by b.score desc;

Q--17、按平均成绩从高到低显示所有学生的所有课程的成绩以及平均成绩

缺失了没有选课的学生

select a.SID,a.Sname,sum(b.score),avg(b.score) from Student a,SC b where a.SID=b.SID group by a.SID,a.Sname order by avg(b.score) desc;

带上NULL学生,截取精度

select a.SID,a.Sname,sum(b.score),cast(avg(b.score) as decimal(5,2)) from Student a left join SC b on a.SID=b.SID group by a.SID,a.Sname order by avg(b.score) desc;

方法1

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

方法2

declare @sql nvarchar(4000)

set @sql = 'select a.SID ' + '学生编号' + ' , a.Sname ' + '学生姓名'

select @sql = @sql + ',max(case c.Cname when '''+Cname+''' then b.score else null end) '+Cname+' '

from (select distinct Cname from Course) as t

set @sql = @sql + ' , 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'

exec(@sql)

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

--及格为>=60,中等为:70-80,优良为:80-90,优秀为:>=90

select CID,max(score),min(score),avg(score) from SC group by CID;

及格率计算

cast 连接符

decimal 截取精度

select 子查询

select (select count(1) from SC where CID=b.CID and score>=60) from SC,Course b;

(select count(1) from SC where CID=b.CID and score >=60 ) / (select count(1) from SC where CID=b.CID) as decimal(5,2)

select b.CID,b.Cname,cast((select count(1) from SC where CID=b.CID and score >=60 ) / (select count(1) from SC where CID=b.CID) as decimal(5,2)) from SC a,Course b group by b.CID,b.Cname;

--方法1

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

max(n.score) 最高分 ,

min(n.score) 最低分 ,

cast(avg(n.score) as decimal(18,2)) 平均分 ,

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)) 及格率 ,

cast((select count(1) from SC where CID = m.CID and score >= 70 and score < 80 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 中等率 ,

cast((select count(1) from SC where CID = m.CID and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优良率 ,

cast((select count(1) from SC where CID = m.CID and score >= 90)*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

order by m.CID

--方法2

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

(select max(score) from SC where CID = m.CID) 最高分 ,

(select min(score) from SC where CID = m.CID) 最低分 ,

(select cast(avg(score) as decimal(18,2)) from SC where CID = m.CID) 平均分 ,

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)) 及格率,

cast((select count(1) from SC where CID = m.CID and score >= 70 and score < 80 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 中等率 ,

cast((select count(1) from SC where CID = m.CID and score >= 80 and score < 90 )*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优良率 ,

cast((select count(1) from SC where CID = m.CID and score >= 90)*100.0 / (select count(1) from SC where CID = m.CID) as decimal(18,2)) 优秀率

from Course m

order by m.CID

###### 子查询 ########

select * ,(select count(1) from SC where CID=b.CID and score>=60) from SC a,Course b;

mysqlsql查询语句大全及实例,mysql查询sql执行记录

mysqlsql查询语句大全及实例,mysql查询sql执行记录

相当于

select count(1) from SC where CID=01 and score >=60;

select count(1) from SC where CID=02 and score >=60;

select count(1) from SC where CID=03 and score >=60;

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

select a.Cname,b.CID,max(b.score) from SC b ,Course a group by b.CID;

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

缺失了没有成绩的学生

select a.Sname,sum(b.score) from Student a,SC b where a.SID=b.SID group by b.SID order by sum(b.score) desc;

left join 包括全部

select a.Sname,sum(b.score) from Student a left join SC b on a.SID=b.SID group by b.SID order by sum(b.score) desc;

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

方法1

select a.Tname,b.CID,b.Cname,avg(c.score) from Teacher a,Course b,SC c where a.TID=b.TID and b.CID=c.CID group by c.CID order by avg(c.score) desc;

方法2

select (select avg(score) from SC where CID=b.CID group by CID),a.Tname,b.CID,b.Cname from Teacher a,Course b where a.TID=b.TID;

使用子查询

方法3

select m.TID , m.Tname , cast(avg(o.score) as decimal(18,2)) 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

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

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

select sum(case when score >= 85 then 1 else 0 end) '85-100' ,SID from SC;

Q--24、查询学生平均成绩及其名次

select a.*,avg(b.score) from Student a left join SC b on a.SID=b.SID group by b.SID order by avg(b.score) desc;

####问题列表

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

Q--27、查询出只有两门课程的全部学生的学号和姓名

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

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

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

Q--31、查询1990年出生的学生名单

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

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

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

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

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

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

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

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

Q--40、查询选修"张三"老师所授课程的学生中,成绩最高的学生信息及其成绩--40.1 当最高分只有一个时

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

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

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

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

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

Q--46、查询各学生的年龄--46.1 只按照年份来算

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

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

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

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

mysqlsql查询语句大全及实例,mysql查询sql执行记录