1. 程式人生 > >sql習題及答案

sql習題及答案

 

sql習題:http://www.cnblogs.com/wupeiqi/articles/5729934.html

習題答案參考:https://www.cnblogs.com/wupeiqi/articles/5748496.html  (有些答案有錯)

 

 

 

-- 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 -- 男女生個數 -- 2、查詢“生物”課程比“物理”課程成績高的所有學生的學號; -- 找出生物成績,找出物理成績,聯合這兩張臨時表,找出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;; -- 3、查詢平均成績大於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; -- 4、查詢所有同學的學號、姓名、選課數、總成績;(兩種解法,另一種是先把表連起來再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 -- 5、查詢姓“李”的老師的個數; -- select count(1) from teacher where tname like '李%'; -- 6、查詢沒學過“李平”老師課的同學的學號、姓名; -- 首先課程表和老師表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 ); -- -- 7、查詢學過“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; -- 8、查詢學過“葉平”老師所教的所有課的同學的學號、姓名; -- 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='李平老師')); -- 10、查詢有課程成績小於60分的同學的學號、姓名; # distinct 如果有重複項,只選擇一個 -- select sid,sname from student where sid in ( -- select distinct student_id from score where num<60); -- 11、查詢沒有學全所有課的同學的學號、姓名; -- 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)); -- 12、查詢至少有一門課與學號為“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 -- 13、查詢至少學過學號為“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); -- 14、查詢和“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) -- 16、向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) -- 17、按平均成績從低到高顯示所有學生的“生物”、“物理”、“體育”三門的課程成績,按如下形式顯示: 學生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; -- 18、查詢各科成績最高和最低的分:以如下形式顯示:課程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; -- 19、按各科平均成績從低到高和及格率的百分數從高到低順序; -- 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; -- 21、查詢各科成績前三名的記錄:(不考慮成績並列情況) -- 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; -- 26、查詢同名同姓學生名單,並統計同名人數; -- select sname,count(1) from student group by sname -- 27、查詢每門課程的平均成績,結果按平均成績升序排列,平均成績相同時,按課程號降序排列; -- 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; -- 29、查詢課程名稱為“數學”,且分數低於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; -- 32、查詢選修“李平老師”所授課程的學生中,成績最高的學生姓名及其成績; -- 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; -- 34、查詢不同課程但成績相同的學生的學號、課程號、學生成績; -- 此處要了解笛卡兒積 -- 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; -- -- 38、查詢沒學過“李平老師”老師講授的任一門課程的學生姓名; #找出選過李平老師的,然後在學生表 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; 如果不是常量,需要設定條件,否則會變為笛卡兒積了。如下所示
# -- 17、按平均成績從低到高顯示所有學生的“生物”、“物理”、“體育”三門的課程成績,按如下形式顯示: 學生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 常量這樣子
'''
'''
#?????? -- 21、查詢各科成績前三名的記錄:(不考慮成績並列情況)
# 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;


#-- 19、按各科平均成績從低到高和及格率的百分數從高到低順序;
# 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,否則是它本身

# -- 34、查詢不同課程但成績相同的學生的學號、課程號、學生成績;
# -- 此處要了解笛卡兒積,連線兩張表不加 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;
答題中幾道不會的題目