在2018年的mysql實訓中,學生需要完成一個題目,答案如下:
-- 1. 查詢每個部門請假天數大于等于3天的學生姓名和請假天數 SELECT sname, leavedays FROM student JOIN department ON student.dno = department.dno WHERE leavedays >= 3; -- 2. 查詢所有選修了“計算機基礎”課程且成績為80分以上的學生信息 SELECT * FROM student JOIN sc ON student.sno = sc.sno WHERE sc.cno = (SELECT cno FROM course WHERE cname = '計算機基礎') AND score >= 80; -- 3. 查詢所有2018年入學的學生及其所在系別 SELECT sname, dname FROM student JOIN department ON student.dno = department.dno WHERE in_year = 2018; -- 4. 查詢學生所選課程超過4門的學生學號和姓名 SELECT sno, sname FROM student JOIN (SELECT sno, COUNT(*) AS course_num FROM sc GROUP BY sno) temp ON student.sno = temp.sno WHERE course_num >4; -- 5. 查詢選課人數最多的前兩門課程的課程名和選課人數 SELECT cname, COUNT(*) AS num FROM course JOIN sc ON course.cno = sc.cno GROUP BY sc.cno ORDER BY num DESC LIMIT 2;
這些答案可以作為mysql實訓的參考,也可供其他mysql學習者參考。