MYSQL之SQL語句練習及思路_1


關系模式

表格需求:

1.student表

sno sname age gender
1 liqiang 23 male
2 liuli 22 female
3 zhangyou 22 male

2.course表

cno cname teacher
k1 c wanghua
k5 database chengjun
k8 complie chengjun

3.score表

sno cno score
1 k1 83
2 k1 85
5 k1 92
2 k5 90
5 k5 84
5 k8 80

問題:

1.用SQL語句實現如下檢索:

(1)查詢‘程軍’老師所教授的所有課程

select cname from course where teacher like 'chengjun';

(2)查詢‘李強’同學所有課程的成績

select course.cname,score from score,student,course wre here student.sno=score.sno and score.cno=course.cno and sname='liqiang';

(3)查詢課程名為‘C’的平均成績

select avg(score) from score where cno=(select cno from course where cname='c');

(4)查詢選修了所有課程的同學的信息

select sno from score group by sno  having count(sno) >= (select  count(cno) from course);

分析:該問題中首先是要對score表進行統計,統計出sno出現的次數,理論上sno的為學生所報的課程數,若該值等於course表中課程統計數量,即為結果。

(5)檢索王老師所授課程的課程號和課程名

select cno,cname from course where teacher like 'wang%';

(6)檢索年齡大於23歲的男學生的學號和姓名

select sno,sname from student where age > 23;

(7)檢索至少選修王老師所授課程中一門課程的女學生姓名

select  distinct sname from student,score where score.cno  in (select cno from course where teacher like 'wang%') and gender='female';

select sname from student,course,score where student.sno=score.sno and course.cno=score.cno and course.teacher like 'wang%' and student.gender='female';

(8)檢索李同學不學的課程的課程號

select cno from course where cno not in (select cno from rom score,student  where student.sno=score.sno and  sname like 'li');

(9)檢索至少選修兩門課程的學生學號

select sno from score group by sno having count(sno) > 2;

(10)檢索全部學生都選修的課程的課程號和課程名

select cno,cname from course where cno in (select cno from score group by cno having count(*) = (select count(*) from student));

思路解析:學生數量=score表中單科目總數

(11)檢索選修課程包含王老師所授課程的學生學號

select sno from score where cno in (select cno from course where teacher like 'wang%');

select distinct sno from score,course where score.cno=course.cno and teacher like 'wang%';

(12)統計有學生選修的課程門數

select count(distinct cno) from score;

(13)統計選修k1課程的學生的平均成績

select avg(score) from score where cno='k1';

(14)求王老師所授課程的每門課程的學生的平均成績

select avg(score),score.cno from score,course where course.cno=score.cno and teacher like 'wang%' group by score.cno;

(15)統計每門課程的學生選修人數(超過2人的課程才統計)。要求輸出課程號和選修人數,查詢結果按人數降序排列,若人數相同,按課程號升序排列。

select cno,count(sno) from score group by cno having count(sno) > 2 order by count(sno) desc,cno asc;

(16)檢索學號比李同學打,而年齡比她小的學生姓名

select sname from student where sno > (select sno from student where sname like 'liq%' ) and age < (select age from student where sname likke 'liq%');

(17)檢索姓名以li開頭的所有學生的姓名和年齡

select sname,age from student where sname like 'li%';

(18)在score中檢索成績為空值的學生學號和課程號

select sno,cno from score where score is null;

(19)檢索年齡大於女同學平均年齡的男學生姓名和年齡

select sname,age from student where age > (select avg(age) from student where gender='female') and gender='male';

(20)檢索年齡大於所有女同學年齡的男學生姓名和年齡

select sname,age from student where age > any (select age from student where gender='female');

(21)檢索至少選修程軍老師所授全部課程的學生姓名

select sname from student,course,score  where student.sno=score.sno and course.cno=score.cno and teacher='chengjun' group by sname having count(*) >= (select count(*) from course where teacher='chengjun');

思路分析,首先找出選修程軍老師全部課程的學生,並統計該值在score表中出現的次數,該數字大於等於程軍老師教授課程總數量。

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM