MySQL條件查詢

student(Sid,Sname,Sage,Ssex)學生表
Sid:學號
Sname:學生姓名
Sage:學生年齡
Ssex:學生性別


Course(Cid,Cname,T#)課程表
Cid:課程編號
Cname:課程名稱
Tid:教師編號


SC(Sid,Cid,score)成績表
Sid:學號
Cid:課程編號
score:成績


Teacher(Tid,Tname)教師表
Tid:教師編號:
Tname:教師名字


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 Sid,avg(score) from SC group by Sid having avg(score)>60;

3、查詢所有同學的學號、姓名、選課數、總成績
select SC.Sid,Sname,count(Cid),sum(score) from SC,student where SC.Sid=student.Sid group by SC.Sid;


4、查詢姓‘李’的老師的個數:
select count(Tname) fromTeacher where tname like '李%';

5、查詢沒有學過“葉平”老師可的同學的學號、姓名:
select Sid,Sname from student where Sid not in (select Sid from SC where Cid in(select c.Cid from Course c,Teacher t where c.Tid=t.Tid and Tname='李老師' )); 


6、查詢學過李老師所教的所有課的同學的學號、姓名:

select student.Sid,Sname from student,SC where student.Sid=SC.Sid and Cid in (select Cid from Course ,Teacher where Course.Tid=Teacher.Tid and Tname='李老師')  group by SC.Sid having count(Cid)= (select count(Cid) from Course,Teacher where Course.Tid=Teacher.Tid and Tname='李老師') ;


7、查詢學過語文並且也學過編號2課程的同學的學號、姓名:

select s.Sid,s.Sname from student s,SC sc1,SC sc2 where s.Sid=sc1.Sid and s.Sid=sc2.Sid and sc1.Cid=2 and sc2.Cid in (select Cid from Course where Cname='語文');

8、查詢課程編號“002”的成績比課程編號“001”課程低的所有同學的學號、姓名:

select Sid,Sname from student where Sid in (select sc1.Sid from SC sc1,SC sc2 where sc1.Sid=sc2.Sid and sc1.Cid=1 and sc2.Cid=2 and sc1.score>sc2.score);

9、查詢所有課程成績小於60的同學的學號、姓名:

select Sid,Sname from student where Sid not in (select distinct Sid from SC where score>60);


10、查詢沒有學全所有課的同學的學號、姓名:(where group by having)

select s.Sid,Sname from student s,SC sc  where s.Sid=sc.Sid group by s.Sid having count(Cid)<(select count(Cid) from Course);


11、查詢至少有一門課與學號爲“10”同學所學相同的同學的學號和姓名:

select  distinct s.Sid,s.Sname from student s,SC sc where s.Sid=sc.Sid and  Cid in (select Cid from SC where Sid=10);

12、查詢學過和學號爲“10”同學所有課的同學的學號和姓名;

select s.Sid,s.Sname from Student s,SC sc where s.Sid=sc.Sid and Cid in (select Cid from SC where Sid=10) group by s.Sid having count(Cid)=(select count(Cid) from SC where Sid=10);


13、把“SC”表中李老師教的課的成績都更改爲此課程的平均成績:??

update SC sc set sc.score=(select avg(sc2.score) from SC sc2 ) where sc2.Cid=sc.Cid group by sc2.Cid and Cid in (select c.Cid from Course c,Teacher t where c.Tid=t.Tid and Tname='李老師'); 


update SC set score = (select avg(sc_2.score) from SC sc_2  where sc_2.cid = sc.cid) where cid in (select c.Cid from course c 
left join teacher t on t.Tid = c.Tid where t.Tname = '李老師');

15、刪除學習“葉平”老師課的SC表記錄:
delete from sc WHERE
cid in (
select c.cid from course c 
LEFT JOIN teacher t on c.tid=t.tid 
where t.tname = '葉平');

17、按平均成績從高到低顯示所有學生的“語文”、“數學”、“英語”三門的課程成績,按如下形式顯示:學生ID,語文,數學,英語,有效課程數,有效平均分:???


select distinct Sid as 學生id,(select score from SC sc1,Course c where sc1.Cid=c.Cid and sc1.Sid=sc.Sid and Cname='語文') as 語文,(select score from SC sc,Course c where sc.Cid=c.Cid and Cname='數學') as 數學,(select score from SC sc,Course c where sc.Cid=c.Cid and Cname='英語') as英語,count (Cid) as 有效課程數,avg(score) as 有效平均分 from SC sc group by Sid order by avg(score) desc;


18、查詢各科成績最高和最低的分:以如下的形式顯示:課程ID,最高分,最低分

select Cid as 課程ID,(select max(score) from SC sc1 where sc1.Cid=sc.Cid ) as 最高分, (select min(score) from SC sc2 where sc2.Cid=sc.Cid) as 最低分 from SC sc group by Cid;

19、按各科平均成績從低到高和及格率的百分數從高到低順序:


SELECT t.cid as 課程號,
c.cname as 課程名,
COALESCE(avg(score),0) as 平均成績,
100*sum(case 
when COALESCE(score,0)>=60 
then 1 else 0 END)/count(*) as 及格百分數
from sc t
left join course c 
on t.cid = c.cid
group by t.cid
order by 100*sum(case 
when COALESCE(score,0)>=60 
then 1 else 0 END)/count(*);
1
2
3
4
5
6
7
8
9
10
11
12
13
20、查詢如下課程平均成績和及格率的百分數(用”1行”顯示): 企業管理(001),馬克思(002),OO&UML (003),數據庫(004):
21、查詢不同老師所教不同課程平均分從高到低顯示:
select t.tid as 教師id,
t.tname as 教師姓名,
sc.cid as 課程id,
avg(score) as 平均成績
from sc as sc
LEFT JOIN course c on sc.cid = c.cid
left join teacher t on c.tid = t.tid
group by sc.cid 
order by avg(sc.score) desc;
1
2
3
4
5
6
7
8
9
22、查詢如下課程成績第3名到第6名的學生成績單:企業管理(001),馬克思(002),UML(003),數據庫(004):
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-0]'
from sc as sc 
left join course as c
on sc.cid = c.cid
group by sc.cid;
1
2
3
4
5
6
7
8
9
24、查詢學生平均成績及其名次:
select 1+(select count(distinct 平均成績) 
from (select sid,avg(score) as 平均成績 
from sc group by sid)t1 
where 平均成績>t2.平均成績) as 名次,
sid as 學生學號,平均成績 
from (select sid,avg(score) 平均成績 from sc group by sid) as t2
order by 平均成績 desc;
1
2
3
4
5
6
7
25、查詢各科成績前三名的記錄(不考慮成績並列情況):
select sid,cid,score
from sc sc_1
where (
select count(3) from sc sc_2 
where sc_1.cid = sc_2.cid 
and sc_2.score>=sc_1.score)<=2 
order by sc_1.cid
);
1
2
3
4
5
6
7
8
26、查詢每門課程被選修的學生數:
select cid, count(sid)
from sc 
group by cid;
1
2
3
27、查詢出只選修一門課程的全部學生的學號和姓名:
select sc.sid,s.sname,
count(sc.cid) as 課程數
from sc as sc
LEFT JOIN student as s
on sc.sid = s.sid
group by sc.sid
having count(sc.cid)=1;
1
2
3
4
5
6
7
28、查詢男生、女生人數:
select count(ssex) as 男生人數
from student
group by ssex
having ssex = '男';
select count(2) from student
where ssex = '女';
1
2
3
4
5
6
29、查詢姓“張”的學生名單:
select sid,sname
from student 
where sname like '張%';
1
2
3
30、查詢同名同姓的學生名單,並統計同名人數:
select sname,count(8)
from student 
group by sname
having count(8)>1;
1
2
3
4
31、1981年出生的學生名單(注:student表中sage列的類型是datetime):
32、查詢平均成績大於85的所有學生的學號、姓名和平均成績:
select s.sname,sc.sid,avg(sc.score) as 平均成績
from sc as sc
left join student as s 
on sc.sid = s.sid
group by sc.sid 
having avg(sc.score)>85;
1
2
3
4
5
6
33、查詢每門課程的平均成績,結果按平均成績升序排序,平均成績相同時,按課程號降序排列:
select cid,avg(score)
from sc 
group by cid
order by avg(score),cid desc;
1
2
3
4
34、查詢課程名稱爲“數據庫”,且分數低於60的學生名字和分數:
select c.cname,s.sid,s.sname,sc.score
from course c
left join sc on sc.cid = c.cid
LEFT JOIN student s on s.sid = sc.sid
where c.cname = '數據庫' and sc.score<60;
1
2
3
4
5
35、查詢所有學生的選課情況:
select sc.sid,sc.cid,s.sname,c.cname
from sc 
LEFT JOIN course c on sc.cid = c.cid
left join student s on sc.sid = s.sid;
1
2
3
4
36、查詢任何一門課程成績在70分以上的姓名、課程名稱和分數:
select distinct s.sid,s.sname,sc.cid,sc.score
from sc 
left join student s on sc.sid = s.sid
left join course c on sc.cid = c.cid
where sc.score>70;
1
2
3
4
5
37、查詢不及格的課程,並按課程號從大到小的排列:
select cid
from sc 
where score<60
ORDER BY cid;
1
2
3
4
38、查詢課程編號爲“003”且課程成績在80分以上的學生的學號和姓名:
select sc.sid,s.sname 
from sc 
left join student s on sc.sid = s.sid
where sc.cid = '003' and sc.score>80;
1
2
3
4
39、求選了課程的學生人數:
select count(2) from 
(select distinct sid from sc)a;
1
2
40、查詢選修“葉平”老師所授課程的學生中,成績最高的學生姓名及其成績:
select s.sname,sc.score
from sc sc 
left join student s on sc.sid = s.sid
left join course c on sc.cid = c.cid
left join teacher t on c.tid = t.tid
where t.tname = '葉平'
and sc.score = (
select max(score) 
from sc sc_1 
where sc.cid = sc_1.cid);
1
2
3
4
5
6
7
8
9
10
41、查詢各個課程及相應的選修人數:
select cid,count(*) from sc group by cid;
1
42、查詢不同課程成績相同的學生和學號、課程號、學生成績:
select DISTINCT a.sid,a.cid,a.score
from sc as a ,sc as b 
where a.score = b.score
and a.cid <> b.cid;
1
2
3
4
43、查詢每門課程成績最好的前兩名:
44、統計每門課程的學生選修人數(超過10人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排序,若人數相同,按課程號升序排序:
select cid as 課程號,count(8) as 選修人數
from sc
group by cid
HAVING count(sid)>10
order by count(8) desc,cid;
1
2
3
4
5
45、檢索至少選修兩門課程的學生學號:
select sid
from sc
group by sid
having count(8)>=2;
1
2
3
4
46、查詢全部學生選修的課程和課程號和課程名:
select cid,cname
from course 
where cid in (select cid from sc group by cid);
1
2
3
47、查詢沒學過”葉平”老師講授的任一門課程的學生姓名:
select sname 
from student 
where sid not in (
    select sid 
    from sc,course,teacher 
    where course.tid = teacher.tid and sc.cid = course.cid 
    and teacher.tname='葉平'
);
1
2
3
4
5
6
7
8
48、查詢兩門以上不及格課程的同學的學號以及其平均成績:
select sid,avg(COALESCE(score,0))
from sc
where sid in (
    select sid 
    from sc 
    where score<60 
    group by sid 
    having count(8)>2
)
group by sid;
1
2
3
4
5
6
7
8
9
10
49、檢索“004”課程分數小於60,按分數降序排列的同學學號:
select sid,score
from sc
where cid='004'
and score<60
order by score desc;
1
2
3
4
5
50、刪除“002”同學的“001”課程的成績:
delete from sc
where sid = '002'
and cid = '001';
————————————————
版權聲明:本文爲CSDN博主「我家有隻小熊二」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/hundan_520520/article/details/54881208

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章