50个MySql语句

Student(S#,Sname,Sage,Ssex) 学生表
Course(C#,Cname,T#) 课程表
SC(S#,C#,score) 成绩表
Teacher(T#,Tname) 教师表

学生表:

create table student
(
   sid varchar(10),
   sname varchar(10),
   sage int, 
   ssex varchar(2)
)

学生表添加语句:

INSERT INTO `student` VALUES ('s001','student1',23,'1'),('s002','student2',26,'0'),('s004','student4',24,'1'),('s005','student5',25,'1'),('s006','student6',26,'0'),('s008','student8',28,'0'),('s009','student9',29,'0'),('s011','student11',21,'1'),('s012','student12',22,'1'),('s014','student14',24,'0');

课程表:

create table course
(
   cid varchar(10),
   cname varchar(10),
   tid  varchar(10)
)

课程表添加语句:

INSERT INTO `course` VALUES ('001','c1','t01'),('002','c2','t02'),('003','c3','t03'),('004','c4','t02'),('005','c5','t01');

成绩表:

create table sc
(
   sid varchar(10),
   cid varchar(10),
   score int
)

成绩添加语句:

INSERT INTO `sc` VALUES ('s001','001',87),('s001','002',77),('s001','005',67),('s002','002',66),('s002','003',87),('s002','004',57),('s004','003',57),('s004','005',97),('s004','002',87),('s005','002',47),('s005','001',67),('s006','004',67),('s006','002',77),('s006','003',67),('s008','001',67),('s008','004',77),('s009','002',87),('s009','004',67),('s009','005',47),('s011','001',67),('s011','003',77),('s011','005',92),('s012','003',91),('s012','002',83),('s012','004',76),('s014','003',72),('s014','005',82);

教师表:

create table teacher
(
   tid  varchar(10),
   tname varchar(10)
)

教师添加语句:

INSERT INTO `teacher` VALUES ('t01','teacher1'),('t02','teacher2'),('t03','teacher3');

答案如下:
1、查询"001"课程比"002"课程成绩高的所有学生的学号

select a.sid from sc a,sc b where a.sid=b.sid
and a.cid='001'
and b.cid='002'
and a.score>b.score

2、查询平均成绩大于60分的同学的学号和平均成绩;

select a.sid,avg(b.score) avgscore from student a, sc b 
where 
a.sid=b.sid
GROUP BY b.cid
HAVING avg(b.score)>=60

3、查询所有同学的学号、姓名、选课数、总成绩;

select a.sid,a.sname,count(*),sum(b.score) from student a,sc b where 
a.sid=b.sid GROUP BY b.sid

4、查询姓"李"的老师的个数;

select count(*) from teacher where tname like '李%'

5、查询没学过"叶平"老师课的同学的学号、姓名;

select sid,sname from student 
where sid not in(select c.sid 
from sc c 
join course co 
on c.cid=co.cid   
join teacher t 
on co.cid=t.tid where 
t.tname='叶平')

6、查询学过"001"并且也学过编号"002"课程的同学的学号、姓名;

select a.* from student a ,sc b, sc c where
a.sid=b.sid 
and b.sid=c.sid
and b.cid='001'
and c.cid='002'

7、查询学过"叶平"老师所教的所有课的同学的学号、姓名;

select a.sid ,a.sname from student a,teacher b ,sc c , course d where 
a.sid=c.sid
and c.cid=d.cid
and d.tid=b.tid
and b.tname='叶平'

8、查询课程编号"002"的成绩比课程编号"001"课程低的所有同学的学号、姓名;

select s1.sid,s.sname from sc s1,sc s2, student s where s1.sid=s2.sid and s.sid=s1.sid
and s1.cid='001'
and s2.cid='002'
and s2.score<s1.score

9、查询所有课程成绩小于60分的同学的学号、姓名;

select sid ,sname from student where sid not in(select sid from sc where score>60)

10、查询没有学全所有课的同学的学号、姓名;

select st.sid,st.sname from student st ,sc c
where st.sid=c.sid
GROUP BY st.sid,st.sname
HAVING count(cid)<(select count(cid) from course)

11、查询至少有一门课与学号为"s001"的同学所学相同的同学的学号和姓名;

select st.sid,st.sname from student st,sc s 
where st.sid=s.sid
and cid in(select cid from sc 
where sid='s001') 

12、查询至少学过学号为"s001"同学所有一门课的其他同学学号和姓名;

select st.sid,st.sname from student st ,sc s 
where st.sid=s.sid
and cid in (select cid from sc where sid='s001')

14、查询和"s002"号的同学学习的课程完全相同的其他同学学号和姓名;

select t1.sid,sname from(select sc.sid,count(distinct sc.cid) from (select cid from sc where sid='s002')t1
left join sc on t1.cid=sc.cid group by sc.sid having count(distinct sc.cid)= (select count(distinct cid) from sc where sid = 's002'))t1
left join student
on t1.sid=student.sid
where t1.sid!='s002'

15、删除学习"叶平"老师课的SC表记录;

delect sc 
from course, Teacher 
where course.Cid=sc.Cid 
and course.Tid=teacher.Tid 
and tname='叶平';

16、向SC表中插入一些记录,这些记录要求符合以下条件:
没有上过编号"002"课程的同学学号、002号课的平均成绩;

Insert SC select Sid,'002',
(Select avg(score) from SC where Cid='002') 
from Student where Sid not in (Select Sid from SC where Cid='002');

17、按平均成绩从高到低显示所有学生的"数据库"、“企业管理”、"英语"三门的课程成绩,按如下形式显示:学生ID,数据库,企业管理,英语,有效课程数,有效平均分

select Sid as 学生ID,
(select score from sc where sc.Sid=t.Sid and Cid='004') as 数据库,
(select score from sc where sc.Sid=t.Sid and Cid='001') as 企业管理,
(select score from sc where sc.Sid=t.Sid and Cid='006') as 英语,
count(*) as 有效课程数, avg(t.score) as 平局成绩
from sc as t
group by Sid 
order by avg(t.score)

18、查询各科成绩最高和最低的分:以如下形式显示:课程ID,最高分,最低分

select L.Cid as 课程ID, L.score as 最高分,
R.score as 最低分
from sc L, sc R 
where L.Cid = R.Cid 
and L.score = (select max(IL.score) 
from sc IL, student as IM 
where L.Cid=IL.Cid and IM.Sid=IL.Sid
group by IL.Cid)
and R.score = (select min(IR.score)
from sc as IR
where R.Cid=IR.Cid
group by IR.Cid);

19、按各科平均成绩从低到高和及格率的百分数从高到低顺序

select t.cid as 课程号,
	max(course.cname)as 课程名,
	isnull(avg(score),0) as 平均成绩,
	100 * sum(case when isnull(score,0)>=60 then 1 else 0 
								end)/count(*) as 及格百分数
    from  sc teac,course
    where t.cid=course.cid
    group by t.cid
    order by 100 * sum(case when isnull(score,0)>=60 then 1 
    									 else 0 end)/count(*) desc;

20、查询如下课程平均成绩和及格率的百分数(用"1行"显示): 企业管理(001),马克思(002),OO&UML (003),数据库(004)

SELECT max(Z.Tid) AS 教师ID,
 MAX(Z.Tname) AS 教师姓名,
 C.Cid AS 课程ID,
 AVG(Score) AS 平均成绩     
 FROM SC AS T,Course AS C ,Teacher AS Z    
 where T.Cid=C.Cid and C.Tid=Z.Tid   
 GROUP BY C.Cid    
 ORDER BY AVG(Score) DESC

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

 SELECT max(Z.Tid) AS 教师ID,
 MAX(Z.Tname) AS 教师姓名,
 C.Cid AS 课程ID,
 AVG(Score) AS 平均成绩     
 FROM SC AS T,Course AS C ,Teacher AS Z    
 where T.Cid=C.Cid and C.Tid=Z.Tid   
 GROUP BY C.Cid    
 ORDER BY AVG(Score) DESC

23、统计列印各科成绩,各分数段人数:课程ID,课程名称,[100-85],[85-70],[70-60],[ <60]

SELECT SC.Cid as 课程ID, Cname as 课程名称,
SUM(CASE WHEN score BETWEEN 85 AND 100 THEN 1 ELSE 0 END) AS [100 - 85]  ,
SUM(CASE WHEN score BETWEEN 70 AND 85 THEN 1 ELSE 0 END) AS [85 - 70],
SUM(CASE WHEN score BETWEEN 60 AND 70 THEN 1 ELSE 0 END) AS [70 - 60],
SUM(CASE WHEN score < 60 THEN 1 ELSE 0 END) AS [60 -]     
FROM SC,Course     
where SC.Cid=Course.Cid     
GROUP BY SC.Cid,Cname;

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

select cid,count(sid) from sc
GROUP BY cid

27、查询出只选修了一门课程的全部学生的学号和姓名

select sc.Sid, student.sname, count(Cid) as 选课数
from sc,student 
where sc.Sid =student.Sid 
group by sc.Sid,Student.sname 
having count(Cid)=1;

28、查询男生、女生人数

select count(Ssex) as 男生人数 
from student 
group by Ssex 
having Ssex='男';
select count(Ssex) as 女生人数 
from student 
group by Ssex 
having Ssex='女';

29、查询姓"张"的学生名单

select sname 
from student 
where sname like '张%';

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

SELECT 
st.*,
COUNT(1) 
FROM student st 
GROUP BY st.sname,st.ssex HAVING COUNT(1)>1

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

select sname, convert(char(11),DATEPART(year,sage)) as age
from student 
where convert(char(11),DATEPART(year,Sage))='1981';

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

select Cid, avg(score) 
from sc 
group by Cid 
order by avg(score), Cid desc;

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

select Sname,SC.Sid ,avg(score)     
from Student,SC      
where Student.Sid=SC.Sid 
group by SC.Sid,Sname 
having    avg(score)>85;

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

select sname, isnull(score,0) 
from student, sc ,course 
where sc.Sid=student.Sid  and sc.Cid=course.Cid and course.cname='数据库' and score<60;

35、查询所有学生的选课情况

select sc.Sid,sc.Cid,sname,cname 
from sc,student course 
where sc.Sid=student.Sid and sc.Cid=course.Cid;

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

select distinct student.Sid,student.sname,sc.Cid,sc.score 
from student,sc 
where sc.score>=70 and sc.Sid=student.Sid;

37、查询不及格的课程,并按课程号从大到小排列

select Cid 
from sc 
where score<60 
order by Cid;

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

select sc.Sid,student.sname 
from sc,student 
where sc.Sid=student.Sid and score>80 and Cid='003';

39、求选了课程的学生人数

select count(*) from sc;

40、查询选修"叶平"老师所授课程的学生中,成绩最高的学生姓名及其成绩

select student.sname,score 
from student,sc,course c, teacher 
where student.Sid=sc.Sid and sc.Cid=c.Cid
and c.Tid=teacher.Tid
and teacher.tname='叶平' 
and sc.score=(select max(score) from sc where Cid=c.Cid);
在这里插入代码片

41、查询各个课程及相应的选修人数

select count(*) from sc group by Cid;

42、查询不同课程成绩相同的学生的学号、课程号、学生成绩

select distinct a.Sid,b.score 
from sc a ,sc b 
where a.score=b.score 
and a.Cid<>b.Cid;

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

select Cid as 课程号,count(*) as 人数
from sc 
group by Cid
order by count(*) desc Cid;

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

select Sid 
from sc 
group by Sid 
having count(*)>=2;

46、查询全部学生都选修的课程的课程号和课程名

select Cid ,cname
from course 
where Cid in (select Cid from sc group by Cid);

47、查询没学过"叶平"老师讲授的任一门课程的学生姓名

select sname 
from student 
where Sid not in (select Sid from course,teacher,sc where course.Tid=teacher.Tid and sc.Cid=course.Cid 
and tname='叶平');

48、查询两门以上不及格课程的同学的学号及其平均成绩

select Sid,avg(isnull(score,0)) 
from sc 
where Sid in (select Sid from sc where score<60 group by Sid having count(*)>2)
group by Sid;

49、检索"004"课程分数小于60,按分数降序排列的同学学号

select Sid 
from sc 
where Cid='004' 
and score<60 
order by score desc;

50、删除"002"同学的"001"课程的成绩

delect from sc 
where Sid='002' 
and Cid='001';

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值