任務9 P140 9.4.8獨立實踐

use JWGL_DB
go

--1.使用子查詢
--(1)查詢10軟件設計班的學生的學號、姓名和家庭地址。
select 學生.學號,學生.姓名,學生.家庭地址
from 學生

--(2)查詢信息工程學院和藝術設計學院學生的學號、姓名和家庭地址。--探討“當沒有用 EXISTS 引入子查詢時,在選擇列表中只能指定一個表達式”
--方法1
select 學生.學號,學生.姓名,學生.家庭地址
from 學生
where 學生.班級編號 in (select 班級.班級編號 from 班級 where 班級.專業編號 in (select 專業.專業編號 from 專業 where 專業.學院編號 in (select 學院.學院編號 from 學院 where 學院.學院名稱 in('藝術設計學院'))))
--方法2
select xs.學號,xs.姓名,xs.家庭地址
from 學生 xs,學院
where 學院.學院編號 in(select 專業.學院編號 from 專業 where 專業.專業編號 in(select 班級.專業編號 from 班級 where 班級.班級編號 in(select 學生.班級編號 from 學生 where 學生.班級編號 = xs.班級編號 ))) and 學院.學院名稱='藝術設計學院'

--(3)查詢軟件技術專業每個學生選修的課程的平均成績和學號
select 學生_課程.學號,avg(學生_課程.課程成績) as '平均成績'
from 學生_課程
where 學生_課程.學號 in(select 學生.學號 from 學生 where 學生.班級編號 
					 in(select  班級.班級編號 from 班級 where 班級.專業編號
					 in(select 專業.專業編號 from 專業 where 專業.專業名稱='軟件技術專業')))
group by 學生_課程.學號

--(4)查詢課程成績高於課程平均成績的學號和課程編號。	採用“自連接查詢”
select sc1.學號,sc1.課程編號,sc1.課程成績
from 學生_課程 sc1
where sc1.課程成績>(select avg(sc2.課程成績) from 學生_課程 sc2)

--(5)編寫查詢,查找每門課程中成績最高的成績、學號和課程編號。
select sc1.學號,sc1.課程編號,(select MAX(sc2.課程成績) from 學生_課程 sc2 group by sc2.課程編號 having sc2.課程編號 = sc1.課程編號) as '最高成績'
from 學生_課程 sc1


select sc1.課程編號,sc1.學號
from 學生_課程 sc1
where Exists (select sc2.學號,sc2.課程編號,Max(sc2.課程成績) as '最高分' from 學生_課程 sc2 group by sc2.課程編號,sc2.學號 having sc2.課程編號=sc1.課程編號 and sc2.學號=sc1.學號)
--“當沒有用 EXISTS 引入子查詢時,在選擇列表中只能指定一個表達式。”也即是說,子查詢的列表中存在多列。


--2.使用Exists子查詢
--(1)使用EXISTS進行查詢,查找已經完成“C#程序設計”選課任務的學生的姓名和學號


--方法1 
select 學生_課程.學號,c1.課程名稱 from 學生_課程,課程 c1 
where 學生_課程.課程編號 = c1.課程編號 and c1.課程名稱='C#程序設計'
--方法2
select 學生.姓名,學生.學號
from 學生
where Exists (select 學生_課程.學號 from 學生_課程 where 學生_課程.課程編號 in(select 課程.課程編號 from 課程 where 課程名稱='C#程序設計'))

--(2)查詢已經選修了6門或6門以上課程的學生的姓名和學號。
select sd1.學號,sd1.姓名
from 學生 sd1
where sd1.學號 in (select sc2.學號 from 學生_課程 sc2 group by sc2.學號 having count(sc2.學號)>=6)

--(3)使用Exists進行查詢,查找沒有完成“C#程序設計”選課任務的學生的姓名和學號。
/*select 學號  錯誤的做法
from 學生
where not Exists 
(select 學生_課程.課程編號 from 學生_課程 where 學生_課程.課程編號 in (select 課程編號 from 課程 where 課程名稱 = 'C#程序設計'))*/

select 姓名,學號
from 學生
where not Exists (select * from 課程,學生_課程 where 課程名稱 = 'C#程序設計' and 課程.課程編號 = 學生_課程.課程編號 and 學生_課程.學號 = 學生.學號)

--(4)使得Exists進行查詢,查找沒有選修任何課程的學生的姓名和學號。
--方法(1)
(select 學生.學號,學生.姓名 from 學生 where 學生.學號 not in (select 學生_課程.學號 from 學生_課程))
--方法(2)
select 姓名,學號
from 學生
where not Exists (select distinct 學生_課程.學號 from 學生_課程,學生 where 學生.學號 = 學生_課程.學號)

--3.在表達式中使用子查詢
--查詢每個學生選修的每門課程的成績及每門課程的平均成績、課程的成績和該課程的平均成績的差值及學生的學號。
select sc1.學號,sc1.課程編號,sc1.課程成績,(select avg(sc3.課程成績) from 學生_課程 sc3 where sc3.課程編號 = sc1.課程編號 group by sc3.課程編號) as '每門課程的平均成績'
,(sc1.課程成績 - (select AVG(sc2.課程成績) from 學生_課程 sc2)) as '該課程的平均成績的差值'
from 學生_課程 sc1

--4 在Update,Delete語句中使用子查詢
--(1)編寫程序,將10軟件設計班學生的課程成績少於60分的學生的成績加5分
update 學生_課程
set 課程成績+=5
where 課程成績<60 and 學生_課程.學號 in (select 學生.學號 from 學生 where 學生.班級編號 in (select 班級.班級編號 from 班級 where 班級.班級名稱='10軟件設計班'))

/*驗證4(1)結果
select 學生_課程.課程成績,學生_課程.學號,學生_課程.課程編號
from 學生_課程
where 學生_課程.課程成績 <60

select 學生_課程.課程編號,學生_課程.課程成績,(select 班級.班級名稱 from 班級 where 班級.班級編號 in (select 學生.班級編號 from 學生 where 學生.學號 =學生_課程.學號)) as '班級名'
from 學生_課程
where 學生_課程.學號 in (select 學生.學號 from 學生 where 學生.班級編號 in(select 班級.班級編號 from 班級 where 班級.班級名稱='10軟件設計班')) and 學生_課程.課程成績<60
*/
--(2)運用子查詢刪除1993年前入學的學生的課程成績
select sc1.課程成績,sc1.學號 --,(select s1.姓名 from 學生 s1 where s1.學號=sc1.學號) as '姓名'
from 學生_課程 sc1
where sc1.學號 in (select 學生.學號 from 學生 where 學生.出生日期<'1993-1-1')

s
drop Procedure StudentTb
Create Procedure StudentTb
(
	@學號 char(12)=null
)
As
select * from 學生
where 學生.學號 = @學號
Go

Exec StudentTb  '1213430101'

寫於2013-05-21

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章