查询练习
簡單查詢
①求js9901班的學生學號和姓名。
select s_no,s_name from student where class_no = ‘js9901’
②求計算機系的所有班級號和班級名。
select class_no,class_name from class where class_dept = ‘計算機系’
③求選修了課程的學生學號。
select s_no from choice
④求選修了01001課程的學號和成績,并要求對查詢結果按成績降序排列,如果成績相同則按學號升序排列。
select s_no,score from choice order by score desc , s_no
⑤求選修了課程01001且成績在80至90(包括80和90)分之間的學生學號和成績,并將成績乘以0.9系數輸出。
select s_no,score,score*0.9 from choice where score between 80 and 90
⑥求講師或副教授姓王的教師信息。
select * from teacher where t_title =‘講師’ or ‘副教授’
⑦求缺少成績的學生學號和課程號。
select s_no,course_no from choice where score is null
⑧求全體學生的姓名和年齡。
select s_name, (year(now())-year(s_birthday)) 年齡 from student
連接查詢
①查詢每個學生的基本情況及其選修課程情況。
select * from student s left join choice c on s.s_no=c.s_no
②求學生的學號、姓名、選修的課程名及成績。
select st.s_no,st.s_name,ch.score,co.course_name from student st left join choice ch on (st.s_no=ch.s_no) join course co using(course_no)
③求王蕾同學的學號、姓名、班級名、所學專業及所在系別。
select s_no,s_name,class_no,class_special,class_dept from student left join class using(class_no)
④查詢所有教師的教師號、姓名、職稱和所授課程名。
select t_no,t_name,t_title,course_name from teacher join teaching using(t_no) left join course using (course_no)
⑤求所有講師所授課程的課程名稱及學分。
select course_name,course_score from teacher join teaching using(t_no) left join course using (course_no)
⑥求選修了兩門以上(含兩門)課程的學號。
select s_no, count(1) 選課數 from choice group by s_no having 選課數 >=2
⑦求選修了數據庫原理及應用課程且成績在85分以上的學生學號、姓名及成績。
select student.s_no,s_name,score from student,choice,course where course_name = ‘數據庫原理及應用’ and score >=85 and choice.course_no=course.course_no and choice.s_no = student.s_no
⑧用語句Transact-SQL表示學生基本情況表(Student)和選修課程情況表(Choice)之間的左外連接、右外連接和完全外連接。
select * from student left join choice using (s_no)
select * from student right join choice using (s_no)
select * from student full join choice using (s_no)
總結
- 上一篇: 外星人笔记本电脑标配240W电源拆解外星
- 下一篇: Excel中多个工作表汇总求和excel