sql習題:http://www.cnblogs.com/wupeiqi/articles/5729934.htmlhtml
習題答案參考:https://www.cnblogs.com/wupeiqi/articles/5748496.html (有些答案有錯)sql
-- SELECT count(*) from score WHERE num>60; -- 查找分數大於60的個數 -- select count(cid),teacher_id from course group by teacher_id; -- select tid,teacher.tname,course.cname from course left join teacher on course.teacher_id = teacher.tid; -- select count(sid),gender from student GROUP BY gender -- 男女生個數 -- 二、查詢「生物」課程比「物理」課程成績高的全部學生的學號; -- 找出生物成績,找出物理成績,聯合這兩張臨時表,找出B.num > P.num -- select B.student_id,B.cname,B.num as b_num,P.cname,P.num as p_num from -- (select * from score left join course on score.course_id = course.cid where cname = '生物') as B -- inner join -- (select * from score left join course on score.course_id = course.cid where cname = '物理') as P -- on B.student_id = P.student_id where B.num > P.num;; -- 三、查詢平均成績大於60分的同窗的學號和平均成績;(進階:以及姓名) -- 首先選擇出平均分大於60分的同窗的學號,再和學生表join,選擇出姓名 -- SELECT -- student.sid, -- student.sname, -- b1.avg_num -- FROM -- ( SELECT avg( num ) AS avg_num, student_id FROM score GROUP BY student_id HAVING avg( num ) > 60 ) AS b1 -- LEFT JOIN student ON b1.student_id = student.sid; -- 四、查詢全部同窗的學號、姓名、選課數、總成績;(兩種解法,另外一種是先把表連起來再group by) -- 這裏注意 count(1)的用法, 相似select age,1 from t1; 會出現列名1,屬性全爲1 -- 首先成績表和學生表連表,再根據學號進行分組,而後選擇出學號,姓名,聚合學科數,求和num -- SELECT -- student.sid, -- student.sname, -- b2.course_num, -- b2.sum_num -- FROM -- ( SELECT student_id, count( course_id ) AS course_num, sum( num ) AS sum_num FROM score GROUP BY student_id ) AS b2 -- LEFT JOIN student ON b2.student_id = student.sid; -- select student.sid,student.sname,count(1) as course_num,sum(num) from score left join student on score.student_id = student.sid group by score.student_id -- 五、查詢姓「李」的老師的個數; -- select count(1) from teacher where tname like '李%'; -- 六、查詢沒學過「李平」老師課的同窗的學號、姓名; -- 首先課程表和老師表join,選擇出李平老師的課程id,而後在成績表中把選擇了 這些課程id的學號選出,用學號分組後,用not in 從學生表中,選擇出沒有選擇過課程的學號和姓名 -- select sid,sname from student where sid not in (select student_id from score where course_id in (select course.cid from course left join teacher on course.teacher_id=teacher.tid where tname = '李平老師') group by student_id ); -- -- 七、查詢學過「1」課程而且也學過編號「2」課程的同窗的學號、姓名; -- 先查到既選擇001又選擇002課程的全部同窗 -- 根據學生進行分組,若是學生數量等於2表示,兩門均已選擇 -- select student.sid,student.sname from -- (select student_id from score where course_id in (1,2) group by student_id having count(*)>1) as b4 -- left join student on b4.student_id = student.sid; -- 八、查詢學過「葉平」老師所教的全部課的同窗的學號、姓名; -- SELECT sname,sid from student where sid in (select student_id from score where course_id in (select course.cid from course left join teacher on course.teacher_id = teacher.tid where tname='李平老師') group by student_id having count(*) = (select count(cid) from course left join teacher on course.teacher_id = teacher.tid where tname='李平老師')); -- 十、查詢有課程成績小於60分的同窗的學號、姓名; # distinct 若是有重複項,只選擇一個 -- select sid,sname from student where sid in ( -- select distinct student_id from score where num<60); -- 十一、查詢沒有學全全部課的同窗的學號、姓名; -- select sid,sname from student where sid not in (select student_id from score group by student_id having count(*)=(select count(1) from course)); -- 十二、查詢至少有一門課與學號爲「001」的同窗所學相同的同窗的學號和姓名; -- select distinct student_id,student.sname from score left join student on score.student_id = student.sid where student_id != 1 and course_id in (select course_id from score where student_id = 1); -- select student_id,sname, count(course_id) -- from score left join student on score.student_id = student.sid -- where student_id != 1 and course_id in (select course_id from score where student_id = 1) group by student_id -- 1三、查詢至少學過學號爲「001」同窗全部課的其餘同窗學號和姓名; -- select student_id from score where student_id !=1 and course_id in (select course_id from score where student_id = 1) group by student_id having count(*) >= (select count(course_id) from score where student_id = 1); -- 1四、查詢和「002」號的同窗學習的課程徹底相同的其餘同窗學號和姓名; #1.課程包含 002,且課程數同樣 -- select student_id,sname from score left join student on score.student_id = student.sid where student_id in ( -- select student_id from score where student_id != 2 group by student_id HAVING count(course_id) = (select count(1) from score where student_id = 2) -- ) and course_id = (select count(1) from score where student_id = 2) -- 1六、向SC表中插入一些記錄,這些記錄要求符合如下條件:①沒有上過編號「002」課程的同窗學號;②插入「002」號課程的平均成績; -- 思路: -- 因爲insert 支持 -- inset into tb1(xx,xx) select x1,x2 from tb2; -- 全部,獲取全部沒上過002課的全部人,獲取002的平均成績 -- -- insert into score(student_id, course_id, num) select sid,2,(select avg(num) from score where course_id = 2) -- from student where sid not in ( -- select student_id from score where course_id = 2) -- 1七、按平均成績從低到高顯示全部學生的「生物」、「物理」、「體育」三門的課程成績,按以下形式顯示: 學生ID,生物,物理,體育,有效課程數,有效平均分; -- SELECT -- student_id, -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 1) as 語文, -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 2) as 數學, -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 3) as 英語 -- from score as s1; -- 1八、查詢各科成績最高和最低的分:以以下形式顯示:課程ID,最高分,最低分; -- select course_id,max(num),min(num),cname,case when min(num) < 10 THEN 0 else min(num) end from score left join course on score.course_id = course.cid group by course_id; -- 1九、按各科平均成績從低到高和及格率的百分數從高到低順序; -- select course_id,avg(num),sum(case when num<60 then 0 else 1 end),sum(1),sum(case when num<60 then 0 else 1 end)/sum(1) as b from score group by course_id order by avg(num) asc,b desc; -- 20、課程平均分從高到低顯示(顯示任課老師); -- select course_id,cname,tname,avg(if(isnull(score.num),0,score.num)) from score -- left join course on score.course_id = course.cid -- left join teacher on course.teacher_id = teacher.tid -- group by course_id order by avg(num) desc; -- 2一、查詢各科成績前三名的記錄:(不考慮成績並列狀況) -- select * from -- ( -- select -- student_id, -- course_id, -- num, -- 1, -- (select num from score as s2 where s2.course_id = s1.course_id group by s2.num order by s2.num desc limit 0,1), -- (select num from score as s2 where s2.course_id = s1.course_id group by s2.num order by s2.num desc limit 3,1) as cc -- -- from score as s1 -- ) as B -- where B.num > B.cc order by B.course_id desc; -- 2六、查詢同名同姓學生名單,並統計同名人數; -- select sname,count(1) from student group by sname -- 2七、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列; -- select avg(if(isnull(score.num),0,score.num)),course_id from score group by course_id order by avg(num) asc,course_id desc; -- 2九、查詢課程名稱爲「數學」,且分數低於60的學生姓名和分數; -- select student.sname,score.num from score -- left join course on score.course_id = course.cid -- left join student on score.student_id = student.sid -- where course.cname = '生物' and score.num < 60; -- 3二、查詢選修「李平老師」所授課程的學生中,成績最高的學生姓名及其成績; -- select student_id,sname,max(num) from score left join student on score.student_id = student.sid where course_id in ( -- select course.cid from course left join teacher on course.teacher_id = teacher.tid where tname = '李平老師') group by student_id order by max(num) desc limit 0,1; -- 3四、查詢不一樣課程但成績相同的學生的學號、課程號、學生成績; -- 此處要了解笛卡兒積 -- select DISTINCT s1.course_id,s2.course_id,s1.num,s2.num from score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id; -- -- 3八、查詢沒學過「李平老師」老師講授的任一門課程的學生姓名; #找出選過李平老師的,而後在學生表 not in -- select sid,sname from student where sid not in (select student_id from score where course_id in (select course.cid from course left join teacher on course.teacher_id = teacher.tid where teacher.tname='李平老師') group by student_id)
##還能夠選擇查詢語句,不過這個語句結果須要爲一個常量 select age,name,(select count(1) from tb) from tb1; 若是不是常量,須要設定條件,不然會變爲笛卡兒積了。以下所示 # -- 1七、按平均成績從低到高顯示全部學生的「生物」、「物理」、「體育」三門的課程成績,按以下形式顯示: 學生ID,生物,物理,體育,有效課程數,有效平均分; # -- SELECT # -- student_id, # -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 1) as 語文, # -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 2) as 數學, # -- (select num from score as s2 where s2.student_id=s1.student_id and course_id = 3) as 英語 # -- from score as s1; ''' select id,(select * from tb1 where tb1.id = 1) from tb2; 此時 子查詢相似一個常量, 每一行就是 id1 常量, id2 常量這樣子 ''' ''' #?????? -- 2一、查詢各科成績前三名的記錄:(不考慮成績並列狀況) # select * from # ( # select # student_id, # course_id, # num, # 1, # (select num from score as s2 where s2.course_id = s1.course_id group by s2.num order by s2.num desc limit 0,1), # (select num from score as s2 where s2.course_id = s1.course_id group by s2.num order by s2.num desc limit 3,1) as cc # # from score as s1 # ) as B # where B.num > B.cc order by B.course_id desc; ''' #重點 s2.student_id=s1.student_id # SELECT # student_id, # (select num from score as s2 where s2.student_id=s1.student_id and course_id = 1) as 語文, # (select num from score as s2 where s2.student_id=s1.student_id and course_id = 2) as 數學, # (select num from score as s2 where s2.student_id=s1.student_id and course_id = 3) as 英語 # from score as s1; # case then 條件 else 字段 END #select course_id,max(num),min(num),cname, # case when min(num) < 10 THEN 0 else min(num) end from score left join course on score.course_id = course.cid group by course_id; #-- 1九、按各科平均成績從低到高和及格率的百分數從高到低順序; # select course_id,avg(num),sum(case when num<60 then 0 else 1 end),sum(1),sum(case when num<60 then 0 else 1 end)/sum(1) from score group by course_id; #-- 20、課程平均分從高到低顯示(顯示任課老師); # select course_id,cname,tname,avg(if(isnull(score.num),0,score.num)) from score # left join course on score.course_id = course.cid # left join teacher on course.teacher_id = teacher.tid # group by course_id order by avg(num) desc; #if(isnull(score.num),0,score.num) 若是 score.num是空,那麼就是0,不然是它自己 # -- 3四、查詢不一樣課程但成績相同的學生的學號、課程號、學生成績; # -- 此處要了解笛卡兒積,鏈接兩張表不加 on 條件,會進行笛卡兒積,就是一張表的首行遍歷鏈接另外一張表全部行,而後第二行繼續遍歷鏈接..... # -- select DISTINCT s1.course_id,s2.course_id,s1.num,s2.num from score as s1, score as s2 where s1.num = s2.num and s1.course_id != s2.course_id;