45道經典SQL題練習(四)
阿新 • • 發佈:2018-11-27
-- 31、查詢所有教師和同學的name、sex和birthday. SELECT TNAME AS '名字',TSEX AS SEX,TBIRTHDAY AS BIRTHDAY FROM TEACHER UNION SELECT SNAME AS '名字',SSEX AS SEX,SBIRTHDAY AS BIRTHDAY FROM STUDENT; -- 32、查詢所有“女”教師和“女”同學的name、sex和birthday. SELECT TNAME AS '名字',TSEX AS SEX,TBIRTHDAY AS BIRTHDAY FROM TEACHER WHERE TSEX='女' UNION SELECT SNAME AS '名字',SSEX AS SEX,SBIRTHDAY AS BIRTHDAY FROM STUDENT WHERE SSEX='女'; -- *自連線* 注意此題 注意此題 注意此題 注意此題 注意此題 注意此題 -- 33、查詢成績比該課程平均成績低的同學的成績表。 SELECT * FROM SCORE A WHERE DEGREE<(SELECT AVG(DEGREE) FROM SCORE B WHERE B.CNO=A.CNO); -- *自連線* 注意此題 注意此題 注意此題 注意此題 注意此題 注意此題 -- 34、查詢所有任課教師的Tname和Depart. SELECT TNAME,DEPART FROM TEACHER; -- 35 查詢所有未講課的教師的Tname和Depart. SELECT TNAME,DEPART FROM TEACHER WHERE TNO NOT IN (SELECT TNO FROM COURSE WHERE CNO IN (SELECT CNO FROM COURSE WHERE CNO IN (SELECT CNO FROM SCORE))); -- 36、查詢至少有2名男生的班號。 SELECT SCLASS FROM STUDENT WHERE SSEX='男' GROUP BY SCLASS HAVING COUNT(SSEX)>1; -- 37、查詢Student表中不姓“王”的同學記錄。 SELECT SNAME FROM STUDENT WHERE SNAME NOT LIKE'王%'; -- 38、查詢Student表中每個學生的姓名和年齡。 SELECT SNAME,(YEAR(NOW())-YEAR(SBIRTHDAY)) AS YEARS FROM STUDENT; -- 39、查詢Student表中最大和最小的Sbirthday日期值。 SELECT MAX(SBIRTHDAY),MIN(SBIRTHDAY) FROM STUDENT; -- 40、以班號和年齡從大到小的順序查詢Student表中的全部記錄。 SELECT * FROM STUDENT ORDER BY SCLASS DESC,SBIRTHDAY DESC; -- 41、查詢“男”教師及其所上的課程。 SELECT * FROM COURSE WHERE TNO IN(SELECT TNO FROM TEACHER WHERE TSEX='男'); -- 42、查詢最高分同學的Sno、Cno和Degree列。 SELECT SNO,CNO,DEGREE FROM SCORE WHERE SNO=(SELECT SNO FROM SCORE WHERE DEGREE=(SELECT MAX(DEGREE) FROM SCORE)); -- 43、查詢和“李軍”同性別的所有同學的Sname. SELECT SNAME FROM STUDENT WHERE SSEX=(SELECT SSEX FROM STUDENT WHERE SNAME='李軍'); -- 44、查詢和“李軍”同性別並同班的同學Sname. SELECT SNAME FROM STUDENT WHERE SSEX=(SELECT SSEX FROM STUDENT WHERE SNAME='李軍') AND SCLASS=(SELECT SCLASS FROM STUDENT WHERE SNAME='李軍') AND SNAME NOT LIKE '李軍'; -- 45、查詢所有選修“計算機導論”課程的“男”同學的成績表 SELECT SNO,DEGREE FROM SCORE WHERE CNO=(SELECT CNO FROM COURSE WHERE CNAME='計算機導論') AND SNO IN(SELECT SNO FROM STUDENT WHERE SSEX='男');