SQL語句測試

班級表    學生表    分數表    課程表    老師表
前提:
    1. 一個班級具有多個學生
    2. 分數應該體現學生對應的課程
    3. 一個老師可以帶多個班級
sql語句執行順序:
	1、from
	2、where
	3、group by ··· having
	4、聚合函數
	5、select查詢語句
	6、order by排序語句
	···
# 1 查詢所有課程(course)以及對應老師(teacher)的名稱
select cname, tname from course left join teacher on course.teacher_id = teacher.tid;
# left join: 左側有的全顯示,左側沒有的不顯示,鏈接關係爲外鍵字段
# 2 查詢學生表(student)中男女生各有多少人
select gender, count(sid) from student group by gender;
# group by: 以某個字段進行分組,count對分組內容進行統計
# 3 查詢物理(course)成績(score)等於100的學生(student)姓名
select sname from student where sid in (select student_id from score where course_id = (select cid from course where cname = '物理') and num = 100)
# 子查詢: 1. 查找物理課程 2. 查看分數等於100的1 3. 查詢獲取該分數的學生姓名
# 4 查詢平均成績(score)大於80分的同學(student)的姓名跟平均成績
select sname, st.avg_num from student inner join (select student_id, avg(num) avg_num from score group by student_id having avg(num) > 80) as st on student.sid = score.student_id;
# 查詢平均成績大於80分的同學id inner join 與之關聯查看學生姓名
# 5 查詢所有學生(student)的學號,姓名,選課數(course),總成績(score)
select sid 學號, sname 姓名, count_course_id 選課數, sum_num 總成績 from student inner join (select student_id, count(course_id) count_course_id, sum(num) sum_num from score group by student_id) as st on student.sid = st.student_id;
# 根據學生分組查詢選課數,總成績,inner join與之關聯查看學生學號,姓名
# 6 查詢李姓老師(teacher)的個數
select count(tid) from teacher where tname like '李%';
# 模糊匹配過濾查找
# 7 查詢沒有報李平老師(teacher)課程(course)的學生(student)姓名
select sname from student where sid not in (select distinct student_id from score where course_id in (select cid from course where teacher_id = (select tid from teacher where tname = '李平老師')))
# 查看學生姓名 not in(報選 李平老師的課程的學生)
# 8 查詢物理課程(course)比生物課程分數(score)高的學生學號跟姓名
select t1.student_id from (select student_id, num from score inner join course on score.course_id = course.cid where course.cname = '物理') as t1 inner join (select student_id, num from score inner join course on score.course_id = course.cid where course.cname = '生物') as t2 on t1.student_id = t2.student_id where t1.num > t2.num;
# 分別查詢物理成績跟生物成績。inner join 比較
# 9 查詢沒有同時(score)選修物理課程(course)跟體育課程的學生(student)姓名
select sname from student where sid in (select student_id from score inner join course on course.cid = socre.course_id and course.cname in ('物理', '體育')  group by student_id having count(course_id) = 1);
# 查詢符合規則的學生姓名(選修了一門課程的學生)
# 10 查詢掛科超過兩門(course)的學生姓名(student)和班級(class)
select t2.sname, class.caption from (select sname, class_id from student inner join (select student_id from score where num < 60 group by student_id having count(course_id) >= 2) as t1 on student.sid = t1.student_id ) as t2 inner join class on class.cid = t2.class_id;
# 
# 11 查詢選修了所有課程(course)的學生姓名(student)
select sname from student inner join (select student_id from score group by student_id having count(course_id) = (select count(cid) from course )) as t1 on t1.student_id = student.sid;
# 
# 12 查詢李平老師(student)教的課程(course)的所有成績(score)記錄
# 13 查詢全部學生(student)都選修了的課程號跟課程名(course)
# 14 查詢每門課程(course)被選修的次數(score)
# 15 查詢只選修了一門課程(course)的學生(student)的學號跟姓名
# 16 查詢所有學生(student)考出的成績(score)並從高到低排序(去重)
# 17 查詢平均成績(score)大於85的學生(student)姓名跟平均成績
# 18 查詢生物(course)成績(score)不及格的學生(student)姓名和對應的生物分數
# 19 查詢所有選修了李平(teacher)老師課程(course)的學生(student)中,這些課程(李平老師的課程)平均成績最高的學生姓名
# 20 查詢每門課程成績(score)最好的前兩名學生(student)姓名
# 21 查詢不同課程但成績(score)相同的學號,課程號,成績
# 22 查詢沒學過“葉平”老師(teacher)課程的學生(student)姓名以及選修的課程(course)名稱;
# 23 查詢所有選修了學號爲1的同學選修過的一門或者多門課程的同學學號和姓名
# 24 任課最多的老師中學生單科成績最高的學生姓名
https://tool.lu/
https://cli.im/
https://0x3.com/
https://www.uupoop.com/
https://www.hipdf.cn/
https://showmore.com/zh/
http://weibodang.cn/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章